Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

doc variable in header/footer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TRM - 10 Dec 2004 00:29 GMT
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!
Jay Freedman - 10 Dec 2004 01:54 GMT
>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.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
TRM - 10 Dec 2004 02:23 GMT
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
Charles Kenyon - 10 Dec 2004 19:01 GMT
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.
Signature


Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

>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!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.