Excel 2007 automation exception using Powershell when calling Workbooks.Add ()

The following code throws an exception in Powershell V1 (Excel 2007):

$E = New-Object -COM "Excel.Application"
$E.Visible = $True
$wb = $E.Workbooks.Add() #<<<Exception here

The error indicates that the format may be old or that the type library is invalid (Spanish translation). A similar script for Word works fine.

+5
source share
3 answers

Adapted to Powershell from one of the solutions proposed in Help and Support for MS Article 320369 .

$ci = new-object system.globalization.cultureinfo "en-US"

$e = New-Object -COM "Excel.Application"
$e.Visible = $True
$e.UserControl= $True
$books = $e.Workbooks
$books.PSBase.GetType().InvokeMember( `
       "Add", `
       [system.reflection.bindingflags]::InvokeMethod, `
       $null, $books, $null, $ci)

From the same article:

, Office, Excel Excel , .

+3

, , . .

, , 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
>> }
>>
+9

. :

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
+2

All Articles