Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word Macro Problem (Word 2000)

    I'm having trouble debugging a Word 2000 macro. What this macro does is to search on certain text in a long document, using wildcards, compare the found text to an array to determine whether it matches certain characteristics, and then insert a list of the matching items at the top of the document. Most of the time this macro works perfectly.

    Occasionally the macro does not find the text in the document EVEN THOUGH THE TEXT IS THERE. It behaves as if the specified text is not present in the document. But, if I then immediately open the Edit/Find dialog box and search for the specified text, Word does find it! And after that, the macro works properly again. (Also, if I use the debugging features to look in the Locals window right after the macro has failed to find the text, I do not see anything that looks wrong. I have also stepped through the macro numerous times and I can't find anything wrong with the sequence/loops/etc.)

    I've been trying to determine when this problem happens; as I said, it doesn't happen most of the time. For one thing, it seems to happen after I run a certain other macro. I added to that macro (which finds styles) to make sure that all find specifications are restored to the default at the end of it, but that didn't help. (The only thing that's different about this other macro is that it was converted from WordBasic, whereas the macro that I'm trying to debug was written in VBA.)

    As I understand it, Word does not retain any information such as variable contents etc. after it has run a macro, so I don't understand what could be affecting my macro. The two macros do not have any identically named variables. The two macros are each in their own module. I tried copying one of them into the other module, so they were both in the same module, but that didn't make any difference.

    Have anyone ever had an experience similar to this? What factors could possibly be involved in such a problem? What could make a macro not find text when it's there and all the find specs are right? I've already debugged three other problems in this macro, and it works perfectly when it does work. I don't know where to look next. Any ideas would be appreciated.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Macro Problem (Word 2000)

    You state that you make sure that all specifications are restored to their default settings at the end of the other macro, but just to exhaust this possibility: try

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    somewhere near the beginning of the problematic macro.

  3. #3
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    Hi Hans,

    Yes, I already have that in both macros.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Macro Problem (Word 2000)

    Random thoughts:

    Are you using Selection.Find or Range.Find?

    Are you collapsing the Selection after getting a hit, and/or redefining the Range?

    Are headers or footers part of the problem?

  5. #5
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Word Macro Problem (Word 2000)

    Is the text, by any chance, in a footer? There is a bug that was at least known in Word 97 that an automated find would not look in certain "stories" in the document, such as the header or footer. Even though you could use the actual feature and find them immediately.

    Kim

  6. #6
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    I am using Selection.Find. After each search, the macro starts again from the top of the document, which I believe resets the selection. No, I'm not searching headers or footers.

  7. #7
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    No, the text is not in a footer, and I don't need to search headers/footers, just the main body of the text.

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Word Macro Problem (Word 2000)

    What is your code defining as the object to search? Is it the active document. If your document loses the focus then the macro won't be searching in the right document.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #9
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    How would my document "lose the focus"?

  10. #10
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Word Macro Problem (Word 2000)

    The document could lose the focus if you have multiple documents open at once and another is made active (or a file is opened by the code). When this could happen depends on when you run the code that fails.

    Are you in the VBA area or looking at the file while it runs? How are you testing it?

    If this other code being run before-hand causes it, what document is left active when it has finished? Do you do anything to the file before you then run the code that fails or is it run immediately after by more code.

    You may need to post your code to give us a better idea of what might be going wrong.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #11
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    Neither macro changes from the current document to any other document. I start out in one document and run both macros in that same document. I mostly test the macro from Word, but sometimes run it from VBA when I want to step, or break, etc. I can reproduce the problem by running the Other Macro first, then running the Misbehaving Macro. The rest of the time, Misbehaving Macro works fine.

    The plot thickens. Apparently there are some bugs having to do with wildcard searches. Someone has sent me this link, although I haven't been able to get the code on that page to work for me:

    http://www.mvps.org/word/FAQs/MacrosVBA/FlushFR.htm

    However, if I manually search on ^p after running Other Macro and before running Misbehaving Macro, Misbehaving Macro works again.....

  12. #12
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Word Macro Problem (Word 2000)

    Does searching for ^13 instead of ^p solve the issue. This should be used if you are wildcarding with returns. You should finish up with a replace of the ^13 with a ^p (no wildcards) when done.

    If Bill Coan says there are bugs, then there will be bugs. I have had problems in this area in the past but its been so long that I can't remember what the solution was - I think it was to use ranges instead of selection objects.

    Having now looked at the link you referenced, which part of that code is not working? Did you know that you needed to get the other code called in the code that doesn't appear on that page.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #13
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    Truly a Strange Case... I cannot get either of the macros from the MVP articles to work *consistently* (meaning that the macros run, but don't solve my problem). Meanwhile, I have at least figured out how to get my macro to differentiate between when it can't find wildcards and when none of the specified text is in the document, so it gives me accurate messages about what's happening. But, when the macro can't find wildcards, if I bring up the Find dialog box from a macro, as in either of the following examples, and search once, after that my macro works properly again. (These are basically what those two macros are doing in another way..)

    With Dialogs(wdDialogEditFind)
    .Find = "^p"
    .PatternMatch = False
    .Format = False
    .Show
    End With

    With Dialogs(wdDialogEditFind)
    .Find = "?"
    .PatternMatch = True
    .Format = False
    .Show
    End With

    Margaret

  14. #14
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Word Macro Problem (Word 2000)

    You are not using the search and replace, just the find. Does this mean that you are interacting with the macro rather than just doing bulk replacements?

    Looking once more at Bill Coan's wording at the top of his page "There is a bug in Word that means any Find or Find or Replace operation in VBA that follows an unsuccessful wildcard search may sometimes fail. The following code fixes this problem. " makes me wonder about using a .Found = True to reset this value before performing the search.

    I need to see the full code to try to work out what you are trying to achieve and to test the possibilities because it has been so long since I have seen this problem I cant remember the steps to reproduce it.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  15. #15
    New Lounger
    Join Date
    Feb 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro Problem (Word 2000)

    <P ID="edit" class=small>(Edited by gwhitfield on 02-Mar-03 17:58. Hyperlinks added)</P>No, I'm not interacting with the macro. Both macros find either certain text or certain styles, test them for various characteristics, and add text to the document based on the results. The macro that causes the other macro not to work uses "RepeatFind" many times, so I think my problem is described on the second page, not the first one that you were looking at:
    http://www.mvps.org/word/FAQs/MacrosVBA/Fl...tialFindBug.htm
    As I said in my last message, when the problem happens, what's actually happening is that a wildcard search RUN FROM VBA will not work (though it will work if executed by hand from the dialog box!). Somehow when you bring up the dialog box and search on almost anything, it must re-initialize whatever has messed up the wildcard search capability. So, all I need to do is have a last segment of the one macro display a find dialog box using .Show and wait for me to execute it. Then my second macro works. I also have added to the code so that if wildcard searches aren't working, my macro will let me know that and thus is now able to differentiate between "wildcard searches aren't working" and "what I'm looking for wasn't found", which is what I needed. I'm sorry that I cannot send you the full code because I did not write the original code and I cannot post it for confidentiality reasons. There's nothing wrong with my code. This is a very reproducible bug, and now that I know what to do for it, I can stop wasting my time on it!!! <img src=/S/smile.gif border=0 alt=smile width=15 height=15> I just didn't realize at first that what was happening was that wildcard searches FROM VBA wouldn't work, because at the same moment, they do work from the dialog box. Ah, the joys of Microsoft. Thanks, Andrew, for your many replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •