VBA, Add hyperlink to each line of selected lines of text in word.

Tuankiet

New Member
Joined
May 7, 2019
Messages
2
First time learning VBA. I want to write a VBA script to add a hyperlink to each line of a range of selected paragraphs in word 2016. The hyperlink would open a document with the filename matching the line of text selected and are stored in the same folder.

Here what I found and put together but I can't get it to compile. please help.

Sub Test()
'
Dim arr

' Put text of Word's selection to the array
arr = Split(Selection.Range, vbCr)

' Show dimension of array
Debug.Print "LBound = " & LBound(arr), "UBound = " & UBound(arr)

' Show each element of array
For i = 0 To UBound(arr)
'select the line
ActiveDocument.Selection.arr(i) unit:=wdLine, Extend:=wdExtend
ActiveDocument.Hyperlinks.Add Anchor:= arr(i) Address:=arr(i) _
, SubAddress:="", ScreenTip:="", TextToDisplay:=arr(i)
Next

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).
Word doesn't ordinarily work with lines - it works with paragraphs - so going line-by-line is a lot of work. Moreover, you haven't said anything about how this 'range of selected paragraphs' might be identified.
 
Upvote 0
Thank you Macropod, I decided to do a different way. The selection method does not seem to work so I use a blank doc, do all work there and copy/paste to the working document.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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