Interacting with Session 0 Out-Of-Process Excel Object in VBScript

795 Views Asked by At

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.

0

There are 0 best solutions below