#1
|
||||
|
||||
VBA Find inexplicably aborts & goes to top
What quirk in a document could cause this code to stop looking for more matches, (when there definitely are more):
Code:
Sub MarkParenthesizedItalicsNoProofing() Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find .Text = "\([A-Z][a-z.]@[!\)]@\)" .MatchWildcards = True .Wrap = False Do While .Execute Selection.MoveStart wdCharacter, 1 Selection.MoveEnd wdCharacter, -1 If Selection.Range.Italic = True Then Selection.Range.NoProofing = True End If Selection.Collapse wdCollapseEnd Loop End With End Sub It only happens on certain files so I surmise it's something about those files and not the code, but for the life of me I can't figure out what's different about them. They vary widely but none are especially long or complex or old or mishandled. The point where the code stops looking varies, but if I copy & paste the first bunch of (successful) matches a dozen times, the macro will find *all* of those, then still freak out on one of the later ones. So the number of iterations seems to be irrelevant. I've tried progressively more desperate simplifications, even not using Do While .Execute at all -- just a regular Find, then if .Found is True, collapse the selection and go to a label above the Find block. Even that variant crazily stops at a random point, goes to the top of the file and hangs. I know this doesn't present well. I also know the Find feature can be quirky. Still, seems like this shouldn't happen. Any ideas? Using Word 2016, Windows 10 Pro. Mark |
#2
|
|||
|
|||
I would guess that some of your documents have text without a clear open/close parentheses. The wildcard search depends on that structure.
|
#3
|
||||
|
||||
Yeah that was my very first thought but it's not the case. In fact in the latest doc where it occurs, there are 3 properly closed pairs of parens on the same page, just south of where the macro surrenders & heads home.
(A separate routine runs on each file beforehand which, among other things, ensures all parens are paired.) |
#4
|
|||
|
|||
Sorry that didn't work. I do think that you may be asking too much of the second half of the wildcard string. Maybe add ^13--and other special characters you see in your doc--to the square brackets, like so "\([A-Z][a-z.]@[!\)^13]@\)". This may prevent execute from crashing.
|
#5
|
||||
|
||||
No luck, same result. Why would adding ^13 help? I guess I should've explained, the purpose of this macro is to spot italicized botanical names in parens, for example (Coryphantha alversonii) and exclude them from the spell-checker. I can't think of a case where a paragraph mark would affect the search.
Thanks for your attention BTW. |
#6
|
||||
|
||||
The following should work.
Code:
Sub MarkParenthesizedItalicsNoProofing() With ActiveDocument.Range With .Find .ClearFormatting .Text = "\([A-Z][a-z.][!\(]@\)" .Replacement.Text = "" .Forward = True .Format = False .MatchWildcards = True .Wrap = wdFindStop .Execute End With Do While .Find.Found .Start = .Start + 1 .End = .End - 1 If .Font.Italic = True Then .NoProofing = True .Collapse wdCollapseEnd .Find.Execute Loop End With End Sub [!\)] should be: [!\(] Otherwise it will fail if there are unmatched parentheses and the entire unmatched string is not italic.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
I'm not a fan of the selection object and I would think you need to be explicit in settintg the direction of the search. There also seems to be a flaw with the No Proofing not catching the first word if it is immediately preceded by a bracket. Try this variation
Code:
Sub MarkParenthesizedItalicsNoProofing() Dim aRng As Range, aSubRng As Range Set aRng = ActiveDocument.Range With aRng.Find .ClearFormatting .Text = "\([A-Z][a-z.]@[!\)]@\)" .MatchWildcards = True .Forward = True .Wrap = False Do While .Execute Set aSubRng = ActiveDocument.Range(aRng.Start + 1, aRng.End - 1) aRng.NoProofing = aSubRng.Italic = True aRng.Collapse wdCollapseEnd Loop End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
||||
|
||||
Thanks to macropod/Paul and Guessed/Andrew. I'm also not a Selection junkie at heart, but my many prior attempts using ranges fell flat, and debugging felt marginally simpler with the text visible. HOWEVER, both of your replies indirectly pointed me to the actual root of the issue, which is:
This document -- and apparently all of the others in which my original code routinely failed -- contains fields whose results contain parens, and those fields confound the adjustments needed to check for italic font within the parens. (Sure enough, the Execute loop went south right when it got to the first such field. Agh. I haven't fixed it yet but it should be straightfoward now. Thanks so much to both of you. |
#9
|
||||
|
||||
Trying to update field results via a Find/Replace is fairly pointless, since anything that causes Word to refresh the field display will wipe out your edits. And, as I said, your Find expression was itself flawed.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
||||
|
||||
Yes I know, and I appreciate the fix to the expression. None of the fields in these docs ever contained, or ever will contain, the botanical names being targeted, so I'll just write around them. The fields are the reason the selection (or range) suddenly died when the macro tried to exclude its parens to check the font (since a field actually counts as just one character).
|
#11
|
||||
|
||||
Perhaps you could attach a document to a post with some representative data (delete anything sensitive) demonstrating the problem you're having with fields? You can do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
||||
|
||||
Sure, if you're interested, but again, there's no urgency now; I can code around it. Short excerpt is attached. It's from an unreleased government document but I've greeked most of the text.
In the sample, the callout for Table 4.4-1 (incl its title) is a REF field, and you can see italic scientific names above & below it. Your code runs fine if I take out that field, but hangs if it's left in. |
#13
|
||||
|
||||
It don't know why you'd have an opening parenthesis within a field code but the closing one outside it. That said, you might try:
Code:
Sub MarkParenthesizedItalicsNoProofing() With ActiveDocument.Range With .Find .ClearFormatting .Text = "\([A-Z][a-z.][!\(]@\)" .Replacement.Text = "" .Forward = True .Format = False .MatchWildcards = True .Wrap = wdFindStop .Execute End With Do While .Find.Found .Start = .Start + 1 .End = .End - 1 If Len(.Text) = 0 Then .End = .End + 1 Else If .Font.Italic = True Then .NoProofing = True End If .Collapse wdCollapseEnd .Find.Execute Loop End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
||||
|
||||
Thanks Paul, this is beautiful. I did stumble over one quirk: at unpredictable times the first word in parens (when there's more than one, which is common) won't escape the spell-checker unless I also include the opening paren in the range to be set to NoProofing, so that part is now:
Code:
If .Font.Italic = True Then .Start = .Start -1 .NoProofing = True End If Thanks again. |
Tags |
vba find and replace |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
find IP in range / find number between numbers | gn28 | Excel | 4 | 06-14-2015 03:46 PM |
Find where find text contains a double quote | norgro | Word VBA | 1 | 01-23-2015 10:58 PM |
Find what box in Find and replace limits the length of a search term | Hoxton118 | Word VBA | 7 | 06-10-2014 05:05 AM |
Macro that can find phrase and then find another and copy | jperez84 | Word VBA | 10 | 09-19-2012 04:48 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |