我正在使用PowerShell解锁锁定的Excel电子表格。当我从作业运行脚本(使用我自己作为代理用户)时,脚本的Excel部分不会运行。但是,当我直接从命令行执行相同的脚本时,它可以正常工作。我的代码是:
#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()
编辑:我添加了日志记录。以下是日志结果:
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.
答案 0 :(得分:5)
确定!原来解决方案如下(如果它出现在搜索结果中):
C:\Temp
C:\Windows\SysWOW64\config\systemprofile\Desktop
这应该解决问题。
答案 1 :(得分:1)
对于Windows 2012 x64,我还必须在它工作之前创建下面的文件夹。
C:\的Windows \ system32 \设置\ systemprofile \桌面
答案 2 :(得分:1)
对我来说,解决方案包括创建这两个文件夹:
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop
答案 3 :(得分:0)
要解决此问题: