Search code examples
vbams-wordmailmerge

Looking for VBA Method to Display a Word Mailmerge Formula on screen or Output to Excel/Other Word Document


I have a word document with mailmerge fields and formula (mostly if statements).

The cells they are embedded in are so small that I cannot view either the field or the formula without copying and pasting the formula into Notepad.

Is there a way via VBA to:

  1. Display the Mailmerge Field/Formula (via a msgbox or other) when I have highlighted the field?
  2. Loop through all Mailmerge fields/formulae and output them to either another word doc or excel spreadsheet

Solution

  • Make a copy of your mailmerge main document, then run the following macro against the copy:

    Sub Demo()
    Application.ScreenUpdating = False
    ActiveWindow.View.ShowFieldCodes = False
    With ActiveDocument.Range
      Do While .Tables.Count > 0
        .Tables(1).ConvertToText vbTab
      Loop
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Text = ""
        .Format = False
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Text = "[^32-^255]"
        .Execute Replace:=wdReplaceAll
      End With
      .Style = wdStyleNormal
      .Font.Reset
      .Fields.Update
    End With
    ActiveWindow.View.ShowFieldCodes = True
    Application.ScreenUpdating = True
    End Sub
    

    You should end up with a document containing just the fields (displayed), some images, tabs & paragraph/line breaks.