Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 10-08-2013, 09:02 AM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default Sloppy but effective

OK, here's the deal...Word Macros don't exactly execute linearly.

I eventually figured out how to write an If/Then/Else statement that mostly worked. Mostly. It didn't actually check the condition until after it replaced (wdReplaceOne). So it would change the first hyphen in a hyperlink and then go "oh, wait! This is a hyperlink!" and then it would skip any subsequent hyphens in that hyperlink.

So I ended up splitting my If/Then/Else into two separate If/Then blocks. The first one says "move on, nothing to do here," and the second one says, "aha! here's where we need a change." The code below, although cringe-worthy, does exactly what I want.

Code:
Sub replaceHyphens()
'
' Find hyphens that occur between digits and change them to en-dash, EXCEPT in hyperlinks
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "([0-9])-([0-9])"
    .Forward = True
    .Format = True
    .Wrap = wdFindContinue
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
 End With
 Do While (Selection.Find.Found = True)
    If (Selection.Style = ActiveDocument.Styles("Hyperlink")) Then
       Selection.Move Unit:=wdSentence, Count:=1
    End If
    Selection.Find.Execute
    If (Selection.Style <> ActiveDocument.Styles("Hyperlink")) Then
        Selection.Find.Replacement.Text = "\1" & Chr$(150) & "\2"
        Selection.Find.Execute Replace:=wdReplaceOne
    End If
Loop
End Sub
Reply With Quote
 

Tags
conditional, replace

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace Unless Style = Hyperlink? Insert the Hyperlink and hide all sheets except clicking Hyperlink PRADEEPB270 Excel 1 02-22-2013 09:47 AM
Hyperlink/Data Insert & replace jclinton Word 1 09-19-2012 07:22 PM
Replace Unless Style = Hyperlink? Macro to replace one specific heading style with another ubns Word VBA 44 09-04-2012 08:17 PM
Find bullets and replace with paragraph style? cdybdahl Word 1 12-02-2011 02:14 AM
Find and replace multiple documents change style BaPW Word 0 08-14-2011 11:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:54 PM.


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