Set variable in VBA, use in document

D

DoctorG

I wish to change the value of a variable in VBA and display its current value
in the document body. i.e. "Room <RoomVar> master controls" where <RoomVar>
changes at the push of a Button or ToggleButton.

I can declare and manage a variable in VBA but I don't know how to reference
it in the document text.
I can set up a File>Properties>Custom>Add value which I can reference in the
text, but I don't know how to reference it in VBA.

Any of the two will do. Please help.
 
H

Helmut Weber

Hi Doctor,

e.g. create a doc-variable with a value, like this:

ActiveDocument.Variables("MyVar").Value = "MyTest1"

Insert a doc-variable-field with a variable with name "MyVar".

Change the value of the variable, like:
ActiveDocument.Variables("MyVar").Value = "MyTest2"

Update the field:
ActiveDocument.Fields("MyVar").Update
or all fields
ActiveDocument.Fields.Update

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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