Powershell 4.0和Excel 2013 |错误|全面工作 [英] Powershell 4.0 and Excel 2013 | Bug | Work-a-round

查看:62
本文介绍了Powershell 4.0和Excel 2013 |错误|全面工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用新的Powershell(4.0)和Excel 2013时遇到麻烦.我知道这是(或曾经)是一个常见的错误,因此我尝试解决它.但是我失败了.我可以打开一个Excel文件并对其进行处理,而不会出现任何问题.但是看来,我无法关闭或保存工作簿.

I am having troubles working with the new Powershell (4.0) and Excel 2013. I know this is (or was) a common bug and I tried to work around it. But I failed. I can open an Excel-file and work on it without any problems. But it seems, that I can't close or save the workbook.

其他信息:我在欧洲-奥地利.

Additional information: I am in Europe - in Austria.

其他信息2:该脚本与Powershell 3.0和Excel 2010配合得很好.

Additional information2: This Script works very well with Powershell 3.0 and Excel 2010.

已知错误,不应在Excel 2013中出现: http://support.microsoft.com/kb/320369/zh-CN

Known bug, which shouldn't be in Excel 2013: http://support.microsoft.com/kb/320369/en-us

我为什么要陈述这个错误?因为没有CurrentCulture部分,我的脚本甚至在2013年都无法打开excel文件.

Why did I state this bug? Because my Script wouldn't even open the excel file in 2013 without the CurrentCulture part.

代码:

$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

提前谢谢!

如果需要任何其他信息来帮助我解决此问题.有空问.我将非常感谢您的帮助.

If any further information is needed to help me solving this problem. Be free to ask. I would really appreciate any help.

推荐答案

我终于可以解决我的问题.这不是很好,我也不知道为什么我的上述内容不起作用:

I have been able to finally work around my problem. It is not very nice and I have no idea why my above stated did not work:

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

但是,我可以使保存功能再次与此一起工作:

However, I could get the save function to work again with this:

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.0和Excel 2013 |错误|全面工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆