2017-09-11 18 views
1

私のコンソールアプリケーションでは、何かを書き込んでExcelワークシートのCustomPropertiesから読み込もうとしています。 Microsoft.Office.Interop.Excel v14アセンブリへの参照があります。コンソールアプリケーションのWorksheet.CustomPropertiesへの書き込みや読み込みができません

firstWorksheet.CustomProperties.Addメソッドを呼び出す行では、HRESULT 0x800A03ECで例外が発生します。

以下

コードの該当ビットです:

static void WriteToExcelCustomDocumentProperties(
    string excelFile, 
    string outputFolder, 
    string propertyName, 
    object propertyValue) 
{ 
    excel::Application excel = null; 
    Workbook workbook = null; 
    Worksheet firstWorksheet = null; 

    try 
    { 
     excel = new excel::Application(); 

     workbook = excel.Workbooks.Open(excelFile); 

     firstWorksheet = workbook.Worksheets[1] as Worksheet; 

     firstWorksheet.CustomProperties.Add(propertyName, propertyValue); 

     var outputFilePath = GetOutputFilePath(excelFile, outputFolder); 

     workbook.SaveAs(outputFilePath); 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine("\nERROR:"); 
     Console.WriteLine($"{excelFile}!{firstWorksheet.Name}"); 
     Console.WriteLine($"{ex.Message}\n"); 
    } 
    finally 
    { 
     if (workbook != null) 
      workbook.Close(); 

     if (excel != null) 
      excel.Quit(); 
    } 
} 

そして以下は、私が受け取るエラーです:

{"Exception from HRESULT: 0x800A03EC"} 
    Data: {System.Collections.ListDictionaryInternal} 
    ErrorCode: -2146827284 
    HResult: -2146827284 
    HelpLink: null 
    IPForWatsonBuckets: 0x7177fe49 
    InnerException: null 
    IsTransient: false 
    Message: "Exception from HRESULT: 0x800A03EC" 
    RemoteStackTrace: null 
    Source: "" 
    StackTrace: " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.CustomProperties.Add(String Name, Object Value)\r\n at CustomDocumentProperties.Program.WriteToExcelCustomDocumentProperties(String excelFile, String outputFolder, String propertyName, Object propertyValue) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 61" 
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    WatsonBuckets: null 
    _HResult: -2146827284 
    _className: null 
    _data: {System.Collections.ListDictionaryInternal} 
    _dynamicMethods: null 
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    _exceptionMethodString: null 
    _helpURL: null 
    _innerException: null 
    _ipForWatsonBuckets: 0x7177fe49 
    _message: "Exception from HRESULT: 0x800A03EC" 
    _remoteStackIndex: 0 
    _remoteStackTraceString: null 
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager} 
    _source: "" 
    _stackTrace: {sbyte[96]} 
    _stackTraceString: null 
    _watsonBuckets: null 
    _xcode: -532462766 
    _xptrs: 0x00000000 

そして、私は、下記のコードを使用して情報を読み取るしようとした場合、私が取得コードリストに続く例外。

static object ReadFromExcelCustomDocumentProperties(
    string excelFile, 
    string propertyName) 
{ 
    excel::Application excel = null; 
    Workbook workbook = null; 
    Worksheet firstWorksheet = null; 
    object value = null; 

    try 
    { 
     excel = new excel::Application(); 

     workbook = excel.Workbooks.Open(excelFile); 

     firstWorksheet = workbook.Worksheets[1] as Worksheet; 

     value = firstWorksheet.CustomProperties[(object)propertyName].Value; 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine($"\nERROR in {nameof(ReadFromExcelCustomDocumentProperties)}:"); 
     Console.WriteLine($"{excelFile}!{firstWorksheet.Name}"); 
     Console.WriteLine($"{ex.Message}\n"); 
    } 
    finally 
    { 
     if (workbook != null) 
      workbook.Close(); 

     if (excel != null) 
      excel.Quit(); 
    } 

    return value; 
} 

は私に次のエラーを与える:

enter image description here

以下はExceptionクラスオブジェクトのダンプです。

{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} 
    Data: {System.Collections.ListDictionaryInternal} 
    ErrorCode: -2147352571 
    HResult: -2147352571 
    HelpLink: null 
    IPForWatsonBuckets: 0x7177fe49 
    InnerException: null 
    IsTransient: false 
    Message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))" 
    RemoteStackTrace: null 
    Source: "" 
    StackTrace: " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.CustomProperties.get__Default(Object Index)\r\n at CustomDocumentProperties.Program.ReadFromExcelCustomDocumentProperties(String excelFile, String propertyName) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 131" 
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    WatsonBuckets: null 
    _HResult: -2147352571 
    _className: null 
    _data: {System.Collections.ListDictionaryInternal} 
    _dynamicMethods: null 
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    _exceptionMethodString: null 
    _helpURL: null 
    _innerException: null 
    _ipForWatsonBuckets: 0x7177fe49 
    _message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))" 
    _remoteStackIndex: 0 
    _remoteStackTraceString: null 
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager} 
    _source: "" 
    _stackTrace: {sbyte[96]} 
    _stackTraceString: null 
    _watsonBuckets: null 
    _xcode: -532462766 
    _xptrs: 0x00000000 

上記の観察された行動は、相互運用機能アセンブリのバグに起因することができることをthis answerから表示されます。

しかし、this answerは、ポスターがコードを正常に実行できることを示唆しているようです。

コードを正常に実行できますか?あなたはこのエラーを見て、修正を知っていますか?

+0

与えられた 'Name'プロパティで' CustomProperty'が既に存在しない場合は、 'CustomProperties.Add'を呼び出すことができます。 'Name'による検索は、Excel VBA環境からでも' WorkSheet.CustomProperties'のために働いたことはありません。これは、CustomPropertiesのワークシート実装による不具合です。 SmartTagからCustomPropertiesコレクションを取得した場合、そのコレクションを名前でインデックスできます。リンクした答えは、列挙ループを使用して名前でルックアップを行う方法を示しています。あなたはそのテクニックを試しましたか? – TnTinMn

答えて

0

それは私はあなたがカスタムプロパティについて何を意味するのか分からないことも可能ですが、Excelで[ファイル] - > [情報]セクションから[プロパティ]を参照している場合:それらのような

enter image description here

ことを来りますSharepointから、WorkbookオブジェクトのContentTypePropertiesコレクションを使用してアクセスします。 Worksheetオブジェクトから何かを示したので、再び、私は完全にボートを見逃しているかもしれ

// Excel.Workbook wb; 

string dmdRegion = wb.ContentTypeProperties["Demand Region"].Value.ToString(); 
wb.ContentTypeProperties["Demand Region"].Value = "EMEA"; 

あなたの例:ここでは

は、私は上記の画像でそれらにアクセスする方法の例です。

関連する問題