I have a very simple VBA script that I am running on Windows Server 2012. It creates an Excel Object, writes to two cells, saves the sheet as a CSV, and closes the Workbook and the Application, and then quits the Excel Object.
This works on Server 2003 without any issues.
When I run the script on Server 2012 R2, the Excel Object Process continues running and has to be terminated manually through task manager.
As I understand, the Excel application is running in Session-0, preventing the object from being closed by the script running in the user session. Additionally, if I alter the script to create the file and save/close the object then re-open the Excel file, it is unable to write to the file as again, it's in Session-0.
When the script runs, it creates an error in the error log of:
Service Control Manager Event 2073: The Interactive Services Detection
service terminated with the following error: Incorrect function.
The script is as follows:
set fso = CreateObject("Scripting.FileSystemObject")
currDir = fso.GetParentFolderName(Wscript.ScriptFullName)
outputFile = currDir & "\ExcelTest.csv"
set objExcel = CreateObject("Excel.Application")
set ExcelWorkbook= objExcel.workbooks.add()
objExcel.application.visible = false
objExcel.application.displayalerts =false
set objExcelWorksheet = objExcel.worksheets(1)
objExcelWorksheet.Cells(1, 1).Value = "Foo"
objExcelWorksheet.Cells(1, 2).Value = "Bar"
ExcelWorkbook.saveas outputFile,23
ExcelWorkbook.close false
objExcel.application.quit
objExcel.quit
set objExcel = nothing
I am looking for either a way to force the Excel object to open in the User Session, or somehow be able to control/access that Excel object. The above code is a simplified snippet of a larger script which opens and closes multiple CSV files, and as such I do need the ability to access those Excel objects.
Caveats: I have no access or ability to change the registry, nor install any third-party applications. Security is tight as we are a HealthCare-related shop.
Related
I am able to open QTP Application using vb script but when i try to open the file, it is opening in read only mode and when i run the script it don't read data from the excel sheet it should have been reading from.
Set oShell = CreateObject ("WScript.Shell")
Dim qtpAppObj,qtpTest
'Create the QTP Application object
Set qtpAppObj = CreateObject("QuickTest.Application")
'Open the test in read-only mode
qtpAppObj.Open c:\test, True
'set run settings for the test
Set qtpTest = qtpAppObj.Test
I am using the code above
So i want to read an excel file and i think it can't be read because of the read-only mode.
This True is for opening the test in read only mode.
qtpAppObj.Open c:\test, True
Change it to false and run
qtpAppObj.Open c:\test, false
Can you Please show the code which is reading the excel file.
I think you maybe reading data from Datatable.
So thats why it is reading from inside the test but not able to read with QTP AOM.
Please show the excel reading code.
I have a simple question, but I've searched for this and couldn't find any helpful topics..
I'm working on a VBScript that opens an Excel file and modify a few stuff in it.. so I'm using this code:
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Visible = False
objXLApp.DisplayAlerts = False
Set objXLWb = objXLApp.Workbooks.Open(FilePath)
Now, what I want to do is to open the Excel file using a way that locks the file and prevents the user from opening it while it's open by the script (until it's closed).
Update:
I think the problem is somehow related to the Excel instances, I tried to do the following (while the file is open by the script):
When I manually open the file (while it's open by the script) they're both become a single instance.
When I open any other Excel file they're both also become a single instance!!! And the original file (opened by the script) becomes visible!
Now this is weird because I'm using CreateObject("Excel.Application") and not GetObject(, "Excel.Application")
There is registry key HKEY_CLASSES_ROOT\Excel.Sheet.8\shell\Open\command on Win 7 Excel 2010 for me with default value "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" /dde. The command line /dde switch enables DDE (Dynamic Data Exchange mechanism - an ancient Win 3.0 interprocess communication method) that forces Excel to start in a single instance. I've tried to remove that switch and opened workbooks, but to no avail. BTW, if you don't have a permission to edit the registry, or you intend to distribute your script to someone who doesn't, that is not a way. Also have tried this answer, but it doesn't work for Win 7 Office 2010.
I've tested test.xlsm file with DDE enabled. When user opens a file, actually it is just reopened in existing instance that make it visible. If any changes has been already made by the script, then Excel alerts:
Anyway write-access is given for the user. After that when the script saves the file, another alert appears:
Some time ago I created a script that worked with Excel application, and encountered the same issue with Win 7 Excel 2010 as you are describing. I noticed that if there were several Excel application instances created with CreateObject() within script, then Excel file opened by user always used exactly the first created instance. I've solved the issue by creating two invisible instances of Excel application, let's say dummy and target. In outline the algorithm for a script is as follows:
Create dummy instance first, no need to add a workbook. After that the dummy instance is exposured an Excel file to be opened by user within it.
Create target instance.
Quit dummy instance.
Open target workbook, modify and save it.
Quit target instance.
Consider the below code that illustrates a possible way to implement what you need:
' target file path
sPath = "C:\Users\DELL\Desktop\test.xlsm"
' create dummy instance
Set oExcelAppDummy = CreateObject("Excel.Application")
' create target instance
Set oExcelApp = CreateObject("Excel.Application")
' quit dummy instance
oExcelAppDummy.Quit
' open target workbook
With oExcelApp
.Visible = False
.DisplayAlerts = False
Set oWB = .Workbooks.Open(sPath)
End With
' make some changes and save
Set oWS = oWB.Sheets(1)
oWS.Cells(1, 1).Value = Now()
oWB.Save
' give additional time for test
MsgBox "Try to open test.xlsm, OK to end the script"
' close target workbook
oWB.Close
' quit target instance
oExcelApp.Quit
Trying open the file you will get desired output:
And the notification after the script ends:
That is strange that you aren't getting a message as below:
One possible method would be
to change the file attributes at the start and end of the code, the version below makes the file readonly and hidden
make your changes
save the file with a different name
change the attributes back
rename the changed file to the original name
code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objXLApp = CreateObject("Excel.Application")
filePath = "C:\Temp\MyFile.xlsm"
filePath2 = "C:\Temp\MyFile1.xlsm"
set objFile = objFSO.GetFile(filePath)
objFile.Attributes = 3
objXLApp.Visible = False
objXLApp.DisplayAlerts = False
Set objxlWB = objXLApp.Workbooks.Open(filePath)
'do stuff
objxlWB.saveas filePath2
objxlWB.Close
objXLApp.Quit
set objXLApp = Nothing
objFile.Attributes = 32
objFile.Delete
objFSO.MoveFile filePath2, filePath
I want to run test on QTP using vb script, I am able to open QTP and start running the test. But, the test is unable to access the external data sheet which is required to fetch data from.
This is the code:
Set oShell = CreateObject ("WScript.Shell")
Dim qtpAppObj,qtpTest
'Create the QTP Application object
Set qtpAppObj = CreateObject("QuickTest.Application")
'Open the test in read-only mode
qtpAppObj.Open c:\test, True
'set run settings for the test
Set qtpTest = qtpAppObj.Test
I don't have much knowledge in creating a VBScript file. I have a code i want to automate to send email out every month. With some research i found the code below:
dim EXL
set EXL = CreateObject("Excel.Application")
'not required
EXL.Visible = true
'your file and macro
EXL.Workbooks.Open "full path to your excel file including extension here"
EXL.Run "Fixing"
'close everything
EXL.Quit
Set EXL = Nothing
My Question is: do i implement this code into my excel module or is in the worksheet event?
Once i have this correctly applied i will be able to set the Windows Task Scheduler to run at the particular time.
Just save the VBscript file as a *.vbs file. Create a text file (txt), copy your code and save the file with the vbs extension. Next go to the Windows Task Scheduler and choose Run a Program and locate your VBS.
Sending emails from VBscript
http://www.analystcave.com/excel-send-email-excel-workbook/
Scheduling programs to run in Windows
http://windows.microsoft.com/en-au/windows/schedule-task#1TC=windows-7
I have an Excel file that gets external data from database table. I need to refresh the file automatically and email it. I intend to use SSIS script task to run some VB script that would open the file, refresh data, save and close (obviously without bringing up the application). then I'll use email task to send the file by email. All I need is the script that refreshes the file and being total noob in VB or C# I have to ask if anyone has a script that does that lying around and which I could customize for my file and use in my script task.
I'll appreciate any hints!
thanks a lot,
Vlad
Hope this is what you looking for
' Create an Excel instance
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
' Disable Excel UI elements
oExcel.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("absolute path to your file")
oWorkbook.RefreshAll
oWorkbook.Save
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
In my case (Connection to MS SQL DB), I had to uncheck the "enable background refresh" option for working fine.
Excel: Data > Connections > Properties > (uncheck) enable background refresh
Old post but 4M01 answer has helped me out heaps.
In my case I had to put a sleep just after opening the workbook to ensure the file loads correctly.
i.e.
oWorkbook = oExcel.Workbooks.Open("absolute path to your file")
Threading.Thread.Sleep(3000)
oWorkbook.RefreshAll
Note also in VS 2015 set is no longer required. so just remove "set " for the code to work.
This may not exactly fit your needs, but if it helps you or someone I was able to just use a simple
Execute Process Task
with the
Executable as /..path../Excel.exe and the
Arguments as the desired file (full path) to be opened