• Word Mergefield AddIf Method (2000)

    Author
    Topic
    #424188

    Hi All,

    I’ve been experimenting with the Word Mergefield AddIf Method, and I’m trying to get it to use the original mergefield as the field’s false result. Here’s what I’ve got so far:

    Sub MergeFieldAddIf()
    Dim i As Integer              ' Counter
    Dim FldNm As String           ' The field name, excluding any switches
    Dim MFld                      ' The original field
    Application.ScreenUpdating = False
    ActiveWindow.View.ShowFieldCodes = True
    With ActiveDocument
        '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
                    'Get the field's name
                    FldNm = .DataSource.DataFields(i).Name
                    'Select the current field
                    .Fields(i).Select
                    MFld = Selection.Range
                    .Fields.AddIf Range:=Selection.Range, MergeField:=FldNm, _
                    Comparison:=wdMergeIfIsBlank, TrueText:="MISSING DATA", _
                    FalseText:=MFld
                End If
            Next i
        End With
    End With
    ActiveWindow.View.ShowFieldCodes = False
    Application.ScreenUpdating = True
    End Sub
    

    What this gives me is the original field code in the false text’s position, but the field braces have been converted to Chrs 19 & 21, respectively. If I don’t use ShowFieldCodes, I get chevrons instead.

    I can get around this using:

                    FalseText:=""
                End If
                With Selection
                     .Range.Collapse (wdCollapseEnd)
                     .MoveLeft Unit:=wdCharacter, Count:=2
                     .MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
                 End With
                 ' This loses any switches from the original field
                 .Fields.Add Range:=Selection.Range, Name:=FldNm
    

    but this strikes me as a bit of a kludge. I can also get around it by copying the original field and pasting it back in afterwards, which is only marginally better. Any ideas?

    Cheers

    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

    Viewing 0 reply threads
    Author
    Replies
    • #973471

      I think I’d go for copying and pasting the original field. By the way, you can’t rely on the merge fields being placed in the document in the same order as the fields in the data source.

      FldNm = .DataSource.DataFields(i).Name

      may lead to incorrect results if the merge fields have been inserted out of order, or if they are repeated. You will have to extract the field name from the MERGEFIELD itself.

      • #973604

        Thanks Hans,

        This is what I end up with – not pretty but it works:

        Sub MergeFieldAddIf()
        Dim i As Integer ‘ Counter
        Dim FldNm As String ‘ The field name, excluding any switches
        Application.ScreenUpdating = False
        ActiveWindow.View.ShowFieldCodes = True
        With ActiveDocument
        ‘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
        ‘Get the field’s name
        FldNm = Trim(Replace(.Fields(i).Code, “MERGEFIELD”, “”))
        ‘Select the current field
        .Fields(i).Select
        Selection.Copy
        .Fields.AddIf Range:=Selection.Range, MergeField:=FldNm, Comparison:=wdMergeIfIsBlank, _
        TrueText:=”MISSING DATA”, FalseText:=””_
        End If
        ‘ Re-insert the original mergefield as the false text
        With Selection
        .Range.Collapse (wdCollapseEnd)
        .MoveLeft Unit:=wdCharacter, Count:=2
        .MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
        .Paste
        End With
        Next i
        End With
        End With
        ActiveWindow.View.ShowFieldCodes = False
        Application.ScreenUpdating = True
        End Sub

        By setting TrueText:=””, the approach I’ve used to re-insert the original mergefield as the false text could be used to alter the true text also. I still think it’s a kludge, though.

        You’ll also see that I’ve changed the way I get the original field’s name (thanks for the heads-up), to:
        FldNm = Trim(Replace(.Fields(i).Code, “MERGEFIELD”, “”))
        I could enhance this to remove the original field’s switches, since they arent’t needed where FldNm is used now, but leaving them in doesn’t seem to have any adverse effects.

        Cheers

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    Viewing 0 reply threads
    Reply To: Word Mergefield AddIf Method (2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: