Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-26-2023, 02:26 AM
harry harry is offline Wildcard search not working consistently Windows 10 Wildcard search not working consistently Office 2019
Advanced Beginner
Wildcard search not working consistently
 
Join Date: Jul 2010
Posts: 31
harry is on a distinguished road
Default Wildcard search not working consistently

I am developing a set of macros for searching through a document to find certain characters. If a specified character is found, then the user may either leave the character unchanged or replace it by typing or pasting another character over it. The user then presses Shift+F4 ("Find next") to continue the search. If further occurrences of the specified character are found, then each time the user may either leave the character unchanged or press F4 ("Repeat previous action") to replace it with the same character as before, then press Shift+F4 to repeat the search procedure until the end of the document is reached.

Some of the macros use wildcards so they search for sets of characters, and some of them don't use wildcards so they search for just one specific character.

The macros all work fine when used separately, but if two macros are used one after the other where one uses wildcards and the other doesn't, then there is an issue, depending on the sequence in which they are used. If the second macro doesn't use wildcards, then it works as expected; but if the second macro does use wildcards, then it doesn't work as expected, as shown in the example below. (Tests 2 and 3 are essentially the same, apart from the sequence in which the macros are run.)

1. Create a document containing the following text.
taxi
blue
above
yellow
zone
boat

2. Create the following two macros.
- The "WildcardsTrue" macro uses wildcards and finds any of the characters "xyz".
- The "WildcardsFalse" macro doesn't use wildcards and finds just the character "a".
- To make the macros easier to use, I set up keyboard shortcuts for them.

----------
Code:
Sub WildcardsTrue()
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "[xyz]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute
End Sub
----------

Code:
Sub WildcardsFalse()
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "a"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
    End With
    Selection.Find.Execute
End Sub
----------

3. Test 1:
- Open the document and press the keyboard shortcuts repeatedly in a random sequence.
- Both macros work as expected. The "WildcardsTrue" macro correctly finds the characters "xyz" in "taxi", "yellow" and "zone", and the "WildcardsFalse" macro correctly finds the character "a" in "taxi", "above" and "boat". When the end of the document is reached, the macros correctly keep cycling through the document.

4. Test 2:
- Go to the beginning of the document.
- Press the keyboard shortcut to run the "WildcardsTrue" macro.
- When it finds the first matching character (the "x" in "taxi"), type "Q" to replace it with that character, then press Shift+F4 ("Find next").
- When it finds the next matching character (the "y" in "yellow"), press F4 ("Repeat last action") to replace it, then press Shift+F4 to continue searching.
- When it finds the next matching character (the "z" in "zone"), press F4 to replace it, then press Shift+F4 to continue searching.
- Word displays a prompt ("We've reached the end of the document. Do you want to continue searching from the beginning?"); click on Yes.
- Word displays another prompt ("No results found" [OK]"); click on OK.

- Go to the beginning of the document.
- Press the keyboard shortcut to run the "WildCardsFalse" macro.


- It correctly finds the first matching character (the "a" in "taxi"). No issue here.

5. Test 3:
- Go to the beginning of the document.
- Press the keyboard shortcut to run the "WildcardsFalse" macro.
- When it finds the first matching character (the "a" in "taxi"), type "Q" to replace it with that character, then press Shift+F4 ("Find next").
- When it finds the next matching character (the "a" in "above"), press F4 ("Repeat last action") to replace it, then press Shift+F4 to continue searching.
- When it finds the next matching character (the "a" in "boat"), press F4 to replace it, then press Shift+F4 to continue searching.
- Word displays a prompt ("We've reached the end of the document. Do you want to continue searching from the beginning?"); click on Yes.
- Word displays another prompt ("No results found" [OK]"); click on OK.

- Go to the beginning of the document.
- Press the keyboard shortcut to run the "WildCardsTrue" macro.
- Instead of finding the first matching character (the "x" in "taxi"), nothing happens; this is the issue. Evidently the search string has been set correctly, as pressing Shift+F4 at this point correctly finds the first matching character (the "x" in taxi"), but how can I get it to find the first matching character without having to press Shift+F4 at this point?
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard search not working consistently Applying styles via wildcard search not working as expected Peterson Word 2 12-10-2020 06:16 PM
Wildcard Search gmaxey Word VBA 4 07-12-2020 05:13 PM
Wildcard search not working consistently Problems with wildcard search VBA MikeForward Word VBA 3 02-19-2019 03:23 PM
Wildcard search not working consistently IFERROR not (consistently) working John 1978 Excel 8 04-26-2017 12:05 AM
Wildcard search help. Kempston Word 0 11-13-2009 03:58 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:22 AM.


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