VB.NET Set Built-in Properties
With more collaboration happening across offices, document properties have gained more importance. Word properties can be categorised as a)Custom Property b) Built-in property
Some common built-in properties are
· Title
· Subject
· Author
· Keywords
· Comments
The following code can be used to get the built-in property of the given word document.
Here are the directives necessary for the program
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Imports System.Reflection
System.Reflection looks an odd man here right! Yes, we will be using the Reflection class to retrieve the properties of the Word document
The following variables initialize the Word Application
Dim oWA As Word.Application = New Word.Application
Dim oWD As Word.Document
The code uses System.Type class’s members like InvokeMember, Gettype etc. Type is the root of the System.Reflection functionality and is the primary way to access metadata.
The following code opens a Word document and sets its ‘Subject’ property.
Sub SetBuiltInProp()
Try
Dim oBuiltProps As Object
oWA.Visible = True
oWD = oWA.Documents.Open("C:\ShasurData\DND_DEC_2008.doc")
oBuiltProps = oWD.BuiltInDocumentProperties
Dim TypBuiltProps As Type = oBuiltProps.GetType
Dim sPropertyName As String = "Subject"
Dim oProp As Object = TypBuiltProps.InvokeMember("Item", BindingFlags.Default Or BindingFlags.GetProperty, Nothing, oBuiltProps, New [Object]() {sPropertyName})
Dim sPropValue As String = "VSTO Examples"
Dim TypSubject As Type = oProp.GetType
TypSubject.InvokeMember("Value", BindingFlags.Default Or BindingFlags.SetProperty, Nothing, oProp, New [Object]() {sPropValue})
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
oWD.Save()
oWD.Close()
oWA.Quit()
End Try
End Sub
The GetType method returns a Type object that represents the type of an instance.
Setting other built-in properties will be the same as above.
No comments:
Post a Comment