Solved

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

Posted on 2014-12-26
2
430 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

743 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