Accessing word CustomDocumentProperties from VB.NET
-
Hi. I have a VB.NET program that is creating Word documents based on an existing template file by filling in data retrieved from a database. Some of the content in the Word document is maintained in custom properties, however, although it is very easy to manage these from with a Word macro, I am having difficulty getting hold of the CustomDocumentProperties collection from within VB.NET. Originally, I tried having a variable of type Word.CustomProperty which I would use, in conjunction with the CustomDocumentProperties collection, but had absolutely no joy. Am I being stupid and missing something very obvious. The problem seems to be that for some reason, CustomDocumentProperties is of type System.__COMObject and does not have any discernable methods or properties. For now, I have created a single line word macro (as part of the original template) that simply sets a custom property to the value you desire, but this is far from ideal, and really goes against the grain for me!! Unfortunately, this is not a solution that can be entirely coded in Word as it is essentially a very small part of a bigger solution (the word doc is just something that is produced at the same time as other things) Does anyone have any experience with CustomDocumentProperties from VB.NET when talking to Office 2003. I prefer to use Option Strict On, but if it is necessary to turn it off, I will! A code snippet from .NET that I am using (simplyfied) is shown below. Any help with this would be greatly appreciated :-D
Imports Word = Microsoft.Office.Interop.Word .. .. .. Dim oWord As Word.ApplicationClass Dim docCurrent As Word.Document ' ' Start MS Word and open the document. ' oWord = CType(CreateObject("Word.Application"), Word.ApplicationClass) docCurrent = oWord.Documents.Open(FileName:="C:\MyOriginalDoc.doc", ReadOnly:=True) ' ' Since it seems impossible to get to the CustomDocumentProperties collection with the Word ' object Model through VB.NET, we run a little macro that is embedded in the template to ' populate the custom properties. Not nice, but apparently necessary - although I won't be ' defeated, so whatch this space! ' oWord.Run("SetDocProperty", "PhaseDescription", strPhaseDesc) ' ' And now we update the fields ' docCurrent.Fields.Update() oWord.Options.PrintFieldCodes = False d