Unable to write to or read from Worksheet.CustomProperties in a console application

1

In my console application, I am trying to write something to and read it back from the CustomProperties of an Excel worksheet. I have a reference to the Microsoft.Office.Interop.Excel v14 assembly.

At the line that calls the firstWorksheet.CustomProperties.Add method, I get an exception with the HRESULT 0x800A03EC.

Below is the relevant bit of code:

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();
    }
}

And below is the error I receive:

{"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

And if I try to read the information using the code listed below, I get the exception that follows the code listing.

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;
}

Gives me the following error:

enter image description here

Below is the dump of the Exception class object.

{"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

It appears from this answer that the above observed behavior may be attributed to a bug in the interop assemblies.

However, this answer seems to suggest that the poster has been able to run the code successfully.

Are you able to run the code successfully? Have you seen this error and know its fix?

c#
excel
com
ms-office
office-interop
asked on Stack Overflow Sep 11, 2017 by Water Cooler v2

1 Answer

0

It's possible I don't know what you mean about custom properties, but if you are referring to the Properties from the File->Info section in Excel:

enter image description here

Like those that would come from Sharepoint, I access them using the ContentTypeProperties collection of the Workbook object.

Here is an example of how I would access them in the image above:

// Excel.Workbook wb;

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

Your example showed something from the Worksheet object, so again, I may have totally missed the boat.

answered on Stack Overflow Sep 12, 2017 by Hambone

User contributions licensed under CC BY-SA 3.0