best type of field to use?

B

BRC

I have a document that I want to insert some info contained in Vba
code. I have spent a lot of time trying to do this and have concluded
(to perserve fomatting) the best way would be to use fields in the
document and just update the field with a vba variable. Lets say I have
var1 var2 and var3. I want to insert them into a specific place in the
document.I.e. field1.value=var1. I am not sure what the best type of
field is to do this. Any suggestions would be greatly appreciated.
Thanks in advance for any suggestions.
BRC
 
D

Doug Robbins - Word MVP

And if the field is formatted with the desired formatting, that is usually
all that you need. Where on the odd occasion that does not produce the
desired result, it can be forced by the addition of a \* charformat switch
before the closing field delimiter } and the apply the desired formatting to
the D of Docvariable.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BRC

Thanks for the input. I thought i had this figured out. Using the
following code i am trying to update the fields.
'code to get newvalue
msgbox(newvalue)
'msbox displays new value
ActiveDocument.Variables(var1).Value = newvalue
'I select range (r) that includes var1
r.select
r.fields.update
'I get Error! No document variable supplied.
What is wrong with this picture? again thank you.
 
G

Greg Maxey

You should be getting an error on
ActiveDocument.Variables(var1).Value = newvalue
The var1 needs to be set off with " "

Try this:

Sub Test()
Dim NewValue As String
NewValue = "Test this code"
ActiveDocument.Variables("var1").Value = NewValue
R.Select
R.Fields.Update
End Sub
 
B

BRC

That was exactly the problem. I was following some code from another
post and that code did not include the quotes around the varname. after
correcting this it works as expected.
Thank you BRC
 
D

Doug Robbins - Word MVP

Or use:

Dim newvalue As String
newvalue = InputBox("Enter the new value for the variable", "Update
Variable")
With ActiveDocument
.Variables("var1").Value = newvalue
.Range.Fields.Update
End With

It will be quicker if you just update the range in question, rather than
select it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

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