none
Access to Word 2007 document custom properties from Visual Basic 2010 Express RRS feed

  • Question

  • Hi,

    I am trying to access the custom document properties of a Word 2007 document from a Visual Basic 2010 Express application.  I have followed the instructions on http://msdn.microsoft.com/en-us/library/dhxe2d75.aspx, How to: Create and Modify Custom Document Properties, but I'm having problems.

    The following is sample code that shows my problem:

     Microsoft.Office.Core

    I have two problems:

    1. If I follow the instructions on the MSDN article absolutely then the CType cast is to type Office.DocumentProperties, but Visual Basic won't accept that - it gives a build error of
    Type 'Office.DocumentProperties' is not defined.

    2. If I change the cast to Microsoft.Office.Core.DocumentProperties then it builds, but I get an exception:

    Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    I am new to Visual Basic, so any help would be gratefully received.

    Many thanks.

     

     

    • Moved by Cindy Meister MVPModerator Thursday, August 11, 2011 5:20 PM not using VSTO technology (From:Visual Studio Tools for Office)
    Thursday, August 11, 2011 1:19 PM

Answers

  • Hi Tim

    OK, Here's the VB.NET code for getting (custom) document properties using GetType.InvokeMember

        Dim wdApp As Word.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
        Dim doc As Word.Document = wdApp.ActiveDocument
        Dim oDoc As Object = doc
        Dim props As Object
        Dim prop As Object
        Dim s As Object
        Dim args() As Object = {"Test"}
        props = doc.GetType.InvokeMember("CustomDocumentProperties", Reflection.BindingFlags.GetProperty, Nothing, oDoc, Nothing)
        prop = props.GetType.InvokeMember("Item", Reflection.BindingFlags.GetProperty, Nothing, props, args)
        s = prop.GetType.InvokeMember("Value", Reflection.BindingFlags.GetProperty, Nothing, prop, Nothing)
        MessageBox.Show(s)
    
    


     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Timarite Monday, August 15, 2011 9:25 AM
    Friday, August 12, 2011 9:52 AM
    Moderator

All replies

  • Hi Timarite

    You aren't using VSTO, but the page to which you refer is for the VSTO technology so the details may not work for other kinds of solutions. Is this a Windows Form?

    Do you have Option Strict On or Off set?

    Note: After you reply I'm going to move this to the Word for Developers forum where it will be more on-topic.


    Cindy Meister, VSTO/Word MVP
    Thursday, August 11, 2011 2:27 PM
    Moderator
  • Hello Cindy,

    Thanks for replying.  I'm confused then - isn't Visual Basic part of Visual Studio?

    Yes, this is a Windows Forms Application.

    Option Strict is on.

    Thanks,

    Tim.

    Thursday, August 11, 2011 3:05 PM
  • Hi Tim

    You're in the Visual Studio TOOLS FOR OFFICE forum - VSTO is an extension technology that's part of Visual Studio Professional (and "higher"). It helps developers extend Office. But none of the documentation can be applied 1:1 to anyone not using VSTO. VSTO solutions run in-process with the Office application as add-ins or as a substitute for "code behind" (macros) in Excel or Word documents.

    Document Properties are actually part of the Office (not the Word, or Excel, or PowerPoint) object model. The Office apps late bind them into their object models. This is no problem for VBA or for VB.NET with Option Strict Off. But as soon as you have Option Strict On you have to work similar to the way C# handles late binding, meaning you need to use GetType().InvokeMember() as described in the KB article

    http://support.microsoft.com/kb/303296

    Since most VB developers use VB in order to take advantage of what Option Strict Off offers, I've never yet found a code sample for this for VB.NET. I'm really busy right now, and it's the end of my day (tired) so if I can avoid typing all this out (and it would have to be tomorrow) I'd be very happy :-) Can you work from the C# code in the article?


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Timarite Thursday, August 11, 2011 6:14 PM
    • Unmarked as answer by Timarite Monday, August 15, 2011 9:23 AM
    Thursday, August 11, 2011 5:20 PM
    Moderator
  • Hello Cindy,

    Thanks for the information.  Two things for me to try - setting Option Strict Off, and the C# type of late binding. 
    I'll certainly give it a go and let you know how I get on.

    Many thanks,

    Tim.

    Thursday, August 11, 2011 6:14 PM
  • Hi Tim

    OK, Here's the VB.NET code for getting (custom) document properties using GetType.InvokeMember

        Dim wdApp As Word.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
        Dim doc As Word.Document = wdApp.ActiveDocument
        Dim oDoc As Object = doc
        Dim props As Object
        Dim prop As Object
        Dim s As Object
        Dim args() As Object = {"Test"}
        props = doc.GetType.InvokeMember("CustomDocumentProperties", Reflection.BindingFlags.GetProperty, Nothing, oDoc, Nothing)
        prop = props.GetType.InvokeMember("Item", Reflection.BindingFlags.GetProperty, Nothing, props, args)
        s = prop.GetType.InvokeMember("Value", Reflection.BindingFlags.GetProperty, Nothing, prop, Nothing)
        MessageBox.Show(s)
    
    


     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Timarite Monday, August 15, 2011 9:25 AM
    Friday, August 12, 2011 9:52 AM
    Moderator
  • Hello Cindy,

    I've used your example code and my application is now working.

    Many thanks for your very helpful support.

    Best Regards

    Tim. 


    Monday, August 15, 2011 9:28 AM