0

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;
nam
  • 21,967
  • 37
  • 158
  • 332
  • what changed? is it running on a different machine? did your office version change? – MikeJ Jun 09 '20 at 20:19
  • @MikeJ Since the above code has been working on my various `VSTO` projects since `2010`, obviously the machines (XP, VISTA, Win10, etc.), the Visual Studio versions (2010,2012,2013,2015,2019) along with Office versions 92003/2007/2010/2016/O365 etc.) have changed, as well. And the same line of code worked on all those versions before. It seems from this [post](https://stackoverflow.com/a/32232245/1232087), the above code of line may not always be reliable. – nam Jun 10 '20 at 20:29
  • sounds like you found the answer below but just out of curiosity have you tried to cast oActiveDoc.BuiltInDocumentProperties as Microsoft.Office.Core.DocumentProperties? does that fail? it sounds like the old PIAs you've originally built the project with no longer work with one of the newer versions of office. Late binding could be the safest route but I'm curious if .net detects the types are the same. – MikeJ Jun 11 '20 at 15:03

3 Answers3

1

There are several ways to bridge the gap with exceptions you've got so far:

  1. Use dynamic and object references. Read more about this approach on the Read BuiltInDocumentProperties/CustomDocumentProperties alway null with Word 2010? page.

  2. Use the late-binding technology represented in .net framework by the Type.InvokeMember method.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • The first link you provided may be a more permanent solution (my upvote) although it's a bit more involved. Thank you for sharing that link. – nam Jun 10 '20 at 16:29
0

Try

string sTitle = oActiveDoc.BuiltInDocumentProperties.Item("Title").Value;
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • When I tried your suggestion, I got the following error: `object' does not contain a definition for 'Item' and no accessible extension method 'Item' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?` – nam Jun 09 '20 at 17:59
  • I'm wondering if the issue has something to the fact that `BuiltInDocumentProperties` is of type `dynamic`. Although for several years I have used `string sTitle = oActiveDoc.BuiltInDocumentProperties["Title"].Value;` on my many `VSTO` project using from `VS2010` to the latest Visual Studio but never had that issue. – nam Jun 09 '20 at 18:06
  • Yes, not use dynamic - Item function is there, but .Net hides property indexers. – Dmitry Streblechenko Jun 09 '20 at 23:20
0

could you try that:

         string sTitle;
         dynamic properties = oActiveDoc.BuiltInDocumentProperties;
         var property = properties["Title"];    
         if (property != null)
          {
              sTitle = property.Value.ToString();
          }
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • Your solution worked (thank you). I'm not sure though if it would work in all situations. But it does for now in my case. But then `string sTitle = oActiveDoc.BuiltInDocumentProperties["Title"].Value;` was also working before and not anymore. So, let me keep my fingers crossed. – nam Jun 10 '20 at 16:25