Challenge writing a macro for setting Mergefields properties

Hi,

I have about 400 Word documents which all use the same set of MergeFields in different layouts.

I need to figure out a way to write a macro which will loop through all the MergeFields in a doc and set the "Preserve formatting during updates" checkbox.

When I manually did this for a field and recorded a macro, the code that got generated was:

WordBasic.FormatField Field:="MERGEFIELD ReqDate "

...which didn't really help since it created a new field when I ran it.

Is there any way I can do this?

Thanks,

JGP

[590 byte] By [JGP] at [2007-12-22]
# 1

OK, so this hack seems to work for me in Word 2003, but I can't claim any "best-practice-ness" about it. So take it with a caveat that further testing may be required...

Const cMergeFormat = " \* MERGEFORMAT "
Dim fld As Word.Field

For Each fld In ActiveDocument.Fields

If Right(fld.Code.Text, Len(cMergeFormat)) <> cMergeFormat Then
fld.Code.Text = fld.Code.Text & cMergeFormat
End If

Next fld

If you are using mail merge in your 400 word documents the collection in the for loop may have to be ActiveDocument.MailMerge.Fields instead of ActiveDocument.Fields

Happy coding!

rusty

rustycoder at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...