Show Textbox if merge field is not null

Hi All,

I'm trying to figure out how to make a textbox visible only if a merge field in it has data. My best guess is something like this should work (but of course it doesn't). Any help is appreciated.

If ActiveDocument.MailMerge.Fields(UD_Notice) Is Not Null Then
    Set Box = ActiveDocument.Shapes.AddTextbox( _
        Orientation:=msoTextOrientationHorizontal, _
        Left:=350, Top:=200, Width:=200, Height:=180)
        Box.TextFrame.TextRange.Text = UD_Notice

End If

Answer
Answer

There really isn't much point in testing the state of a mergefield via VBA, since that only exists in the mailmerge main document and is lost in the output document. What you can do, though, is use field coding in the mailmerge main document to conditionally display the textbox:
{IF«UD_Notice»<> "" "Anchor textbox here"}

You'll need to use 'relative to character' positioning to anchor the textbox where indicated.

Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar. The spaces represented in the field construction are all required.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 14, 2023 Views 1,390 Applies to: