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.

https://support.microsoft.com/kb/257757

· 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

 m_oExcel.Quit()

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

            Message

            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

Resolution

************

· 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

  • Anonymous
    June 04, 2010
    Seems also to apply to running vbs scripts from Task Scheduler. Thanks for the tip.

  • Anonymous
    June 04, 2010
    Seems also to apply to running vbs scripts from Task Scheduler. Thanks for the tip.

  • Anonymous
    October 07, 2010
    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.

  • Anonymous
    January 26, 2012
    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.

  • Anonymous
    June 19, 2012
    Thanks Bharat for this article ,Regards Nitin Sarwahi

  • Anonymous
    November 16, 2012
    Thanks for the tip. It works very well.

  • Anonymous
    January 31, 2013
    same issue with apache/php from system() function. same resolution also

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

  • Anonymous
    April 09, 2013
    Excellent ... thanks a lot

  • Anonymous
    May 07, 2013
    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!

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

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

  • Anonymous
    October 24, 2013
    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!

  • Anonymous
    January 01, 2014
    WOW... thanks for the help. It works now!

  • Anonymous
    January 07, 2014
    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!

  • Anonymous
    January 23, 2014
    Thanks heaps for posting!

  • Anonymous
    April 14, 2014
    Good solution. It work very well. Thanks a lot...!

  • Anonymous
    April 19, 2014
    The comment has been removed

  • Anonymous
    June 03, 2014
    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.

  • Anonymous
    October 05, 2014
    Wonderful !! Worked in one go !!  :)

  • Anonymous
    October 06, 2014
    Thanks a lot!

  • Anonymous
    November 06, 2014
    Thank you! I needed this to convert xlsx files to pdf via vbs triggered by a php script. Here, take some awesome karma!

  • Anonymous
    April 05, 2016
    THANK YOU!!! THANK YOU!!! THANK YOU!!!Just adding a simple folder "Desktop" to C:\Windows\SysWOW64\config\systemprofile worked for us after server updates stopped our previously working task.

  • Anonymous
    July 22, 2016
    Still saving our lives in 2016, Thanks(vbscript, wsf, cscript, task scheduler, excel)