none
Repeat find hanging after the first loop RRS feed

  • Question

  •  

    I'm trying to get a macro to run in Word 2007 that was working on Word 2003.  The macro hangs after the first loop of a find loop.  Below is the code that was working in word 2003.

                oWD.Activate
                Set rDcm = ActiveDocument.Range
                Set rTmp = Selection.Range
                With rDcm.Find
                    .Format = False
                    .Wrap = wdFindStop
                    .Text = _
                        "???????????????????????""COMMAND START""                            ?EXEC?*""COMMAND END""                              ?EXEC?"
                    .MatchWildcards = True

                    While .Execute
                        Set rTmp = rDcm.Duplicate
                        rTmp.Select
                        Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend
                        Selection.Copy
                        Documents(pp_log).Activate
                        Selection.Paste
                        Call get_pp_data(pp_log, stepper, oWB)
                        Documents(pp_log).Activate
                        Selection.WholeStory
                        Selection.Delete Unit:=wdCharacter, Count:=1
                        .Text = _
                        "???????????????????????""COMMAND START""                            ?EXEC?*""COMMAND END""                              ?EXEC?"
                    Wend
                End With

     

    The oWD is a large text document (>1600 pages) and the text string I'm searching for uses wild cards and is a large number of characters (>255).  The code I pasted searches for the text string and copies it to another document where I extract information using another call procedure.  It then deletes that string from the document and repeats the find in oWD.  This code runs through one loop and then hangs.

    I have changed the .Execute line to Do While .Execute(FindText:=strText, MatchWildCards:=True) but I get the same results.

    Thursday, January 13, 2011 1:35 PM

Answers

  • I stepped through the above loop and it did delete the text in the Documents(pp_log) document.  Where it hung up was at the line

                         Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend

    This is extending the selection down 4 lines and was the only way I knew of doing this.  The text in oWD is just lines of text with no formating so moving the cursor by characters, words or lines was the only way I knew of moving and getting the information I wanted.

    In the call procedure I'm using the selection to extract the information I'm looking for after a successful find.execute.  The text used for the find is common and the information I'm extracting is unique, so I'm moving the cursor a specific number of characters/words after the find.execute.

    Does that make any sense?

     

    Edit:

    The above doesn't seem to be accurate.  I was stepping through the code again and was getting some statistics for the two documents and it seems the oWD document is causing the problem since it keeps giving me "Word is finishing analyzing your document" and this doesn't stop.  I'm not sure what to do when opening the file to prevent this from happening.  I was opening it at a word document and changed it to Format:=wdOpenFormatText but that didn't help.  I  have the following to "help" speed up opening the document (even though I had visible:=false when the code did run in word 2003):

                Application.Options.CheckSpellingAsYouType = False
                Application.Options.CheckGrammarAsYouType = False
                Application.ScreenUpdating = False

    Some statistics of one oWD document shows >360K words.  Opening it outside the macro isn't a problem.

    Edit #2:

    I unchecked "Keep track of formatting" and it seems to be running!

    Thursday, January 13, 2011 6:45 PM

All replies

  • Can you be more specific about what you mean by "hangs", please?

    Do you get an error (if so, what)? Does the code stop execution (if yes, at which line does it quit)? Does it go into an endless loop?


    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 5:02 PM
    Moderator
  • Hi Cindy,

     

    It just seems to go into an endless loop, I'm unable to break when this happens.

     

    Thanks,

    Pete

    Thursday, January 13, 2011 5:48 PM
  • Hi Pete

    That probably means that Delete isn't working correctly? If you step through the macro (F8) and every once and a while look at the result in the Word window, is the search text actually getting deleted?

    Working with Selection is always a bit "iffy" because you can never really be sure the "cursor" is going where you think it should. Working with a Range object is usually more reliable, and it's easier for someone not familiar with the application to follow what's going on.

    I can see why you use Selection in the first instance - that's the only way to move by line. But are these "lines" perhaps also a paragraphs?

    Could you explain, in "human terms", exactly what your code should be doing? I think we can clean out most of the "Activate" and "Select", which should make the code much more reliable (and self-documenting).


    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 6:00 PM
    Moderator
  • I stepped through the above loop and it did delete the text in the Documents(pp_log) document.  Where it hung up was at the line

                         Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend

    This is extending the selection down 4 lines and was the only way I knew of doing this.  The text in oWD is just lines of text with no formating so moving the cursor by characters, words or lines was the only way I knew of moving and getting the information I wanted.

    In the call procedure I'm using the selection to extract the information I'm looking for after a successful find.execute.  The text used for the find is common and the information I'm extracting is unique, so I'm moving the cursor a specific number of characters/words after the find.execute.

    Does that make any sense?

     

    Edit:

    The above doesn't seem to be accurate.  I was stepping through the code again and was getting some statistics for the two documents and it seems the oWD document is causing the problem since it keeps giving me "Word is finishing analyzing your document" and this doesn't stop.  I'm not sure what to do when opening the file to prevent this from happening.  I was opening it at a word document and changed it to Format:=wdOpenFormatText but that didn't help.  I  have the following to "help" speed up opening the document (even though I had visible:=false when the code did run in word 2003):

                Application.Options.CheckSpellingAsYouType = False
                Application.Options.CheckGrammarAsYouType = False
                Application.ScreenUpdating = False

    Some statistics of one oWD document shows >360K words.  Opening it outside the macro isn't a problem.

    Edit #2:

    I unchecked "Keep track of formatting" and it seems to be running!

    Thursday, January 13, 2011 6:45 PM
  • Hi Pete

    Yes, according to http://support.microsoft.com/kb/983028 you've found the issue and the solution.


    Cindy Meister, VSTO/Word MVP
    Sunday, January 16, 2011 8:36 AM
    Moderator
  • Hi Cindy,

     

    I think that did fix my original problem, thanks for your help.

     

    Pete

    Monday, January 17, 2011 5:00 PM