PDA

View Full Version : Automatic hyperlinks in individual lines



JohnnyBravo
08-20-2013, 02:26 PM
I've got a simple word document. Dealing with MS Office 2010. All i want to do is make automatic hyperlinks:
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)
www.something.com/1 (http://www.something.com/1)

When I hit the return key at the end of each line, MS Word automatically makes the URL a hyperlink that the user can click on. This is exactly what I want. However, I have hundreds of these lines and it would be a complete PITA to go through each 200-300 lines and hit return. So I recorded a macro.

I get this:

10479

Here is the VBA code:

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
End Sub

I don't know enough about VBA to modify this routine so that the desired result is:
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1
http://www.something.com/1


Any help would be greatly appreciated.

Doug Robbins
08-20-2013, 06:29 PM
Use the following code:


Dim i As Long
Dim rngLinks As range
Dim rngAddress As range
Set rngLinks = Selection.range
With rngLinks
For i = .Paragraphs.Count To 1 Step -1
Set rngAddress = .Paragraphs(i).range
rngAddress.MoveEnd wdCharacter, -1
ActiveDocument.Hyperlinks.Add Anchor:=.Paragraphs(i).range, _
Address:=rngAddress.Text
Next i
End With

JohnnyBravo
08-20-2013, 08:53 PM
Nice, thanks for doing that Doug. Quick question:

Your routine makes every single line a hyperlink even if there is no URL. Is there a way to put a condition in there? Say for example,
If line starts with http: or https:
Then make hyperlinks.

Doug Robbins
08-21-2013, 12:15 AM
Use:


Dim i As Long
Dim rngLinks As range
Dim rngAddress As range
Set rngLinks = Selection.range
With rngLinks
For i = .Paragraphs.Count To 1 Step -1
If Left(.Paragraphs(1).range.Text, 4) = "http" Then
Set rngAddress = .Paragraphs(i).range
rngAddress.MoveEnd wdCharacter, -1
ActiveDocument.Hyperlinks.Add Anchor:=.Paragraphs(i).range, _
Address:=rngAddress.Text
End If
Next i
End With

JohnnyBravo
08-21-2013, 05:31 AM
Thank you Doug. Your help is very much appreciated! You just saved me a ton of keystrokes. :)

Edit: Just tried it and no go. Does it matter if the first line at the top of the page is just simple text?
hello there
my name is JohnnyBravo
http://www.something.com/1

With the first routine you provided above, I did a Ctrl + A to sell all the text and then ran the VBA routine. With the 2nd routine, I did the same and nothing happens. Weird.

gmaxey
08-21-2013, 03:48 PM
What happens if you set Word Options>Proofing>AutoCorrect Options>AutoFormat>Internet and network paths with hyplerlinks. Then add the Word Command AutoCorrect Now to the Quick Access Toolbar and execute it?

Doug Robbins
08-23-2013, 06:14 PM
Sorry, the code should have been:


Dim i As Long
Dim rngLinks As range
Dim rngAddress As range
Set rngLinks = Selection.range
With rngLinks
For i = .Paragraphs.Count To 1 Step -1
If Left(.Paragraphs(i).range.Text, 4) = "http" Then
Set rngAddress = .Paragraphs(i).range
rngAddress.MoveEnd wdCharacter, -1
ActiveDocument.Hyperlinks.Add Anchor:=.Paragraphs(i).range, _
Address:=rngAddress.Text
End If
Next i
End With



Posting of code seems to have gone crazy

The line:

If Left(.Paragraphs(1).range.Text, 4) = "http" Then

should have been

If Left(.Paragraphs(i).range.Text, 4) = "http" Then