以下代码在Powershell V1(Excel 2007)中引发异常:
$E = New-Object -COM "Excel.Application" $E.Visible = $True $wb = $E.Workbooks.Add() #<<错误表示格式可能是旧的或类型库无效(从西班牙语翻译).Word的类似脚本运行得很好.
1> 小智..:当目前的文化不是en-US时,Office互操作程序集似乎有这个问题.显而易见的解决方法是设置文化.
在交互式控制台上将整个事件作为单个命令运行非常重要,因为PowerShell V1始终为每个命令调用创建一个新线程.
PS C:\Users\jachymko> $e = new-object -com excel.application PS C:\Users\jachymko> $e.workbooks.add() Exception calling "Add" with "0" argument(s): "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))" At line:1 char:17 + $e.workbooks.add <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation PS C:\Users\jachymko> & { >> [threading.thread]::CurrentThread.CurrentCulture = 'en-US' >> $e = new-object -com excel.application >> $e.workbooks.add() >> $e.visible=1 >> } >>