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

·         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

Comments (22)

  1. Scott says:

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

  2. Scott says:

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

  3. Matt says:

    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.

  4. Michael says:

    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.

  5. Nitin Sarwahi says:

    Thanks Bharat for this article ,Regards Nitin Sarwahi

  6. ngocphu says:

    Thanks for the tip. It works very well.

  7. Ben says:

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

    same resolution also

  8. Ben says:

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

  9. yasir says:

    Excellent … thanks a lot

  10. Chris says:

    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!

  11. Koen says:

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

  12. Gary says:

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

  13. Gary says:

    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!

  14. Ben says:

    WOW… thanks for the help. It works now!

  15. Bob says:

    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!

  16. Norma says:

    Thanks heaps for posting!

  17. DUNGTRAN says:

    Good solution. It work very well.

    Thanks a lot…!

  18. Mike says:



    C:WindowsSystem32configsystemprofileDesktop AND THIS RESOLVED THE ISSUE!!!!!


    Yes I am using all CAPS because I just spent the last 3 weeks of my life tearing my code apart and all it was was this stupid Microsoft crap that I should of known it was in the first place! I created the folder and the script ran PERFECTLY! WTF? I mean really? These Microsoft programmers are paid top dollar and we are paying top dollar for Microsoft products and they can't even fix stupid bugs like this? I have never seen, in any programming language, where you needed an empty folder in some random directory for a completely unrelated script to open a completely unrelated application in a completely unrelated directory.

  19. James Fogel says:

    Thank you, thank you thank you. You saved me a ton of work. Also, you may need to explixitly grant permissions to this folder to the account SQL Server (not the agent) is running under.

  20. Ankur Paul says:

    Wonderful !! Worked in one go !!  🙂

  21. John says:

    Thanks a lot!

  22. Johann says:

    Thank you! I needed this to convert xlsx files to pdf via vbs triggered by a php script.

    Here, take some awesome karma!