Word VBA Macro

Steve001

Board Regular
Joined
Apr 13, 2017
Messages
78
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 does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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

Forum statistics

Threads
1,225,725
Messages
6,186,650
Members
453,367
Latest member
bookiiemonster

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