PowerShell 4.0和Excel 2013 | Bug | 工作一圆

我遇到了新的Powershell(4.0)和Excel 2013的麻烦。我知道这是(或者)是一个常见的错误,我试图解决它。 但是我失败了。 我可以打开一个Excel文件,并没有任何问题的工作。 但似乎我无法closures或保存工作簿。

其他信息:我在欧洲 – 在奥地利。

其他信息2:这个脚本在Powershell 3.0和Excel 2010中运行良好。

已知的错误,不应在Excel 2013中: http : //support.microsoft.com/kb/320369/en-us

为什么我说这个bug? 因为我的脚本甚至不会在没有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 

ErrorCode的:

 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 

提前致谢!

如果需要更多的信息来帮助我解决这个问题。 随意问。 我真的很感激任何帮助。

我终于能够解决我的问题了。 这不是很好,我不知道为什么我的上述没有工作:

 $newci = [System.Globalization.CultureInfo]"en-US" [system.threading.Thread]::CurrentThread.CurrentCulture = $newci [System.Threading.Thread]::CurrentThread.CurrentUICulture = $newci 

但是,我可以得到保存function再次工作:

 Function Using-Culture ( [System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"), [ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}")) { $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture trap { [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture Invoke-Command $script [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } Using-Culture en-US -script { #SCRIPTBLOCK } 

我正在使用PowerShell 4和办公室2013年,我正在使用这些行来保存和closures:

 #Define html code for Excel save to .htm. $xlExcelHTML = 44 ################# #Save final result as an .xlsx file. $b.SaveAs("$PathFileXML") ##################### #Save final result as a .htm file $b.SaveAs("$PathFileHTML",$xlExcelHTML) ################################### #Close and quit Excel. $b.Close() ########## #Make sure excel and outlook is fully closed. gps *Excel* | Stop-Process -force ######################################### 

我在Win7上使用了一段时间的Excel 2013和PoSH 4.0的脚本,今天开始出现了一个类似的错误,声称Activate [在工作表上]不是一个有效的方法。 但是运行

 $sheet | Get-Member $sheet.Activate() # failed here 

表明Activate 有效的成员。 添加睡眠没有任何区别。

对我来说,运行32位版本的PowerShell,可以从开始 – >所有程序 – >附件 – > Windows Powershell – > Windows Powershell(x86)。

今天早上我申请了一批Office 2013更新,也许其中一个破了东西。