PDA

View Full Version : Searching for mail merge fields



robert32
06-23-2009, 07:43 AM
Hi,

I'm after some code that will allow me to search though a word document and isolate any instances of mail merge field.

I have got as far as writing the code to toggle the field codes so that they are displayed, but am struggling to find the code that will test a set of text for the presence of mail merge field(s).

Any help would be grately appreciated.


Cheers,

Rob.

fumei
06-23-2009, 01:25 PM
Post some code. And please tell us exactly what you are trying to do.

"I'm after some code that will allow me to search though a word document and isolate any instances of mail merge field."

What does that mean? Isolate? Isolate...how? What does that mean?

Why do you want to test for the "presence" of merge fields?

robert32
06-23-2009, 03:51 PM
Basically, I have a set of mail merge templates that interface with a central database.

I need to apply some code to each to find all mail merge fields and export them to a separate file.

I need to know exactly what the fields are, but there are ~700 docs, so do not have the time to go through each doc and copy and paste each mail merge field.



Thanks,

Rob.

fumei
06-24-2009, 02:55 PM
Sorry, but I do not follow why you are doing this.

Basically, I have a set of mail merge templates that interface with a central database.

OK, this is normal.

I need to apply some code to each to find all mail merge fields and export them to a separate file.

Why? What is the purpose of doing this?

I need to know exactly what the fields are, but there are ~700 docs, so do not have the time to go through each doc and copy and paste each mail merge field.

Again...WHY. WHY do you need to go through 700 docs and cut and paste the merge fields themselves?????

You can go through a document and find mail merge fields....except....standard built-in ones are explicitly named. For example:Dim aField As Field
Dim msg As String
For Each aField In ActiveDocument.Fields
If aField.Type = wdFieldMergeField Then
msg = msg & aField.Code & vbCrLf
End If
Next
MsgBox msgwould find all mail merge fields that are NOT predefined mail merge fields.

In other words, AddressBlock would not be found. GreetingLine would not be found. That is because they have their own constant.
Dim aField As Field
Dim msg As String
For Each aField In ActiveDocument.Fields
If aField.Type = wdFieldMergeField Or _
aField.Type = wdFieldAddressBlock Or _
aField.Type = wdFieldGreetingLine Then
msg = msg & aField.Code & vbCrLf
End If
Next
MsgBox msg
would find them.