doc variable in header/footer

T

TRM

I would like to add a doc variable to the header/footer of word doc using VBA
- can this be done? I have found how to add a variable... maybe with a
bookmark? Sorry, just thought of that. If there are other suggestions or
comments, please respond! Thanks for your help!
 
J

Jay Freedman

I would like to add a doc variable to the header/footer of word doc using VBA
- can this be done? I have found how to add a variable... maybe with a
bookmark? Sorry, just thought of that. If there are other suggestions or
comments, please respond! Thanks for your help!
If you've already defined a document variable -- and the only way to
do that is through VBA, using the ActiveDocument.Variables.Add method
-- then you can display its value anywhere in the document by
inserting a DocVariable field containing the name of that variable.
 
T

TRM

Okay - if I use the "activedocument.variables.add" method - which I do have
in the code, how do I control where the doc variable field is added - can
this be done using VBA? I need it to be added to the document header and
footer sections. Just to make sure I am giving a clear (?) objective here;
I'll give you the situation. Normally the user will use a template I have
created to make the form, which has the docvariable fields already in place.
But, some forms were created prior to this set up. Now I need to be able to
add the docvariable fields into the form using VBA. I thought that was the
"add" method mentioned above, if it is... then I'm back to how do I control
where they are inserted. If it's not, then I still need to find out how to
add the fields! THANKS again!!!!!!!!
 
D

Doug Robbins

The following code will add a { DOCVARIABLE varname } field to the Primary
Header of the First Section of the active document.
You probably need to be more specific about just where in the Header it gets
added and you will have to do that by setting varange appropriately:

Dim varange As Range
Set varange =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
ActiveDocument.Fields.Add Range:=varange, Type:=wdFieldDocVariable,
Text:="varname"


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
T

TRM

Just for more information, here is the code I have. I am not getting errors
on the docvariable.add lines, but I am also not seeing the variables in the
document (anywhere). Also, I am trying to do this from Access.

Dim oApp As Object
Dim oDoc As Object
Dim CurrDate As Date
Dim strSavDir As String

If (strApp = "Yes") Then
'add check for existing doc variable, if not, create it
Set oApp = GetObject("", "Word.Application")
oApp.Documents.Open (strPath)
With oApp
Set oDoc = oApp.Documents.Item(1)
If (strExist = "-1") Then
oDoc.Variables.Add Name:="FormName", Value:=strDocName
oDoc.Variables.Add Name:="FVersion", Value:=Revision
'oDoc.Variables.Add Name:="FormName", Value:=strDocName
'oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Variables.Add
Name:="FormName"

oDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Update
oDoc.Fields.Update
 
A

Alex Ivanov

Docvariable is just what it is, a variable.
You need to insert DOCVARIABLE fields anywhere in the document where you
want to display its value.
Insert/Field/DocVariable/VariableName
or through code

activedocument.variables("MyVar")="Hello, Word!"
activedocument.Fields.Add
activedocument.Sections(1).Footers(wdHeaderFooterPrimary).Range,wdFieldDocVariable,"MyVar"
 
C

Charles Kenyon

If your document is (ever) going to be used in Word 97, you need to use a
document property rather than a document variable. Document variable fields
in headers/footers will crash Word 97.
 

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