[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Writing macro to insert MergeField

Posted on 2005-05-05
3
Medium Priority
?
605 Views
Last Modified: 2012-08-13
I need a macro that when run will open up a dialog box (non-modal if possible) and allow the user to choose from a predefined list of merge fields and insert into the document. I'm not asking anyone to write this entire macro for me, just some ideas/suggestions to point me in the right direction. Thanks in advance for you help!

- Larkin
0
Comment
Question by:larkinyoung
  • 2
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 13943312
I suggest that you put bookmarks where you want the merge fields to go, then code like this.

In a module:

Option Explicit

Sub OpenForm()
    UserForm1.Show False 'non-modal
End Sub

In the UserForm:

Option Explicit

Private Sub CommandButton1_Click()
    Dim strBookmarkName As String
    Dim strMergeFieldName As String
    Dim rng As Range
    Select Case ComboBox1.Text
        Case "First Name"
            strBookmarkName = "bmkFirstName"
            strMergeFieldName = "FirstName"
        Case "Second Name"
            strBookmarkName = "bmkSecondName"
            strMergeFieldName = "SecondName"
    End Select
    Set rng = ActiveDocument.Bookmarks(strBookmarkName).Range
    ActiveDocument.MailMerge.Fields.Add rng, strMergeFieldName
End Sub

Private Sub UserForm_Activate()
    Me.ComboBox1.Clear
    ComboBox1.AddItem "First Name"
    ComboBox1.AddItem "Second Name"
End Sub
0
 

Author Comment

by:larkinyoung
ID: 13951954
Bookmarks arent an option, the idea is that the user can select a merge field name from a listbox of prepopulated values, for example, and insert that merge field in the location that the cursor is placed in the document.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 13953016
OK.
That's simpler. Change this procedure.

Private Sub CommandButton1_Click()
    Dim strMergeFieldName As String
    Select Case ComboBox1.Text
        Case "First Name"
            strMergeFieldName = "FirstName"
        Case "Second Name"
            strMergeFieldName = "SecondName"
    End Select
    ActiveDocument.MailMerge.Fields.Add Selection.Range, strMergeFieldName
End Sub
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question