简体   繁体   中英

Conditional Replace in Word Macro

Hi all and thanks in advance for any replies;

This question is about replacing text only under certain conditions.

Background: I'm working on a macro for the editorial department of an academic institution. They get loads of documents that have the same issues and asked for some help to reduce the time they spend on each one.

Two of the things they want:

  • If a hyphen is between two digits, change it to an en-dash
  • Change every ampersand (&) to the word "and"

I have a RegExp that finds and replaces those hyphens just fine, but I noticed a problem. My find/replace changes the "display text" of hyperlinks. Same with ampersands. Bad. So what I'm trying to figure out is how to exclude text that has Selection.Style = Word.ActiveDocument.Styles("Hyperlink")

BTW, what's the logical operator for "not equal"? I tried <> and >< but I always get an error telling me that an expression is expected. I'm new to VBA so please forgive the newbie question.

This is working (part of a much larger Sub):

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "([0-9])-([0-9])"
    .Replacement.Text = "\1" & Chr$(150) & "\2"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

So can I create an If/Then statement to tell it to replace only if the style is not hyperlink?

Thanks again, Rissa

PS I searched for similar posts and found one but it had never been answered.

Maybe a safer way to find out if your selection is a hyperlink is to use the following VBA code:

If Selection.Hyperlinks.Count = 1 Then
    MsgBox "The selection is a hyperlink"
Else
    MsgBox "The selection is not a hyperlink"
End If

I just tested it quickly and it works perfectly. To answer you second question, operations such as "=" and "<>" are for basic types such as Integer, Float, Long etc. Word.ActiveDocument.Styles("Hyperlink") returns an object. Therefore you would need to use "Is" and "Is Not"

Hope that helps.

(Thanks Black Cr0w, the logical operator is good to know)

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 did a replace (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 along, 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.

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

If anyone wants to suggest a cleaner way to do this, I'm all ears.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM