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!

Help with Mail merge fields ... 2

Status
Not open for further replies.

DanAtCDS

Technical User
May 20, 2003
22
US
Can anyone tell me why the following code causes an error when executed within Word?? I'm getting a "run-time error 424 object required"
Code:
ActiveDocument.MailMerge.Fields(i).Code = "{If {" & ActiveDocument.MailMerge.Fields(i).Code & "} =" & Chr(34) & Chr(34) & _
        " " & Chr(34) & "XX" & Chr(34) & " " & "{" & ActiveDocument.MailMerge.Fields(i).Code & "}"
I am trying to cycle through my word document and replace all mail merge fields with If statements to determine if they will have data or not. If they don't have data I would like to show "XX".

Any help and/or direction will be greatly appreciated!!

Thanks,
Dan
 
Dan,

Try this:
Code:
ActiveDocument.MailMerge.Fields(i).Code[b].Text[/b] = "{If {" & ActiveDocument.MailMerge.Fields(i).Code[b].Text[/b] & "} =" & Chr(34) & Chr(34) & _
        " " & Chr(34) & "XX" & Chr(34) & " " & "{" & ActiveDocument.MailMerge.Fields(i).Code[b].Text[/b] & "}"

** The Code property returns a Range object.

Regards,
Mike
 
Hi DanAtBigBlue,

The code might work, but the technique doesn't - you don't end up with the original mergefield but only a text representation of it. Try this instead:
Code:
Sub MergeFieldAddIf()
Dim TrkStatus As Boolean      ' Track Changes flag
Dim i As Integer              ' Counter
Dim FldNm As String           ' The field name, including any switches
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument
    ' Store current Track Changes status
    TrkStatus = .TrackRevisions
    'Switch off - we don't want to track these!
    .TrackRevisions = False
    'Limit action to mailmerge fields
    With .MailMerge
        ' Loop backwards through all mailmerge fields and update
        For i = .Fields.Count To 1 Step -1
            'Limit actions to MERGEFIELD fields (eg ignore MERGEREC & MERGESEQ fields)
            If .Fields(i).Type = wdFieldMergeField Then
                'Select the current field
                .Fields(i).Select
                Selection.Copy
                FldNm = Trim(Replace$(.Fields(i).Code, "MERGEFIELD", ""))
                .Fields.AddIf Range:=Selection.Range, MergeField:=FldNm, Comparison:=wdMergeIfIsBlank, _
                TrueText:="XX", FalseText:=""
                'Now go back and replace the empty FalseText with the original merge field.
                With Selection
                    .Range.Collapse (wdCollapseEnd)
                    .MoveLeft Unit:=wdCharacter, Count:=2
                    .MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
                    .Paste
                End With
            End If
        Next i
    End With
    ' Restore original Track Changes status
    .TrackRevisions = TrkStatus
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub

Cheers
 
Macropod,
You're exactly right, ... My code "worked" but didn't perform the task I was hoping it would. [sad] Yours on the other hand, ... was awesome!! [thumbsup] Thank you for giving me the assistance I needed to accomplish the task at hand.

Your time and effort are much appreciated!!
Thank you,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top