![]() |
#1
|
|||
|
|||
![]()
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? |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Peterson | Word | 2 | 12-10-2020 06:16 PM |
Wildcard Search | gmaxey | Word VBA | 4 | 07-12-2020 05:13 PM |
![]() |
MikeForward | Word VBA | 3 | 02-19-2019 03:23 PM |
![]() |
John 1978 | Excel | 8 | 04-26-2017 12:05 AM |
Wildcard search help. | Kempston | Word | 0 | 11-13-2009 03:58 AM |