Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-25-2018, 07:09 AM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default 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
What happens: it stops looking, after finding several matches, sends the cursor to the top of the file, then flutters as if stuck in a loop, then hangs. No error message or other clues.

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
Reply With Quote
  #2  
Old 05-25-2018, 12:51 PM
d4okeefe d4okeefe is offline VBA Find inexplicably aborts & goes to top Windows 10 VBA Find inexplicably aborts & goes to top Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

I would guess that some of your documents have text without a clear open/close parentheses. The wildcard search depends on that structure.
Reply With Quote
  #3  
Old 05-25-2018, 12:55 PM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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.)
Reply With Quote
  #4  
Old 05-25-2018, 02:22 PM
d4okeefe d4okeefe is offline VBA Find inexplicably aborts & goes to top Windows 10 VBA Find inexplicably aborts & goes to top Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 05-25-2018, 07:55 PM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 05-26-2018, 12:34 AM
macropod's Avatar
macropod macropod is offline VBA Find inexplicably aborts & goes to top Windows 7 64bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Note that there is a flaw in your own Find expression:
[!\)]
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]
Reply With Quote
  #7  
Old 05-26-2018, 12:50 AM
Guessed's Avatar
Guessed Guessed is offline VBA Find inexplicably aborts & goes to top Windows 10 VBA Find inexplicably aborts & goes to top Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #8  
Old 05-26-2018, 02:38 AM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 05-26-2018, 02:49 AM
macropod's Avatar
macropod macropod is offline VBA Find inexplicably aborts & goes to top Windows 7 64bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 05-26-2018, 03:01 AM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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).
Reply With Quote
  #11  
Old 05-26-2018, 03:32 AM
macropod's Avatar
macropod macropod is offline VBA Find inexplicably aborts & goes to top Windows 7 64bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #12  
Old 05-26-2018, 04:37 AM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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.
Attached Files
File Type: docx example.docx (38.8 KB, 10 views)
Reply With Quote
  #13  
Old 05-26-2018, 06:15 AM
macropod's Avatar
macropod macropod is offline VBA Find inexplicably aborts & goes to top Windows 7 64bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 05-29-2018, 01:11 AM
mark99k's Avatar
mark99k mark99k is offline VBA Find inexplicably aborts & goes to top Windows 7 32bit VBA Find inexplicably aborts & goes to top Office 2010 32bit
Novice
VBA Find inexplicably aborts & goes to top
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

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
And yes, the field-straddling parentheses are truly bizarre but they aren't mine and we're forbidden to touch them.

Thanks again.
Reply With Quote
Reply

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
VBA Find inexplicably aborts & goes to top Find where find text contains a double quote norgro Word VBA 1 01-23-2015 10:58 PM
VBA Find inexplicably aborts & goes to top Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
VBA Find inexplicably aborts & goes to top Macro that can find phrase and then find another and copy jperez84 Word VBA 10 09-19-2012 04:48 PM
VBA Find inexplicably aborts & goes to top Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:39 PM.


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