Knowledgebase: Letters
Microsoft Word Macro to create merge fields
Posted by Carlos Ramos on 19 Nov 2012 12:59 PM
1. Click Alt + F8 on your keyboard.
2. Enter "mf" under 'Macro name' and click the Create button.
3. Paste the macro code below, save it and close the Microsoft Visual Basic for Applications window.
Sub mf()
'
' mf Macro
' Converts text selected in a document into a merge field.
'
    Selection.Copy
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:=Selection.Text, PreserveFormatting:=False
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "«"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = "»"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
4. Back in MS Word, press ALT + F, then press T (for 2007 go to the Office button and select Word Options).
5. Select Customize Ribbon (Customize in 2007).
6. Click the Customize... button next to Keyboard shortcuts.
7. Under Categories, select "Macros".
8. Under Macros, select "mf".
9. Place the cursor on the 'Press new shortcut key' field and press ALT + M.
10. Click Assign, Close and OK.
To test it:
1. Type a field name in word within brackets (e.g. {BALANCE}).
2. Select the field including the brackets, but nothing else (not even adjoining spaces).
3. Press ALT + M and your selection should be converted to a merge field.
(3 vote(s))
Helpful
Not helpful

Copyright © 2015 Insight Software, LLC | My Vision Express