Results 1 to 3 of 3
  1. #1
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Word Mergefield AddIf Method (2000)

    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:
    <pre>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
    </pre>

    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:
    <pre> 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
    </pre>

    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
    [MS MVP - Word]

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Mergefield AddIf Method (2000)

    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.

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Word Mergefield AddIf Method (2000)

    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
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •