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!!!!!!!!
> >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
[quoted text clipped - 10 lines]
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
Doug Robbins - 10 Dec 2004 02:54 GMT
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"

Signature
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
> Okay - if I use the "activedocument.variables.add" method - which I do
> have
[quoted text clipped - 31 lines]
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
TRM - 10 Dec 2004 04:23 GMT
Thank you so much for the quick response - I will put this to use!
> The following code will add a { DOCVARIABLE varname } field to the Primary
> Header of the First Section of the active document.
[quoted text clipped - 42 lines]
> >> Jay Freedman
> >> Microsoft Word MVP FAQ: http://word.mvps.org
TRM - 10 Dec 2004 02:55 GMT
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
> 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
[quoted text clipped - 22 lines]
> > Jay Freedman
> > Microsoft Word MVP FAQ: http://word.mvps.org
Alex Ivanov - 10 Dec 2004 04:21 GMT
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"

Signature
Please reply to NG only. This email is not monitored.
Alex.
> Just for more information, here is the code I have. I am not getting
> errors
[quoted text clipped - 60 lines]
>> > Jay Freedman
>> > Microsoft Word MVP FAQ: http://word.mvps.org