Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Through all Merge Fields within a Word Document 1

Status
Not open for further replies.

DanAtCDS

Technical User
May 20, 2003
22
US
I'm not sure if I asked the appropriate questions in my last post to get a clear answer. So we'll try again!

I have code which will highlight fields left blank after a mail merge has occurred. However, this code is all static in nature. That is, every merge field is coded "behind the macro" so that it cycles through each and every one. (a lot of repetitive code being used, just for different merge fields.) What I'd like to know is if there is a way to dynamically loop through a Word document and if any given merge field within it is blank, .... I'll highlight it. So if I have 35 merge fields, I'll have one block of "highlighting" code and loop through it 35 times instead of 35 blocks of the same code.
Any help is greatly appreciated and if I've been unclear or there is anyone willing to give this a shot please let me know!!!!!!!

Thanks in advance for anything you can toss my way!
Dan
 
Hi Dan,

The difficulty with what you're asking is that there are no mergefields left in the document after the merge has taken place - all you've got is the text result where the fields used to be.

However, since you want the empty fields highlighted, you can do that with a bit of field coding as part of the mailmerge process itself, as indicated in my previous reply.

If you want to, you could then have your macro loop through the highlighted text - all you need to be sure of, then, is that the highlighting will be unique to the empty mergefields.

Cheers
 
Macropod,
Sorry I'm being so dense here!! You make a very valid point with trying to do anything after the merge. However, can I do the highlighting on all merge fields without coding each one possible?
 
Hi Dan,

Basically, you can't. As advised in the earlier thread, you could embed your mergefields into IF statements like:
{IF{MERGEFIELD Item}<> "" {MERGEFIELD Item} "MISSING DATA"}
This will output the string "MISSING DATA" for any empty field. To make the "MISSING DATA" string stand out more, you could format it with whatever font attributes you'd like (eg Bold, Red with Yellow background).

Of course, if you're going to do a search for the "MISSING DATA" strings in the output files, the extra formatting probably isn't necessary.

It would be a fairly trivial task to code a vba routine to go through a document, or all documents in a folder, looking for mergefields and wrap them with the extra field coding. I don't have the time to do this right now - maybe next week if you're still having difficulty.

Cheers
 
macropod,
If you can give me further direction that would be great!! I tried to make the inserted IF statements work for me but I never could figure it out! In addition, is there a way to put blank spaces ( " " ) instead of "MISSING DATA" AND have it's background highlighted??
One last question, ... what happens if there is already an if statement present, ... There are some check boxes on several forms that need dealing with?

Thanks,
Dan
 
Hi Dan,

I am not sure you have really got what macropod is telling you.

1. If a mergefield contains nothing (no data), the field is NOT inserted into the document. Therefore there is nothing to search for. There is no field to find.

2. Therefore, you make an IF statement that checks the mergefield data, and if it IS empty, then make it not empty.

3. Therefore:

{IF{MERGEFIELD Item}<> "" {MERGEFIELD Item} "MISSING DATA"}

is, from the point of view of meaning, the same as:

{IF{MERGEFIELD Item}<> "" {MERGEFIELD Item} "BlahBlahBlah"}

OR;
{IF{MERGEFIELD Item}<> "" {MERGEFIELD Item} " "}

It is simply checking for a value, and if it is something, then change it to something else. What you are wanting to do, is check for nothing, and if nothing is the value, then make it something. "MISSING DATA" is something; "BlahBlahBlah" is something - as is " ".

However, I may be having another stupid day, but if the IF is checking <> "" - this seems to me to be saying if the mergefield value is NOT blank, then make it "Missing Data". Should this not be if the mergefield data IS blank, make it "Missing data" - or " "?

Whatever.

Gerry
 
Gerry,
The IF statement is set up correctly. It states, IF FIELD_IN_QUESTION is NOT EQUAL to NULL (it has data) then use FIELD_IN_QUESTIONS'S value, ELSE "MISSING DATA" I follow what macropod is saying. However, what I'd like to do is not put any text in the area needing it (thus the " "). I'd jut like to highlight it to draw the applicant's attention to the area for further review.
Is there a way to add formatting to the IF statements as well??

Thanks,
Dan
 
Hi Dan,

Here's some quick & dirty code to modify all mergefields in a document:

Sub ChangeMergeFields()
Dim TrkStatus As Boolean ' Track Changes flag
Dim aField As Integer
Dim HiLite As String
HiLite = Chr(160) & Chr(160) & Chr(160) & Chr(160) & Chr(160)
With ActiveDocument
' Store current Track Changes status, then switch off
TrkStatus = .TrackRevisions
.TrackRevisions = False
' Loop through all MailMerge Fields and update
For aField = .MailMergeFields.Count To 1 Step -1
With .MailMerge.Fields(aField)
.Select
'Add the IF Test
With Selection
.Copy
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
.TypeText Text:="IF"
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=1
.TypeText Text:="= """" """ & HiLite & """ "
.MoveLeft Unit:=wdCharacter, Count:=2
.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend
.Range.HighlightColorIndex = wdPink
.MoveRight Unit:=wdCharacter, Count:=3
.Paste
End With
End With
Next
' Restore original Track Changes status
.TrackRevisions = TrkStatus
End With
End Sub

There is a more sophisticated AddIf Method for this, but I couldn't figure out how to get it to re-insert the original mailmerge field as the true or false value - best I could do was to get the field coding.

You mention there being "some check boxes on several forms". These won’t be affected, as the code is only looking for mergefields.

Cheers
PS: If you run this code more than once against a given document, every mergefield, including the ones inserted into the IF statements via the previous iteration, will again be nested within IF statements.

PPS: I coded the macro to produce the coloured blank as a true result, instead of as the false result - there's not much difference either way.
 
Macropod,
Thank you for the code! I've not had a chance to put it in place and test it in my scenario but it looks promising.
In regards to the check boxes. These are merge fields. The original owner of the mail merge documents set them up so if a mail merge value met certain criteria, then it gave a "checked" check box. Otherwise, we are left with an "unchecked" check box.

Please accept my great appreciation for all of your help and guidance through this Q&A session! It has helped me tremendously![2thumbsup] I'm afraid that my Business Users of this process are now leaning away from the mail merge document and I'm going to not get to use this code!!?!?!?!?![evil] However, I'll file it away in my "code bank" and be ready to use it when the next opportunity arises!

Thanks again,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top