Word VBA Macro

Steve001

Board Regular
Joined
Apr 13, 2017
Messages
79
Office Version
  1. 365
  2. 2021
  3. 2013
Platform
  1. Windows
Hello,

I am not familiar with macros in word but have used them in excel quite a bit...
Is it possible to look through a word document for hyper links these are mainly from 'insert cross reference' then do the following ....

Some of these can be inside tables from 'insert table function'

check if it has an underline
underline if not

check the colour
set to dark blue if not

PS. Please put comments in the code so i can understand

I am using office 363 & office 2021

Regards
Steve
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
For example:

VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim HLnk As Hyperlink
For Each HLnk In ActiveDocument.Hyperlinks
  HLnk.Range.Style = wdStyleHyperlink
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
For example:

VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim HLnk As Hyperlink
For Each HLnk In ActiveDocument.Hyperlinks
  HLnk.Range.Style = wdStyleHyperlink
Next
Application.ScreenUpdating = True
End Sub
Hello Macropod,

tried this today at work and it didnt work, we use office 365

The document does have multiple sections in it. 3 or 4 i think and a different front page
The colour change was more than likley done with CTRL+A then font "automatic" or a highlighted section then font "automatic" on a document review 😖

Regards

Steve
 
Upvote 0
In which case, they probably aren't actual hyperlinks, but just url text strings.

That could be rectified with code like:
VBA Code:
Sub Demo()
ActiveDocument.AutoFormat
End Sub
but that tends to be too aggressive.

Try:
VBA Code:
Sub MakeLinks()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Replacement.Text = ""
    ' hyperlinks
    .Text = "htt[ps]{1,2}://[!^13^t^l ]{1,}"
  End With
  Do While .Find.Execute
    .Hyperlinks.Add .Duplicate, .Text, , , .Text
    .Start = .Hyperlinks(1).Range.End
  Loop
End With
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Replacement.Text = ""
    ' email addresses
    .Text = "<[0-9A-ÿ.\-]{1,}\@[0-9A-ÿ\-.]{1,}"
  End With
  Do While .Find.Execute
    .Hyperlinks.Add .Duplicate, .Text, , , .Text
    .Start = .Hyperlinks(1).Range.End
  Loop
End With
Application.ScreenUpdating = True
End Sub
The above code activates all email addresses and hyperlinks in the document body.
 
Upvote 0
Hello Macropod,

Thank you for your help so far its very much appriciated. Unfortunatley i have sent you on a incorrect path.
I found out today that some of the report templates that we use have styles set up and others dont and the ones that do have them can be a bit 'iffy'

is it possable to set a wdStyleHyperlink in VBA to the following :

Underline
Dark Blue Text

then use the 1st bit of code above to set any overridden colours or missing underlines ?

would this work on the "insert cross referance" link?

what would happen if this is then run in a document that has styles set ?
what would happen with email addresses ? are they a differnt style ?

Many thanks

Steve
 
Upvote 0
Word's built-in Hyperlink Style is what you get is you type a url into a document then press <Enter> or <Space> after it. Word is also designed so that such hyperlinks changed color when followed.

Cross-references are not hyperlinks. Yes, they can link within a document the way hyperlinks do, but they are fundamentally different.
 
Upvote 0

Forum statistics

Threads
1,225,761
Messages
6,186,890
Members
453,383
Latest member
SSXP

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