Hi Diane
The name of this collection and object in the Word object model is confusing, no question :-) Perhaps the Word developer team would have chosen something else, some twenty years ago, if they'd realized how programming languages were going to evolve...
No, the VSTO tool doesn't automatically pick up document VARIABLE objects (although it's definitely a fascinating thought). Think of a docVar (I'll use that term to try to help distinguish these from variables you use in your code) as a container in a document file, similar to a bookmark or a document property (as in File/Properties). It's just one more thing in the document binary file; one more thing Word provides.
So, in order to use it in any code (whether VSTO, VBA or something else), you first have to address the object (the docVar) for it to be recognized. Then you can access its content (the Value property). To get the content of a bookmark (just to continue the example), you do something like this
Dim bkm as Word.Bookmark Dim fName as String fName = rs("fname").Value Set bkm = doc.Bookmarks("bookmarkName") bkm.Range.Text = fName
A document property you access along these lines
Dim prop as Office.DocumentProperty Dim fName as String fName = rs("fname").Value Set prop = doc.BuiltinDocumentProperties("Title") prop.Value = fName
Correspondingly, for a docVar
Dim docVar as Word.Variable Dim fName as String fName = rs("fname").Value Set docVar = doc.Variables("docVarName") docVar.Value = fName
Hope this helps dissipate some of the confusion :-)
-- Cindy Meister (Word MVP) |