Search code examples
c#vstooffice-interop

Microsoft Office Interop: Cannot apply indexing with [] to an expression of type 'object'


Following code of my Microsoft Office WORD VSTO Add-in, was successfully getting the document title before. But now, it's throwing the error shown below:

string sTitle = oActiveDoc.BuiltInDocumentProperties["Title"].Value;

Error:

Cannot apply indexing with [] to an expression of type 'object'

Based on some similar online solutions (such as this and this) to the issue I tried the following code but I'm still getting the exact same error.

Question: What I may be missing here and how can it be resolved?

Ref: Document.BuiltInDocumentProperties gets a Microsoft.Office.Core.DocumentProperties collection that represents all the built-in document properties for the document.

Following also gives the exact same error:

string sTitle = oActiveDoc.BuiltInDocumentProperties["Title"].Value as string;

or

string sTitle = oActiveDoc.BuiltInDocumentProperties["Title"] as string;

Solution

  • could you try that:

             string sTitle;
             dynamic properties = oActiveDoc.BuiltInDocumentProperties;
             var property = properties["Title"];    
             if (property != null)
              {
                  sTitle = property.Value.ToString();
              }