Windows Phone Developers

Wednesday, December 10, 2008

How to get Built-in Property of a Word Document using VB.NET

VB.NET Retrieve Built-in Property / VB.NET Extract Built-in Property

Some differentiable information can be stored in a built-in property, for example, keywords in a Word document. The following VB.NET code uses System.Reflection members to get the Built-in property

The code needs the following directives

Imports Office = Microsoft.Office.Core

Imports Word = Microsoft.Office.Interop.Word

Imports System.Reflection

It also needs the following declarations

Dim oWA As Word.Application = New Word.Application

Dim oWD As Word.Document

The following code that extracts the value stored in ‘Subject’ property:

Sub GetBuiltInProp()

Try

Dim oBuiltProps As Object

oWA.Visible = True

oWD = oWA.Documents.Open("C:\ShasurData\DND_DEC_2008.doc")

oBuiltProps = oWD.BuiltInDocumentProperties

Dim oTypeBuiltProps As Type = oBuiltProps.GetType

Dim sPropertyName As String = "Subject"

Dim oProperty As Object = oTypeBuiltProps.InvokeMember("Item", BindingFlags.Default Or BindingFlags.GetProperty, Nothing, oBuiltProps, New [Object]() {1})

Dim oTypeProp As Type = oProperty.GetType

Dim sPropertyValue As String = oTypeProp.InvokeMember("Value", BindingFlags.Default Or BindingFlags.GetProperty, Nothing, oProperty, New [Object]() {})

Catch ex As Exception

Console.WriteLine(ex.Message)

Finally

oWD.Close(False)

oWA.Quit()

End Try

End Sub

Here we are retrieving the built-in property of the document using

oWD = oWA.Documents.Open("C:\ShasurData\DND_DEC_2008.doc")

oBuiltProps = oWD.BuiltInDocumentProperties

Then using the System.type class’s GetType and InvokeMethod to get to the built-in property

Dim oTypeBuiltProps As Type = oBuiltProps.GetType

Dim sPropertyName As String = "Subject"

Dim oProperty As Object = oTypeBuiltProps.InvokeMember("Item", BindingFlags.Default Or BindingFlags.GetProperty, Nothing, oBuiltProps, New [Object]() {1})

The builtindocument property collection has many Items. Hence we get the Item first through InvokeMember and then use the object to retrieve the value of that particular item

Dim oTypeProp As Type = oProperty.GetType

Dim sPropertyValue As String = oTypeProp.InvokeMember("Value", BindingFlags.Default Or BindingFlags.GetProperty, Nothing, oProperty, New [Object]() {})

The above method overcomes the "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))." error

Please feel free to modify and suggest improvements

See also How to Insert Document Properties in Word Document

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment:

  1. work ONLY with office SP1 OR greater!

    ReplyDelete