Avatar of marrick13
marrick13
 asked on

Search for and Delete Text Not Hyperlinked-Why Does the Macro not Find Text if I Change its Case (and it's not case-sensitive)?

Delete Text Not Hyperlinked Test (Nov 3 2021).docmThe attached macro is intended to search for user-entered text and remove all occurrences of it in the active document if it is not hyperlinked.
 
The only way I know how to test text for links is to query if it has a link address or display text, and since this causes an error if no link is present, I trap that error and use it to perform the deletion.
 
What I have so far works perfectly as long as I either leave the default text in the input box as is, or retype it with the same case. If I change the case of just one letter in the input box, the macro doesn’t find it and stops. Even though I have .MatchCase = False, and even if I add Option Compare Text, this search is still case sensitive, and nothing I've tried works.
 
Can someone help me with getting this to find all occurrences of the text, delete those with links, regardless of the case of the entered search text?
 
The attached document that contains the macro has five instances of “Add as Friend Send Message”, two of which have links and should be preserved while the other three should be removed. First you can run it as is with the default search text to see how it is supposed to work, regardless of case, and then close it without saving and run it again, but change the case of the input box, and you’ll see it fails to find any of the same occurrences.

Microsoft WordVBA

Avatar of undefined
Last Comment
marrick13

8/22/2022 - Mon
Fabrice Lambert

Hi,

Set only the minimum options you need and leave everything else with default values:
    With oRng.Find
        .Text = pText
        .Replacement.Text = ""
        .Forward = True
        .MatchCase = False
        .Execute
        While .Found
            Countit = Countit + 1
            If oRng.Hyperlinks(1).Address <> "" Then
                oRng.Find.Execute
            End If
        Wend
    End With

Open in new window

In other words:
Don't pay for what you don't need.

Side notes:
- Use meaningfull names, things like "ST" are not meaningfull.
- Give up hungarian notation, it provides nothing usefull.
marrick13

ASKER
Wow -that works! Thank you so much.  I understand the concept of not paying for what I don't need, but can you explain why it made a difference here? All that was dine was to remove 6 statements, so why did they such a (negative) difference?
ASKER CERTIFIED SOLUTION
Fabrice Lambert

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
marrick13

ASKER
Thanks. That link didn't help much. I will assume those extra statements just did things to adversely affect the search when they weren't even needed. Your advice was sound. Thank you again!
Your help has saved me hundreds of hours of internet surfing.
fblack61