【发布时间】:2016-02-29 03:17:57
【问题描述】:
我在使用新的 Powershell (4.0) 和 Excel 2013 时遇到问题。 我知道这是(或曾经是)一个常见的错误,我试图解决它。但我失败了。我可以打开一个 Excel 文件并毫无问题地处理它。但似乎我无法关闭或保存工作簿。
附加信息:我在欧洲 - 在奥地利。
附加信息2:此脚本非常适用于 Powershell 3.0 和 Excel 2010。
已知错误,不应出现在 Excel 2013 中:http://support.microsoft.com/kb/320369/en-us
我为什么要陈述这个错误?因为如果没有 CurrentCulture 部分,我的脚本甚至不会在 2013 年打开 excel 文件。
代码:
$Path = "C:\Script\test.xls"
#Excelvar:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$newci = [System.Globalization.CultureInfo]"en-US"
[system.threading.Thread]::CurrentThread.CurrentCulture = $newci
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $newci
#Sheets:
$Sheet1 = "1"
$Sheet2 = "2"
$Sheet3 = "3"
$Sheet4 = "4"
$Workbook = $Excel.Workbooks.open($Path)
#Sheets:
$Sheet1 = $Workbook.Worksheets.Item($Sheet1)
$Sheet2 = $Workbook.Worksheets.Item($Sheet2)
$Sheet3 = $Workbook.Worksheets.Item($Sheet3)
$Sheet4 = $Workbook.Worksheets.Item($Sheet4)
#some reading and writing in the sheets
##############################
#Saving and closing:
$Workbook.SaveAs($Path)
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) > $null
错误代码:
Method invocation failed because [System.__ComObject] does not contain a method named 'SaveAs'.
At C:\Script\SCRIPT.ps1:620 char:5
+ $Workbook.SaveAs($Path)
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.__ComObject] does not contain a method named 'Close'.
At C:\Script\SCRIPT.ps1:621 char:5
+ $Workbook.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
提前致谢!
如果需要任何进一步的信息来帮助我解决这个问题。随意问。非常感谢任何帮助。
【问题讨论】:
标签: excel powershell