VBA help for placing hyperlink into Word from Excel

LionChaser10

New Member
Joined
Nov 30, 2022
Messages
4
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello! I am new to VBA so please forgive me, but I need help copying information from Excel into a Word document that is already created and the last item needs to be a hyperlink. The sheet has 3 columns first being the filename, second column is random text, and third column is the full path of the link that needs to be inserted into the Word document as a hyperlink. For clarity, the path is a network drive and the excel is formatted as plain text for the path. There are several thousand Word documents that are already created, but just need to be updated. Everything works perfect with the code, except do not know how to make the text inserted in third column after the "Link: " to be a hyperlink. I have tried mail merge in Word and for this activity does not meet the needs we are aiming for, and this seems to be the best solution. Would you be able to help me? Thank you in advance!

VBA Code:
Sub UpdateFiles()
 Dim sFn As String, r As Range, i As Long, x As Long
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx"
        If Len(Dir(sFn)) > 0 Then
            Set wdDoc = wdApp.Documents.Open(sFn)
            With wdDoc
                .Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10)
                .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10)
                .Content.InsertBefore "Filename: " & r.Text & Chr(10)
                .Close
                i = i + 1
            End With
            Set wdDoc = Nothing
        Else
            x = x + 1
        End If
    Next r
    Set wdApp = Nothing
    MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped"
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try.
VBA Code:
Sub UpdateFiles()
 Dim sFn As String, r As Range, i As Long, x As Long
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx"
        If Len(Dir(sFn)) > 0 Then
            Set wdDoc = wdApp.Documents.Open(sFn)
            With wdDoc
                'delete this line  .Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10)
                'add the following three lines
                Selection.HomeKey Unit:=wdStory
                .Hyperlinks.Add Anchor:=Selection.Range, Address:=r.Offset(0, 2).Text & Chr(10)
                .Content.InsertBefore "Link: "
                .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10)
                .Content.InsertBefore "Filename: " & r.Text & Chr(10)
                .Close
                i = i + 1
            End With
            Set wdDoc = Nothing
        Else
            x = x + 1
        End If
    Next r
    Set wdApp = Nothing
    MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped"
End Sub
 
Upvote 0
Sub UpdateFiles() Dim sFn As String, r As Range, i As Long, x As Long Dim wdApp As New Word.Application Dim wdDoc As Word.Document For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row) sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx" If Len(Dir(sFn)) > 0 Then Set wdDoc = wdApp.Documents.Open(sFn) With wdDoc 'delete this line .Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10) 'add the following three lines Selection.HomeKey Unit:=wdStory .Hyperlinks.Add Anchor:=Selection.Range, Address:=r.Offset(0, 2).Text & Chr(10) .Content.InsertBefore "Link: " .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10) .Content.InsertBefore "Filename: " & r.Text & Chr(10) .Close i = i + 1 End With Set wdDoc = Nothing Else x = x + 1 End If Next r Set wdApp = Nothing MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped" End Sub
@HongRu Thank you so much for the reply! The code stops running at "Selection.HomeKey Unit:=wdStory", I am lost at what the issue may be, any thoughts?
 
Upvote 0
try to add one more line "wdDoc.Activate" before "Selection.HomeKey Unit:=wdStory"
VBA Code:
Sub UpdateFiles()
 Dim sFn As String, r As Range, i As Long, x As Long
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx"
        If Len(Dir(sFn)) > 0 Then
            Set wdDoc = wdApp.Documents.Open(sFn)
            With wdDoc
                '.Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10)
                wdDoc.Activate
                Selection.HomeKey Unit:=wdStory
                .Hyperlinks.Add Anchor:=Selection.Range, Address:=r.Offset(0, 2).Text & Chr(10)
                .Content.InsertBefore "Link: "
                .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10)
                .Content.InsertBefore "Filename: " & r.Text & Chr(10)
                .Close
                i = i + 1
            End With
            Set wdDoc = Nothing
        Else
            x = x + 1
        End If
    Next r
    Set wdApp = Nothing
    MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped"
End Sub
 
Upvote 0
try to add one more line "wdDoc.Activate" before "Selection.HomeKey Unit:=wdStory"
VBA Code:
Sub UpdateFiles()
 Dim sFn As String, r As Range, i As Long, x As Long
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx"
        If Len(Dir(sFn)) > 0 Then
            Set wdDoc = wdApp.Documents.Open(sFn)
            With wdDoc
                '.Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10)
                wdDoc.Activate
                Selection.HomeKey Unit:=wdStory
                .Hyperlinks.Add Anchor:=Selection.Range, Address:=r.Offset(0, 2).Text & Chr(10)
                .Content.InsertBefore "Link: "
                .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10)
                .Content.InsertBefore "Filename: " & r.Text & Chr(10)
                .Close
                i = i + 1
            End With
            Set wdDoc = Nothing
        Else
            x = x + 1
        End If
    Next r
    Set wdApp = Nothing
    MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped"
End Sub
@HongRu Thank you, but it still stops running on "Selection.HomeKey Unit:=wdStory", other thoughts?
 
Upvote 0
Try this.
VBA Code:
Sub UpdateFiles()
 Dim sFn As String, r As Range, i As Long, x As Long
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    For Each r In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        sFn = ActiveWorkbook.Path & "\" & r.Value & ".docx"
        If Len(Dir(sFn)) > 0 Then
            Set wdDoc = wdApp.Documents.Open(sFn)
            With wdDoc
                '.Content.InsertBefore "Link: " & r.Offset(0, 2).Text & Chr(10)
                'only add "wdApp" before "Selection.Range"
                .Hyperlinks.Add Anchor:=wdApp.Selection.Range, Address:=r.Offset(0, 2).Text & Chr(10)
                .Content.InsertBefore "Link: "
                .Content.InsertBefore "Message: " & r.Offset(0, 1).Text & Chr(10)
                .Content.InsertBefore "Filename: " & r.Text & Chr(10)
                .Close
                i = i + 1
            End With
            Set wdDoc = Nothing
        Else
            x = x + 1
        End If
    Next r
    Set wdApp = Nothing
    MsgBox i & " Files Updated" & Chr(10) & x & " Files Skipped"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top