![]() |
#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? |
#2
|
|||
|
|||
![]()
This is interesting. I'm wondering if Greg's detailed explanation of Word's Fickle find property can help you, if you haven't already read it, I'm hoping it can answer your problem.
Word's Fickle VBA .Find Property |
#3
|
|||
|
|||
![]()
Thanks for your reply. I hadn't come across that article before, and I learned some useful things from it, but unfortunately I didn't find anything in it that appeared to be applicable to this issue.
|
#4
|
|||
|
|||
![]()
Okay. I am not at my computer to try at the moment, but off the top of my head, I'm wondering if it would behave better using a Range rather than Selection? Since it sometimes works, and then breaks, and Word is known for modifying the selection during a find, I'd try to explicitly set your range and see if that changes anything.
|
#5
|
|||
|
|||
![]()
Thanks for your further reply. However, the issue appears to be not with the scope of the search but with the execution of the search. The search string is always found in the expected place; the issue is that when a wildcard macro is preceded by another macro, the first match is not found by the wildcard macro until the user presses Shift F4 (Find next), whereas the first match should be found immediately when the macro is run (as it is when the wildcard macro is run by itself).
The cause appears to be something to do with the end-of-document prompts that appear at the end of the preceding macro. Test 4: - Go to the beginning of the document. - Run the "WildcardsFalse" macro. - When it finds the first match (the "a" in "taxi"), type "Q" to replace it, then press Shift+F4 ("Find next"). - When it finds the next match (the "a" in "above"), press F4 ("Repeat last action") to replace it, then press Shift+F4 to continue searching. - When it finds the next match (the "a" in "boat"), press F4 to replace it. At this point, don't press Shift+F4, so there are no end-of-document prompts. - Go to the beginning of the document. - Press the keyboard shortcut to run the "WildCardsTrue" macro. - It correctly finds the first match (the "x" in "taxi"). No issue in this case. Test 5: - Go to the beginning of the document. - Run the "WildcardsFalse" macro. - When it finds the first match (the "a" in "taxi"), type "Q" to replace it, then press Shift+F4 ("Find next"). - When it finds the next match (the "a" in "above"), press F4 ("Repeat last action") to replace it, then press Shift+F4 to continue searching. - When it finds the next match (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. If you press Shift+F4 (Find next) at this point, then the first match will be found, but it should be found immediately when the macro is run (as it is when the macro is run by itself). The essential difference between Test 4 (which doesn't have the issue) and Test 5 (which does have the issue) is that Test 4 skips the end-of-document prompts at the end of the preceding macro. (I can't skip these as a work-around, as I need to include the end-of-document prompts as part of a consistent procedure.) |
![]() |
|
![]() |
||||
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 |