Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-10-2014, 08:43 PM
THM THM is offline W07: search and replace style without losing hyperlink Windows 7 64bit W07: search and replace style without losing hyperlink Office 2007
Novice
W07: search and replace style without losing hyperlink
 
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
  #2  
Old 10-11-2014, 03:17 AM
macropod's Avatar
macropod macropod is offline W07: search and replace style without losing hyperlink Windows 7 64bit W07: search and replace style without losing hyperlink Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-15-2014, 08:52 AM
THM THM is offline W07: search and replace style without losing hyperlink Windows 7 64bit W07: search and replace style without losing hyperlink Office 2007
Novice
W07: search and replace style without losing hyperlink
 
Join Date: Oct 2014
Posts: 2
THM is on a distinguished road
Default

Ok, great, I'll try this and report back. Sorry for the code tags - 'twas my first post.
Reply With Quote
Reply

Tags
hyperlink, macro, search and replace



Similar Threads
Thread Thread Starter Forum Replies Last Post
W07: search and replace style without losing hyperlink replace letters by font without losing format romanticbiro Word VBA 31 03-16-2017 04:00 PM
W07: search and replace style without losing hyperlink Losing formatting using .Text to replace treva31 Word VBA 2 09-30-2014 10:31 PM
W07: search and replace style without losing hyperlink 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:21 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft