【问题标题】:Powershell 4.0 and Excel 2013 | Bug | Work-a-roundPowershell 4.0 和 Excel 2013 |错误 |周而复始
【发布时间】: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


    【解决方案1】:

    我终于能够解决我的问题。这不是很好,我不知道为什么我上面所说的不起作用:

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

    但是,我可以让保存功能再次使用:

    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
    }
    

    【讨论】:

      【解决方案2】:

      我正在使用 Powershell 4 和 office 2013,我正在使用这些行来保存和关闭:

      #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
      #########################################
      

      【讨论】:

        【解决方案3】:

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

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

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

        对我有用的是运行 PowerShell 的 32 位版本,可从开始 --> 所有程序 --> 附件 --> Windows Powershell --> Windows Powershell (x86) 获得。

        我今天早上应用了大量的 Office 2013 更新,其中一个可能破坏了一些东西。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-16
          • 1970-01-01
          • 2016-07-06
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多