• w2k Crash After VBA Selection / Ranges Searches (w2k / VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » w2k Crash After VBA Selection / Ranges Searches (w2k / VBA)

    Author
    Topic
    #369019

    Dear Loungers

    I have one macro that searches through a document for markers (<>), finds what is in between the markers, then displays the text between the markers as a message to an ‘end user’. I have called this macro PauseAndPrompt. This macro uses the Selection object to perform searches, deleting text, etc.

    I have another 2 macros that are used in other parts of my system. The first of these is called SrchRep – it has a search string and replace string – both of which are passed into this procedure by the second macro. It then uses a ‘For Each’ loop to cycle through all ‘story ranges’ of a document and performs a search and replace of each relevant story range using the variables that are passed to this routine.

    The third macro simply calls the SrchRep macro and passes it valid parameters. It calls the SrchRep macro twice.

    All macros function perfectly well, except if I launch the first pause and prompt macro, immediately followed by the third macro (which in turn calls the srchrep macro), the first call to srchrep successfully completes, but the second call makes word crash.

    Any assistance would be greatly appreciated. I have been tearing my hair out with this one. Text file with sample code is attached.

    Thanks
    Cathy

    Viewing 1 reply thread
    Author
    Replies
    • #580083

      This part seems curious to me:

      Dim aStory As Variant
      Set aStory = ActiveDocument.StoryRanges

      Try this instead:

      Dim aStory As Range

      I’m curious why the .text are not upper case? Maybe the above will fix that?

      (Frankly I don’t know why you’re getting the crash, but the above might help.)

      • #580095

        Doesn’t work. The problem is not with my code. The crash will only occur if I run the pause and prompt macro followed by the testing macro. The problem seems to be related to the usage of the Selection object in a search and replace, followed by the usage of Story Ranges in a search and replace (same Word session). I am going to try to rework my code so that I use standard Ranges, rather then Story Ranges.

        I was wondering if anyone else has experienced this before? I have had this tested on a variety of operating system. It also fails in Word 97.

        • #580107

          I don’t think it’s just Selection/Range, or this would crash, too.

          Sub Testing()
          Selection.Find.ClearFormatting
          With Selection.Find
              .Text = "Forward"
              .Replacement.Text = "Backward"
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
          End With
          Selection.Find.Execute Replace:=wdReplaceAll
          Stop
          Call SrchRep("Apple", "Orange")
          Stop
          Call SrchRep("Pink", "Blue")
          End Sub
          '---
          Sub SrchRep(strSrch As String, strReplace As String)
          Dim aStory As Range
          For Each aStory In ActiveDocument.StoryRanges
              aStory.Find.Replacement.ClearFormatting
              With aStory.Find
                  .Text = strSrch
                  .Replacement.Text = strReplace
                  .Forward = True
                  .Wrap = wdFindContinue
                  .Format = False
                  .MatchCase = False
                  .MatchWholeWord = False
                  .MatchWildcards = False
                  .MatchSoundsLike = False
                  .MatchAllWordForms = False
              End With
              aStory.Find.Execute Replace:=wdReplaceAll
          Next aStory
          End Sub

          Something else is happening as a result of running the PauseAndPrompt procedure first, something that causes the second ReplaceAll (Pink/Blue) to fail, but I’m not sure why. I certainly get better results if I comment out the “smart copy and paste” portions of PauseAndPrompt. Perhaps there is something buggy in Word there that this has revealed?

          • #580131

            I tried removing the “Smart Copy and Paste”, but Word still froze. I have also tried using standard ranges rather then StoryRanges, but this too froze.

            If SrchRep uses the Selection object rather then ranges, it works, but will not find words in headers/footers. Example:

            Sub SrchRep(strSrch, strReplace)
            Selection.HomeKey Unit:=wdStory
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
            .Text = strSrch
            .Replacement.Text = strReplace
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
            Exit Sub

            End Sub

            Do you know how I can search headers and footers without using ranges?

            • #580366

              It sounds terribly redundant, but you could do something like Range.Select within each StoryRange to do the searches. (I haven’t tried this, but I’m fairly certain it should work.)

    • #580138

      Hi Cathy,

      Probably not germane to the main problem, but could you explain what the Pause and Prompt macro (and Alt + F12) is supposed to do? I’ve tried running it but don’t know what to do next. Alt + F12 doesn’t seem to do anything.

      Also, if you are presenting the text string in a message box to the user, it would be clearer to the user if you enclosed the text string in quotation marks – single quotes would work fine – so they can tell which is the found string and which is just the msg box text.

      Also also, why is the user being required to manually type the text back in? – if you have the text stored in a string, can’t you automatically have the macro reinsert the text? (if that is what the macro is intended to do…)

      Gary

      • #580153

        OK, a little more about what I am doing …..

        These macros form part of a system – one of the main objectives of which is to allow an organisation to maintain their own precedents and standard documents without the need for complex templates that contain VBA code. There is a master template that stores all shared procedures – this is loaded globally. The Alt + F12 key is assigned to the ‘Pause and Prompt’ macro. This macro searches for text between <> and displays this information in a message box. For example, there may be a prompt in a document that reads “<>”. When the user presses Alt + F12 a message will appear on screen that reads “type in name of product and press Alt + F12 to continue”. The insertion point will be at the correct point in the document, ready for the user to type in the variable information. They press the Alt + F12 key again, which relaunches the Pause and Prompt macro and this moves to the next point in the document were variable information is required.

        This is one feature of my product, called autoOffice. This system has worked very well for my clients over many years and has proved especially beneficial to my clients when they upgraded from Word 6/95 to Word 97. I am not looking to change what I am doing here.

        There are also a few templates for letter, fax, memo, report. These templates do have associated forms and code and are linked to the shared routines in the master template. Instead of using bookmarks to mark the position of variable information (such as office address, author name, addressee information, etc), I use markers – , , etc. After each of these templates displays a form to request addressee information, and the user has selected their personal author details (there are links to an author database for author info and outlook for addressee info), the srch and replace routine is called. The markers in the document could be in the main body of the document, or in headers or footers.

        Again, I am not looking to change the way that I am doing this.

        If you are interesting in viewing this product, you can visit my website – http://www.streamline-automation.com.au – follow the links to product information and download the autoOffice.exe (self extracting exe).

        Now Gary, if you have read this far, I am sure you are thinking “why did I ask???”

        My problem only occurs when a user has worked through a standard document (using pause and prompt – Alt F12), then runs a letter, memo, fax or report that uses ranges and srch and replace.

        Cathy

    Viewing 1 reply thread
    Reply To: w2k Crash After VBA Selection / Ranges Searches (w2k / VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: