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]