Macro to apply style

B

boulderado

Hi all,

I am looking to create a macro that will search a document for all email
addresses and apply a style (bold and a different color than the other text).
Any tips would be greatly appreciated.

Thanks.
 
J

Jay Freedman

boulderado said:
Hi all,

I am looking to create a macro that will search a document for all
email addresses and apply a style (bold and a different color than
the other text). Any tips would be greatly appreciated.

Thanks.

The AutoFormat As You Type feature (if the "Internet and network paths with
hyperlinks" option is enabled in the AutoCorrect options dialog) applies the
style named Hyperlink to email addresses and URLs. The separate AutoFormat
feature does the same for existing addresses.

By default the Hyperlink style uses a blue underlined font. If you want to
change that formatting, modify the Hyperlink style. No macro is needed.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

boulderado

Thanks, Jay.

The problem is the Word document comes from a client every month and the
formatting is not consistent. I would like to just run a macro that
identifies the email addresses and automatically applies a font weight of
bold and a color of Blue, Accent 1.
 
J

Jay Freedman

OK, this code will do that...

The big list of stuff is just to make sure that the macro doesn't
permanently change your options to values you don't want. It saves the
current values, changes what it needs, and then puts back the original
option values.

Dim oldAFopt_bullet As Boolean
Dim oldAFopt_first As Boolean
Dim oldAFopt_heading As Boolean
Dim oldAFopt_list As Boolean
Dim oldAFopt_other As Boolean
Dim oldAFopt_space As Boolean
Dim oldAFopt_paren As Boolean
Dim oldAFopt_mail As Boolean
Dim oldAFopt_style As Boolean
Dim oldAFopt_east As Boolean
Dim oldAFopt_frac As Boolean
Dim oldAFopt_hyper As Boolean
Dim oldAFopt_ord As Boolean
Dim oldAFopt_plain As Boolean
Dim oldAFopt_quote As Boolean
Dim oldAFopt_symbol As Boolean

Sub ReformatAddresses()
' store the user's current set of
' AutoFormat options, and then set them
' the way we want them
RASaveAndSetOptions

' run the AutoFormat to make hyperlinks
ActiveDocument.Range.AutoFormat

' restore the user's options
RARestoreOptions

' modify the Hyperlink style as needed
With ActiveDocument.Styles("Hyperlink")
.Font.Bold = True
.Font.Color = -738131969 ' Accent 1
End With
End Sub

Private Sub RASaveAndSetOptions()
With Options
oldAFopt_bullet = .AutoFormatApplyBulletedLists
oldAFopt_first = .AutoFormatApplyFirstIndents
oldAFopt_heading = .AutoFormatApplyHeadings
oldAFopt_list = .AutoFormatApplyLists
oldAFopt_other = .AutoFormatApplyOtherParas
oldAFopt_space = .AutoFormatDeleteAutoSpaces
oldAFopt_paren = .AutoFormatMatchParentheses
oldAFopt_mail = .AutoFormatPlainTextWordMail
oldAFopt_style = .AutoFormatPreserveStyles
oldAFopt_east = .AutoFormatReplaceFarEastDashes
oldAFopt_frac = .AutoFormatReplaceFractions
oldAFopt_hyper = .AutoFormatReplaceHyperlinks
oldAFopt_ord = .AutoFormatReplaceOrdinals
oldAFopt_plain = .AutoFormatReplacePlainTextEmphasis
oldAFopt_quote = .AutoFormatReplaceQuotes
oldAFopt_symbol = .AutoFormatReplaceSymbols

.AutoFormatApplyBulletedLists = False
.AutoFormatApplyFirstIndents = False
.AutoFormatApplyHeadings = False
.AutoFormatApplyLists = False
.AutoFormatApplyOtherParas = False
.AutoFormatDeleteAutoSpaces = False
.AutoFormatMatchParentheses = False
.AutoFormatPlainTextWordMail = False
.AutoFormatPreserveStyles = True
.AutoFormatReplaceFarEastDashes = False
.AutoFormatReplaceFractions = False
.AutoFormatReplaceHyperlinks = True
.AutoFormatReplaceOrdinals = False
.AutoFormatReplacePlainTextEmphasis = False
.AutoFormatReplaceQuotes = False
.AutoFormatReplaceSymbols = False
End With
End Sub

Private Sub RARestoreOptions()
With Options
.AutoFormatApplyBulletedLists = oldAFopt_bullet
.AutoFormatApplyFirstIndents = oldAFopt_first
.AutoFormatApplyHeadings = oldAFopt_heading
.AutoFormatApplyLists = oldAFopt_list
.AutoFormatApplyOtherParas = oldAFopt_other
.AutoFormatDeleteAutoSpaces = oldAFopt_space
.AutoFormatMatchParentheses = oldAFopt_paren
.AutoFormatPlainTextWordMail = oldAFopt_mail
.AutoFormatPreserveStyles = oldAFopt_style
.AutoFormatReplaceFarEastDashes = oldAFopt_east
.AutoFormatReplaceFractions = oldAFopt_frac
.AutoFormatReplaceHyperlinks = oldAFopt_hyper
.AutoFormatReplaceOrdinals = oldAFopt_ord
.AutoFormatReplacePlainTextEmphasis = oldAFopt_plain
.AutoFormatReplaceQuotes = oldAFopt_quote
.AutoFormatReplaceSymbols = oldAFopt_symbol
End With
End Sub
 

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