View Single Post
 
Old 10-10-2014, 08:43 PM
THM THM is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Oct 2014
Posts: 2
THM is on a distinguished road
Question W07: search and replace style without losing hyperlink

Hello,

How might I run a search ad replace (in macro form) where the style changes is applied only to a portion of a hyperlinked string. I always lose the hyperlink in the replaced portion

Example

Say I have the following string:
Section X of the Interpretation Act

The entire string is hyperlinked with the following link (kept brief for the question):
http://www.e-laws.gov.on.ca/sectionX

The "Interpretation Act" section must be styled with Emphasis.

When I run the following macro, the hyperlink is stripped from the newly stylized segment leaving only "Section X" hyperlinked.

So this:
Section X of the Interpretation Act

becomes this:
Section X of the Interpretation Act


Code:
Sub StyleChange()
'
' StyleChange Macro
'
'
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Emphasis")
With Selection.Find
  .Text = "Interpretation Act"
  .Replacement.Text = "Interpretation Act"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Last edited by macropod; 10-11-2014 at 03:18 AM. Reason: Added code tags & formatting
Reply With Quote