#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
You could use something along the lines of:
Code:
With Selection.Hyperlinks(1).Range.Fields(1).Result .Start = .Start + InStrRev(.Text, "Interpretation Act") - 1 .Style = "Emphasis" End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Ok, great, I'll try this and report back. Sorry for the code tags - 'twas my first post.
|
Tags |
hyperlink, macro, search and replace |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
replace letters by font without losing format | romanticbiro | Word VBA | 31 | 03-16-2017 04:00 PM |
Losing formatting using .Text to replace | treva31 | Word VBA | 2 | 09-30-2014 10:31 PM |
Exclude style(s) from search & replace | NobodysPerfect | Word | 2 | 03-20-2014 02:29 AM |
Replace Unless Style = Hyperlink? | Designergrrl | Word VBA | 7 | 10-08-2013 09:04 AM |
Search and Replace - Clear Search box | JostClan | Word | 1 | 05-04-2010 08:46 PM |