2016-02-17 27 views
8

powershell-scriptからCorruptLoadパラメーターでexcelファイルを開く必要があります。しかし、私がそれをしようとすると、私はエラーException calling "Open" with "15" argument(s): "open method workbooks class failed"を取得します。このエラーはとなり、私はOpenと15個の引数をすべて呼び出します。そして、私が15の引数を持つVB.netプログラムで同じエクセルファイルを開こうとすると、または名前付き引数の値を指定してCorruptLoad、問題はありません!powershellでエラーを開くときにエラーが発生する

私はpowershell v 4.0、Office 2010 SP2および.NET Framework 4.5.2を使用しています。エラーが発生した理由を、私は考えていない

$excel = New-Object -ComObject Excel.Application 
$excel.Visible = $false 
try 
{ 
    $missing = [System.Type]::Missing 
# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing, 
#        $missing, $missing, $missing, $missing, $missing, 
#        $missing, $missing, $missing, $missing, $missing) 

# $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing, 
#        $missing, $missing, $missing, $missing, $missing, 
#        $missing, $missing, $missing, $missing, 1) 

    $XlCorruptLoad = "Microsoft.Office.Interop.Excel.XlCorruptLoad" -as [type] 

    $wb = $excel.Workbooks.Open("d:\temp\start_instrument.xls", $missing, $missing, $missing, $missing, 
           $missing, $missing, $missing, $missing, $missing, 
           $missing, $missing, $missing, $missing, $XlCorruptLoad::xlRepairFile)  
} 
catch 
{ 
    Write $Error[0].ToString() 
} 

# some stuff 

if ($excel -ne $null) 
{ 
    $excel.Quit() 

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null 
    $excel = $null 
} 

[System.GC]::Collect() | Out-Null 
[System.GC]::WaitForPendingFinalizers() | Out-Null 

は、ここに私のpowershellコードです。私はどんなアドバイスや前提にもうれしく思います!

+0

を参照してください?あなたは '$ missing'としてそれを持っていますか? https://msdn.microsoft.com/en-us/library/office/ff193596.aspx。 – Matt

+0

@Matt、私は 'Open'関数を呼び出すすべての変種を試しました。コードサンプルを変更して表示しました。 – Indian

+0

清算していただきありがとうございます。 – Matt

答えて

7

PowerShellスクリプトで遊んだあと、それは非常に奇妙です。 $excel.Workbooks.Open.Invoke.ToString()が実行されたときに

はまず、ワークブックのOpenメソッドは、オブジェクト

観察された行動は、唯一の14個のパラメータを報告します。 ouptutは読む:しかし

Workbook Open (string, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant, Variant) 

、15個のパラメータの過負荷方法のいくつかの種類があるため、読み出し$Error[0]|format-list -force私は2つのテスト・コール、15個のパラメータを持つ最初とを有する第二の、より詳細なエラーを使用した場合16.

15パラメータ

Exception    : System.Runtime.InteropServices.COMException (0x800A03EC): Unable to get the Open property of the Workbooks class 
          at System.Management.Automation.Interpreter.MethodInfoCallInstruction.InvokeInstance(Object instance, Object[] args) 
          at System.Management.Automation.Interpreter.DynamicInstructionN.Run(InterpretedFrame frame) 
          at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 

16パラメータ

Exception    : System.Management.Automation.MethodException: Cannot find an overload for "Open" and the argument count: "16" ---> 
         System.Reflection.TargetParameterCountException: Cannot find an overload for "Open" and the argument count: "16" 
          --- End of inner exception stack trace --- 
          at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception 
         exception) 
          at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) 
          at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 
          at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 

上記のように、メソッドは15個のパラメータをサポートしますが、16個はサポートしません。ただし、15番目のパラメータにどの値を指定しても、ファイルを開くことができません。

レコードの場合、それは14以下の引数で動作しますが、例外をスローするのは15番目の引数だけです。私が見てきたすべてのものから

結論

、私は唯一のPowerShellでExcelのCOM相互運用機能のサポートに問題があると結論付けることができます。 15番目のパラメータを$missingとすると、単にWorkbooks.Open() referenceに従って動作を変更すべきではありません。 PowershellのExcelには問題があります。COMサポートは、VBAスクリプトをマクロとして実行すると、ドキュメントごとにすべて動作するため、さらに強化されています。

は、次のステップ

を推奨最初の仕事の周りにそれが頭に浮かぶマクロとしてVBAスクリプトを作成し、コマンドラインからVBAスクリプトを実行するために特別に使用されているExcelファイルに保管することです。それは実装するのに最小限の労力しかかからず、OPと自分の両方からテストすることで動作することが知られています。

あなたはどの難易度のPowerShellからマクロをトリガを持っている場合は、あなたが引数リストでそれを指定していない理由を `CorruptLoad`を使用する必要がある場合は、Calling Excel macros from PowerShell with arguments

+0

Peter、別のファイルのマクロを使っていただきありがとうございます!そして、Excel COMコンポーネントには、「Office 2010 SP2」、「powershell 2.0」、「.NET Framework 4.0」という別のサーバーでは、このような問題は発生しないという状況があります。 – Indian

+0

@Indian私はこれに匹敵する環境がないので、私はこれを確認できません。私たちはかなりのバグの証拠を持っています。 https://connect.microsoft.com/PowerShell私の答えの情報は、バグ報告のための良い基礎を提供する必要があります。 – Marvin

+0

https://windowsserver.uservoice.com/forums/301869-powershell –

関連する問題