Bloqueada early binding and BuiltinDocumentProperties

  • Friday, May 04, 2012 5:31 PM
     
     

    I have Option Strict turned on which does not allow late binding.

    Reading http://support.microsoft.com/kb/303296 , it says -  Note The DocumentProperties and the DocumentProperty interfaces are late bound interfaces. To use these interfaces, you must treat them like you would an IDispatch interface.

    What does that mean?

    How can I use early binding?

All Replies

  • Friday, May 04, 2012 8:45 PM
     
     
    Temporarily set Option Strict Off.  Run with breakpoints on the statements in question.  Use Intellisense to find out what the late bound type is.  Do the same and set Option Strict On.
  • Friday, May 04, 2012 8:54 PM
     
      Has Code

    I did as you said, and the BuiltinDocumentProperties is showing as System.__ComObject

    The following is my entire code.

           Dim Wrd As New Microsoft.Office.Interop.Word.Application
            Dim DocName As Object = "C:\TEMP\test.doc"
            Wrd.Application.Visible = False
            Dim wrdDocument As Microsoft.Office.Interop.Word.Document = Wrd.Documents.Open(DocName)
    
            Debug.Print(wrdDocument.BuiltInDocumentProperties("author").value.ToString)
            wrdDocument.Close()
            Wrd.Application.Quit()
            wrdDocument = Nothing
            Wrd = Nothing
     

    BuiltinDocumentProperties is an interface

    I tried

            Dim properties As Microsoft.Office.Core.DocumentProperties = DirectCast(wrdDocument.BuiltInDocumentProperties, Microsoft.Office.Core.DocumentProperties)
    
            Dim prop As Microsoft.Office.Core.DocumentProperty
            prop = properties.Item("Author")
    
            Debug.Print(prop.Value.ToString)

    per  http://msdn.microsoft.com/en-us/library/4e0tda25%28v=vs.100%29.aspx

    but I am getting a run-time error at the DirectCast saying that Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties



    • Edited by ieee488 Friday, May 04, 2012 9:19 PM
    •  
  • Friday, May 04, 2012 10:23 PM
     
     Answered

    I think that it means you have to declare variables as object not specific types to hold e.g. BuiltinDocumentProperties. You can the create variables of the specific type by calling GetType on the objects. That, IIRC, is how things were done with IDispatch - been along time and not did a great deal with COM.

    If you push the code in the article through a C# to VB translator you will see how MS do it. It seems alot tod in this day and age, so someone with more Office Interop experience may have a much neater solution. I don't have Office and the last 'interop' stuff I did was in VB6.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    • Marked As Answer by ieee488 Saturday, May 05, 2012 2:28 AM
    •  
  • Saturday, May 05, 2012 2:27 AM
     
     Answered Has Code

    That did the trick.

    I converted the C# code over to VB and it worked. What a pain!

    Dim Wrd As New Microsoft.Office.Interop.Word.Application
    Dim DocName As Object = "C:\TEMP\test.doc"
    Wrd.Application.Visible = False
    Dim wrdDocument As Microsoft.Office.Interop.Word.Document = Wrd.Documents.Open(DocName)
    
    Dim properties As Object = wrdDocument.BuiltInDocumentProperties
    Dim typeProperties As Type = properties.GetType
    Dim oNameProp As Object = typeProperties.InvokeMember("Item",
    							  BindingFlags.Default Or BindingFlags.GetProperty,
    							  Nothing, properties,
    							  New Object() {"Title"})
    
    Dim typeNameProp As Type = oNameProp.GetType()
    
    Dim strValue As String = typeNameProp.InvokeMember("Value",
    							  BindingFlags.Default Or BindingFlags.GetProperty,
    							  Nothing, oNameProp,
    							  New Object() {}).ToString()
    
    Debug.Print(strValue)
    
    wrdDocument.Close()
    Wrd.Application.Quit()
    wrdDocument = Nothing
    Wrd = Nothing

    • Marked As Answer by ieee488 Saturday, May 05, 2012 2:29 AM
    •