changing the font and font-size in DOCVARIABLE field from vba

4

4tis

hi
i know how to change the text of a DOCVARIABLE field in word with a vba
macro
but i cannot find a way to change the font name, font size, align and
left to right with VBA for a specific DOCVARIABLE field.
any advice please ?
 
C

Cindy M.

Hi 4tis,
i know how to change the text of a DOCVARIABLE field in word with a vba
macro
but i cannot find a way to change the font name, font size, align and
left to right with VBA for a specific DOCVARIABLE field.
You do this by manipulating the RANGE. For example

Dim rng as Word.Range
Set rng = ActiveDocument.Fields(index).Result
rng.Font.Name = "Arial"

the tricky part will be determining the index to identify the field.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
4

4tis

i tried this code based on what you wrote to me :

Dim ind As Integer


ind = ActiveDocument.Variables("myPrimaryFooterField").Index
MsgBox (ind)


Set rng = ActiveDocument.Fields(ind).Result
rng.Font.name = "Arial"


but then, when i run this code i get an error says :
run-time error '5941':
the requested number of the collection does not exist.

i did not found any helping issue about this error
any idea what i can do to resolve this ?
did i done something worng ?

thanks in advanced
 
C

Cindy M.

Hi 4tis,
i tried this code based on what you wrote to me :

Dim ind As Integer


ind = ActiveDocument.Variables("myPrimaryFooterField").Index
MsgBox (ind)


Set rng = ActiveDocument.Fields(ind).Result
rng.Font.name = "Arial"


but then, when i run this code i get an error says :
run-time error '5941':
the requested number of the collection does not exist.
In Word, the fields in a document are indexed by the order in
which they appear in the document. There is no relationship
whatsoever to the Index property of the Variables collection. Word
has over 100 different kinds of fields, not just docVar fields.

If you have no other way of identifying the field, and you aren't
trying to format it at the time the field is being inserted, then
you probably have no choice but to loop through the entire
collection of fields in the Word document, testing the identifier
for each field. For example, something along these lines

For each fld in ActiveDocument.Fields
If Instr(fld.Code, "myPrimaryFooterField") <> 0 Then
Set rng = ActiveDocument.Fields(ind).Result
rng.Font.name = "Arial"
End If
Next

If you need to do this a lot then it would probably make sense to
loop through the fields once, setting up a custom collection with
an index on the doc variable's name so that you can access them
more quickly...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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