Make hyperlinks blue/underlined

S

supersub15

Hi there,

I'm kind of a novice, so any help is appreciated.

I'm trying to create a macro that looks into the document to find
hyperlinks, and if they're not already blue and underlined, make them
so.

I got this far, but I'm not sure if it's correct and where to go from
here:

Dim objHyperlink As Word.Hyperlink
Dim objWdRange As Word.Range

'check for hyperlinks
If objWdRange.Hyperlinks.Count > 0 Then
For Each objHyperlink In WdRange.Hyperlinks
xxxxx
Next
End If
End Sub

Thanks for your help.
Carlos
 
M

macropod

Hi supersub15,

You code will only find hyperlinks that already exist in the form of a HYPERLINK field.

Depending on what other formatting you document has, you might be able to achieve what you want with:

Sub ApplyHyperlinks()
Activedocument.Range.AutoFormat
End Sub
 
S

supersub15

Thanks for the reply, macropod.

Yes, I just want to format the hyperlinks. I ran your macro on one of
my documents, and the hyperlinks remained unchanged. Is there a way to
force the issue on the hyperlinks only (maybe using my bit of code)

Thanks
Carlos
 
G

Graham Mayor

There are two styles associated with hyperlinks i.e the Hyperlink style and
the Followed Hyperlink style.
Format these styles as you require them and the hyperlink appearance will
follow.

Sub FormatMyHyperlinks()
With ActiveDocument
With .Styles("Hyperlink")
.BaseStyle = "Default Paragraph Font"
.Font.Underline = wdUnderlineSingle
.Font.Color = wdColorBlue
End With
With .Styles("FollowedHyperlink")
.BaseStyle = "Default Paragraph Font"
.Font.Underline = wdUnderlineSingle
.Font.Color = wdColorPlum
End With
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

supersub15

Thanks Graham. Unfortunately, the hyperlinks are in "Body Text" style,
the same as the paragraph(s) they are in.

I've tried using this:

Sub ApplyHyperlinks()
With Options
.AutoFormatAsYouTypeReplaceHyperlinks = True
.AutoFormatReplaceHyperlinks = True
End With
ActiveDocument.Range.AutoFormat
End Sub

But, again, nothing. It's driving me crazy.
 
S

supersub15

I also found this bit of code while doing a search on the forum:

Sub FindAndActivateDeadHyperlinks2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "http"
.Wrap = wdFindStop
Do While .Execute
oRng.MoveEndUntil Cset:=" ", Count:=wdForward
ActiveDocument.Hyperlinks.Add Anchor:=oRng, Address:="oRng.text"
oRng.Collapse wdCollapseEnd
Loop
End With
End Sub

It works great if the hyperlink is followed by a space, but becomes
unpredictable and gives a "4198" run-time error, otherwise.

Any way to improve it?
 
G

Graham Mayor

OK then make them Hyperlink style

Sub ApplyHyperlinkStyle()
Dim oLink As Hyperlink
For Each oLink In ActiveDocument.Hyperlinks
oLink.Range.Style = "Hyperlink"
Next oLink
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top