Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel.Application not working in PowerShell using SQL Job, but works from command line

I am using PowerShell to unlock a locked Excel spreadsheet. When I run the script from a job (using myself as the Proxy user), the Excel portion of the script does not run. When I execute the same script directly from the command line, however, it works fine. My code is:

#Unlock Attatchment 
$x = New-Object -comObject Excel.Application
$x.visible = $false
$x.DisplayAlerts = $False
$workbook = $x.Workbooks.Open($spreadsheetFileName,1,$false,
                              5,$spreadsheetPassword,$spreadsheetPassword)
$workbook.SaveAs($tmpFileName,1,$null,$null,$null,$null,$null,
                $x.XlSaveConflictResolution.xlLocalSessionChanges,
                $null,$null,$null,$null)
$x.Workbooks.Close()
$x.Quit()

Edit: I added logging. Here are the log results:

Exception calling "Open" with "6" argument(s): Microsoft Office Excel cannot access the file "_____"
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.
like image 927
nosirrahcd Avatar asked Jan 20 '23 02:01

nosirrahcd


2 Answers

Ok! Turns out the solution is as follows (in case this shows up in a search result):

  1. Add Directory: C:\Temp
  2. Add Directory: C:\Windows\SysWOW64\config\systemprofile\Desktop

That should fix the problem.

like image 178
nosirrahcd Avatar answered Jan 25 '23 22:01

nosirrahcd


For Windows 2012 x64 I also had to create the folder below before it worked.

C:\Windows\System32\config\systemprofile\Desktop

like image 30
Ben McLean Avatar answered Jan 25 '23 23:01

Ben McLean