Solved

Insert an active but variable hyperlink from the DataSource in a MailMerge Word doc

Posted on 2014-12-26
2
455 Views
Last Modified: 2015-01-12
Hello,

How do you insert an active but variable hyperlink from the DataSource in a mail merge Word (2013) document?

For example, suppose you are doing a Word mail merge and have defined, as you're DataSource, an Excel file (named Source.xlsm) which contains several hundred records (rows). Also, suppose that the 6th column in the Excel file has the heading URL and contains several dozen different URLs distributed randomly throughout the records.

When the mail merge Word doc is created, the URL for a given record appears wherever the <<URL>> field is placed. However, it appears as simple text just like the rest of the document and does not have any hyperlink functionality.

Questions:

1) In a Word mailmerge, how do you create a <<URL>> field which has hyperlink functionality?

2) Is there a way to display a "friendly name" (using the term from the Excel =HYPERLINK() function) as the hyperlink in a mailmerge rather than the URL itself? (For this question, assume that the 5th column [in the Excel file used in the above example] has the heading "URL Name" and contains the "friendly names" for each record.)

Thanks
0
Comment
Question by:WeThotUWasAToad
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40519537
There isn't a method for that built-in to MailMerge, but it can be done with VBA code. This Word macro example finds the worksheet and copies the cell with the hyperlink (here from column 3) into a place where there is a some distinctive text (here "@@@@")
Sub StepMerge()
    Dim r As Integer
    Dim rng As Word.Range
    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    Dim wdMainDoc As Document
    Dim wdResultDoc As Document
    Dim strQueryParts() As String
    
    Set wdMainDoc = ActiveDocument
    Set xlApp = CreateObject("Excel.Application")
    Set xlWbk = xlApp.Workbooks.Open(wdMainDoc.MailMerge.DataSource.Name)
    strQueryParts = Split(wdMainDoc.MailMerge.DataSource.QueryString, "`")
    Set xlWks = xlWbk.Worksheets(Replace(strQueryParts(1), "$", ""))
    
    With wdMainDoc.MailMerge
        .Destination = wdSendToNewDocument
        For r = 1 To .DataSource.RecordCount
            .DataSource.LastRecord = r
            .DataSource.FirstRecord = r
            .Execute
            Set wdResultDoc = Application.ActiveDocument
            Set rng = wdResultDoc.Range
            With rng.Find
                .Text = "@@@@"
                .Execute
                xlWks.Cells(r + 1, 3).Copy
                rng.Paste
            End With
        Next r
    End With
    xlWbk.Close False
    xlApp.Quit
End Sub

Open in new window

0
 

Author Closing Comment

by:WeThotUWasAToad
ID: 40545755
Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
My experience with Windows 10 over a one year period and suggestions for smooth operation
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now