Use Document Variable in Footer

C

cmw

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.
 
D

Doug Robbins - Word MVP

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.


--
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
 
C

cmw

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
Doug Robbins - Word MVP said:
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.


--
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

cmw said:
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.
 
D

Doug Robbins - Word MVP

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

--
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

cmw said:
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
Doug Robbins - Word MVP said:
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.


--
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

cmw said:
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.
 
C

cmw

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



Doug Robbins - Word MVP said:
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

--
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

cmw said:
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
Doug Robbins - Word MVP said:
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.


--
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
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.
 
D

Doug Robbins - Word MVP

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


--
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

cmw said:
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



Doug Robbins - Word MVP said:
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

--
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

cmw said:
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:

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.


--
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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top