Inserting variables in docs

P

Peter Knight

Hi all,

I have a document that I wish to write the value of an environment variable
into.

I can get the value of the environment easily enough, e.g. LoginID$ =
Environ("NAME"), the question is, how do I then insert the value of LoginID$
into a specific point in my document?

TIA

Peter
 
M

martinique

You'll need to do it in two steps, via a macro. In the macro read the
environment variable, and set the value as a document property (read Help on
the CustomDocumentProperties collection). If your macro is called AutoNew it
will run automatically when a new document is created.

Then, within your document, insert a DocProperties field.
 
D

Doug Robbins - Word MVP

Hi Peter,

To get the name of the user, see the article “How to get the username of the
current user” at:
http://www.mvps.org/word/FAQs/MacrosVBA/GetCurUserName.htm

To insert the user's name into the document, if you have a bookmark locating
the place where you want the name to appear, you can insert the name into
the range of the bookmark using

ActiveDocument.Bookmarks("BMName").Range.InsertBefore GetUserName

Or you could set a document variable to the user name be using

ActiveDocument.Variables("User").Value = GetUserName

and then in the document, use a { DOCVARIABLE "User" } field where you want
the name to appear.

I would warn against using Document Properties as a { DOCPROPERTY } field
can create an error if used in a header or footer.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Knight

Thanks for your help everybody. A shame docvariables can't be used in
footers, as that's exactly where I wanted to put the Environment variable
result. Oh well, I'll just have to put it somewhere else.

Many thanks
Peter
 
D

Doug Robbins - Word MVP

Hi Peter,

{ DOCVARIABLE } fields CAN be used in footers (and headers). It's the {
DOCPROPERTY } fields that cause problems in those locations in some versions
of Word.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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