Word VBA Macro

Steve001

Board Regular
Joined
Apr 13, 2017
Messages
85
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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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
I will try this at work tommrow, thank you and let you know
 
Upvote 0
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
Hello Macropod,,

Is it posable to use VBA to set cross referance properites ?

Steve
 
Upvote 0
Set them in what sense?
Hello Macropod,,

so after inserting a Cross referance

Change the inserted link to Dark Blue, and then under line it

I tried to record a macro to see if it would do this but there was nothing there

Regards

Steve
 
Upvote 0

Forum statistics

Threads
1,226,287
Messages
6,190,059
Members
453,592
Latest member
bcexcel

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