2016-02-17 12 views
8

मैं एक्सेल powershell स्क्रिप्ट से CorruptLoad paramterer साथ फ़ाइल खोलने की आवश्यकता में उत्कृष्टता। लेकिन जब मैं इसे बनाने की कोशिश करता हूं, तो मुझे Exception calling "Open" with "15" argument(s): "open method workbooks class failed" त्रुटि मिलती है। यह त्रुटि केवलतब होता है जब मैं सभी 15 तर्क के साथ Open कहते हैं। और जब मैं 1512 तर्कों के साथ 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

देखते हैं कि आप 'CorruptLoad' कारण है कि आप यह तर्क सूची में निर्दिष्ट नहीं किया है उपयोग करने के लिए की जरूरत है? आपके पास यह '$ गायब' है? https://msdn.microsoft.com/en-us/library/office/ff193596.aspx। – Matt

+0

@ मैट, मैंने 'ओपन' फ़ंक्शन को कॉल करने के सभी प्रकारों को आजमाया। मैंने इसे दिखाने के लिए अपना कोड नमूना बदल दिया। – Indian

+0

स्पष्टीकरण के लिए धन्यवाद। – Matt

उत्तर

7

ज्यादा अपने PowerShell स्क्रिप्ट के साथ चारों ओर खेलने के बाद ... यह सब बहुत अजीब है।

अवलोकन किया गया व्यवहार

सबसे पहले, कार्यपुस्तिकाएं पर ओपन विधि आपत्ति केवल 14 मापदंडों की रिपोर्ट जब $excel.Workbooks.Open.Invoke.ToString() चलाया जाता है। ouptut पढ़ता है:

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

हालांकि, वहाँ 15 मापदंडों के लिए अधिभार विधि किसी तरह का है, क्योंकि जब अधिक वर्बोज़ त्रुटि का उपयोग कर रीड-आउट $Error[0]|format-list -force मैंने किया दो परीक्षण कॉल, 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 में एक्सेल COM इंटरॉप समर्थन के साथ एक समस्या है। $missing के रूप में 15 वें पैरामीटर की आपूर्ति करने से Workbooks.Open() reference के अनुसार व्यवहार में बदलाव नहीं करना चाहिए। पावरहेल के एक्सेल COM समर्थन के साथ कोई समस्या होने पर और मजबूती मिलती है क्योंकि, जब वीबीए स्क्रिप्ट मैक्रो के रूप में चलती है, तो सबकुछ दस्तावेज़ीकरण के अनुसार काम करता है।

अगले चरण

सुझाए गए पहले काम के आसपास जो मन में आता मैक्रो के रूप में VBA स्क्रिप्ट लिखने और कहा कि कमांड लाइन से VBA स्क्रिप्ट को चलाने के लिए विशेष रूप से प्रयोग किया जाता है excel फ़ाइल में संग्रहीत करना है। इसे लागू करने के लिए न्यूनतम प्रयास करना होगा और यह ओपी और खुद दोनों से परीक्षण करके जाना जाता है कि यह काम करेगा।

आप powershell से मैक्रो ट्रिगर किसी भी कठिनाई है, तो Calling Excel macros from PowerShell with arguments

+0

पीटर, मैक्रोज़ के साथ किसी अन्य फ़ाइल से विचार के लिए धन्यवाद! और एक्सेल COM घटक के साथ intresting स्थिति हैं: 'Office 2010' SP2 के साथ किसी अन्य सर्वर पर, powerhell 2.0 और' .NET Framework 4.0' ऐसी समस्या नहीं होती है। – Indian

+0

@ इंडियन मेरे पास कोई तुलनीय वातावरण नहीं है इसलिए मैं इसकी पुष्टि नहीं कर सकता। हमारे पास एक बग का काफी सबूत है; इसलिए, यदि आप चाहें, तो आप इसे यहां माइक्रोसॉफ्ट में जमा कर सकते हैं: https://connect.microsoft.com/PowerShell मेरे उत्तर में दी गई जानकारी को बग रिपोर्ट के लिए एक अच्छा आधार प्रदान करना चाहिए। – Marvin

+0

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

संबंधित मुद्दे