Unable to open excel files using a cscript in SQL Server Jobs

Unable to open excel files using a cscript in SQL Server Jobs

Rate This
  • Comments 22

·         Disclaimer : Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

·         The following article discusses the complications that developers may face. The article also offers alternatives to Automation that can speed performance. Developers should be aware, however, that the suggestions that this article provides are for informational purposes only.


·         Please follow the steps provided in the “Resolution” section only if you have a critical business requirement to resolve the issue discussed below in the “Problem Description” section.


Problem Description


·         SQL Server jobs are used to execute a cscript

·         This cscript tries to open an excel file

·         Though the job executes successfully the excel file was not opened and an error was logged in the Job history

·         The Type of SQL job is "operating system (cmdexec)"

·         Command used in the SQL Job is

o    cscript "c:\sample\sample.vbs"


***<Sample CSCRIPT>***

option explicit

  Dim m_oExcel

 Dim m_oWorksheet

Dim oWB

Dim oSheet

Dim m_sTemplate


 m_sTemplate = "c:\sample\sample.xls"

WriteOut " "

WriteOut ">>>>>>>>>>>>>>>>>>>>>>>Starting Excel at " & cstr(now) & " >>>>>>>>>>>>>>>>>>>>>>>"

WriteOut " "

Set m_oExcel = CreateObject("Excel.Application")

'm_oExcel.Visible = False

'm_oExcel.ScreenUpdating = False

m_oExcel.Visible = True

m_oExcel.ScreenUpdating = True

WriteOut ">>>>>>>>>>>>>>>>>>>>>>>Pre-Open"

Set oWB = m_oExcel.Workbooks.Open(m_sTemplate, 3)

WriteOut ">>>>>>>>>>>>>>>>>>>>>>>File Opened "

Set oSheet = oWB.Worksheets(1)

'm_oExcel.Visible = True

'm_oExcel.ScreenUpdating = True

m_oExcel.DisplayAlerts = False


Set m_oExcel = Nothing

'End of main code

   'Begin of function definitions

Function WriteOut(sString)

     WScript.Echo sString

End Function

***</Sample CSCRIPT>***


·         The error in the sql job history is

            Date  4/9/2010 8:55:21 AM

            Log  Job History (test)


            Step ID  1

            Server  <Server name>

            Job Name  <Job name>

            Step Name  <Step name>

            Duration  00:00:00

            Sql Severity  0

            Sql Message ID  0

            Operator Emailed 

             Operator Net sent 

             Operator Paged 

             Retries Attempted  0



            Executed as user: <User name>. Microsoft (R) Windows Script Host Version 5.7  Copyright (C) Microsoft Corporation. All rights reserved.      

             Microsoft Office Excel: Microsoft Office Excel cannot access the file '<location>\<filename>.xls'.

There are several possible reasons:  The file name or path does not exist.  The file is being used by another program.   The workbook you are trying to save has the same name as a currently open workbook.  Process Exit Code 0.  The step succeeded.


·         When tried to run the cscript from the command it would open the xls file and write value into it

·         We tried to reproduce the issue on a 32bit SQL Server and got the same error




·         A “Desktop” folder seems to be necessary in the “systemprofile” folder in the location C:\Windows\SysWOW64\config\ to open an Excel file

·         Create the “Desktop” folder for Windows 2008 Server (x64) under the location C:\Windows\SysWOW64\config\systemprofile

·         And for a 32 bit Windows 2008 Server create the “Desktop” folder under the location C:\Windows\System32\config\systemprofile

·         After creating the folder the SQL Server jobs should execute successfully


Bharath Kumar
SE, Microsoft SQL Server

Reviewed by
Shamik Ghosh, Akbar Farishta & Amit Banerjee
CSS , Microsoft SQL Server

Leave a Comment
  • Please add 1 and 4 and type the answer here:
  • Post
  • Seems also to apply to running vbs scripts from Task Scheduler. Thanks for the tip.

  • Seems also to apply to running vbs scripts from Task Scheduler. Thanks for the tip.

  • Thank You!!!  I spent almost a whole day trying to get several of our automated reports to run after moving them from Server 2003 to 2008 before a coworker ran across this article.  You would think that Microsoft Office, Microsoft VB .NET, Microsoft SQL Server and Microsoft Windows Server would all play well together but I see this more often than I should.  Seriously? An empty directory?  Thanks for yet another headache Micro$oft.

  • Thank you also!!!  I (like a long list of others in this and other forums) spent most of a day - luckily not more - trying to get a simple vbscript that opens Excel and runs a macro to work.  Used to work fine in 2003.  Only good thing is came across this article at 4.15pm on Friday afternoon and I've got a supply of cold beers at home to help wind down a very frustrating day.

  • Thanks Bharat for this article ,Regards Nitin Sarwahi

  • Thanks for the tip. It works very well.

  • same issue with apache/php from system() function.

    same resolution also

  • I'm newbie in vbs, I took a week to find this post. I never imagin this could be possible.

  • Excellent ... thanks a lot

  • Wow!  How on earth did you figure this out?  I had written a VB.NET executable that opens an Excel file and runs a macro.  It ran fine when run from my machine but failed with the same error you got when I ran it as a Scheduled Task.  Adding the "Desktop" folder fixed it.  Thanks a lot!

  • Golden tip! Just spent 3 days working my way through all kinds of fora, but this one provided the solution. Respect!

  • If I didn't already have a first-born, I'd name him after you!

  • I should add I was trying to run a vbscript program that opened a spreadsheet via the Business Objects Central Management Console, and this worked for me too.  Thank you!

  • WOW... thanks for the help. It works now!

  • OMG! Thank you so much! I had a VBScript launching from a Scheduled Task that opened an Excel template and did some Excel automation. It was failing with the meaningless "Microsoft Office Excel cannot access the file"

    Thank goodness for Google! Just adding the Desktop folder worked like a charm!

Page 1 of 2 (22 items) 12