I have an MSAccess application which uses different templates. The user
clicks on a button in the Access form, and the application selects the
appropriate template, fills it with data from the database, saves and prints
it, then closes the document. The templates have a footer which prints the
current date and the page.
I need to add variable to the template which can be printed in the footer.
I've read some of the posts regarding document variables, but my code resides
in VB modules in Access, not in the Word documents, and I'm not sure how to
begin.
How do I create a variable in the template which can be populated by the
Access application and prints in the footer?
Code snippet:
Dim WordObj As Word.Application
Set WordObj = GetObject(, "Word.Application")
WordObj.Documents.Add Template:=TemplateFile, NewTemplate:=False
The application moves to bookmarks and prints variable data, sets fonts, etc.
I would use:
Dim WordObj As Word.Application
Dim WordDoc as Document
Set WordObj = GetObject(, "Word.Application")
Set WordDoc = WordObj.Documents.Add(Template:=TemplateFile,
NewTemplate:=False)
With WordDoc
.Variables("varName").Value = [something from your database]
[other code to manipulate populate the document]
End With
You mention moving to bookmarks. It would be much better to use the .Range
of the bookmark that move to it. You can however also create document
variables to hold the information that you are now putting into the
bookmarks and by use of the \* Charformat switch on the { DOCVARIABLE
varname } field, you can have formatting automatically applied by formatting
the D of DOCVARIABLE in the template with the desired formatting.
You will need to include code to update the field to display the information
that is contained in the variables.

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
>I have an MSAccess application which uses different templates. The user
> clicks on a button in the Access form, and the application selects the
[quoted text clipped - 20 lines]
> The application moves to bookmarks and prints variable data, sets fonts,
> etc.
cmw - 07 Jan 2006 20:26 GMT
Thanks, I see how to populate the variable, but I think I'm missing the
first step - creating it. How do I place the variable into the footer unless
I create it in the template, add it to the footer and save the template then
populate it when I create a new doc from the template?
Thanks again,
cmw
> I would use:
>
[quoted text clipped - 43 lines]
> > The application moves to bookmarks and prints variable data, sets fonts,
> > etc.
Doug Robbins - Word MVP - 08 Jan 2006 08:46 GMT
You just need to know the name of the variable that will be created. You
can then insert a { DOCVARIABLE varName } in the template where varName is
the name of the variable that is being created by the
.Variables("varName").Value = statement

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Thanks, I see how to populate the variable, but I think I'm missing the
> first step - creating it. How do I place the variable into the footer
[quoted text clipped - 61 lines]
>> > fonts,
>> > etc.
cmw - 08 Jan 2006 20:19 GMT
I appreciate all your help. I'm making progress but -
1. Added {docvariable NBNBR} in the footer and saved the template
2. In the VBModule in the Access Application
Dimmed and set the the WordDocument and attempted to populate the variable
Dim WordDoc as Document
.Variables("NBNBR").Value = rstWorkSheet!NBNBR
But, the document created from the template contained the text "{docvariable
NBNBR}" in the footer, not the value of the variable (when I do a watch on
the .variables("NBNBR").value in the module, I can see the appropriate value.
Really appreciate your help!!! cmw
> You just need to know the name of the variable that will be created. You
> can then insert a { DOCVARIABLE varName } in the template where varName is
[quoted text clipped - 66 lines]
> >> > fonts,
> >> > etc.
Doug Robbins - Word MVP - 08 Jan 2006 20:35 GMT
Did you use Ctrl+F9 to insert the field delimiters { }? If so, use Alt+F9
to toggle off the display of the field codes.
To get the fields to be updated with the value of the variable, you will
need to include code to access the .Range of the footer and update the
fields in it
Dim i As Long
With WordDoc
For i = 1 To .Sections.Count
.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields.Update
.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields.Update
Next i
End With

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
>I appreciate all your help. I'm making progress but -
> 1. Added {docvariable NBNBR} in the footer and saved the template
[quoted text clipped - 88 lines]
>> >> > fonts,
>> >> > etc.