none
Word 2010: Replace menu within macros applies to the entire document in stead of the selection RRS feed

  • Question

  • Hi!
    I have a grat number of macros in Word that I have used many years. But now when I use them in Word 2010, they behave unexpectedly.

    Example: A code that substitutes manual line breaks with paragraph marks, performs not only on the selection, but on the entire document. This happens with any other replace menu as well.

    Please advise how to get rid of this problem.
    Regards
    MrProper

    MrProper

    Saturday, December 22, 2012 2:03 PM

Answers

  • I haven't experienced this problem.

    I assume that you have code with something like

        With Selection.Find
            .Text = "^l"
            .Replacement.Text = "^p"
            ...
            ...
            .Execute Replace:=wdReplaceAll
        End With

    Make sure that there is a line

            .Wrap = wdFindStop

    above the .Execute line. wdFindStop tells Word not to continue beyond the end of the selection.

    Regards, Hans Vogelaar

    Saturday, December 22, 2012 2:29 PM
  • You are correct.  In Word 2000, 2003 and 2007 a wdFindAsk when used with selected text will first replace in the selection and then ask the user if they want to run to the end of the document and then ask if they want to search from the beginning of the document.

    In Word 201/2013, unless wdFindStop is used, the end of the selection is ignored and the replacement is executed in all text from the selection start until the end of the document.

    I don't know why this change in behaviour occurs, but find it hard to believe and see no reason why it  was deliberate.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by MrProper Saturday, December 22, 2012 6:41 PM
    Saturday, December 22, 2012 3:24 PM

All replies

  • I haven't experienced this problem.

    I assume that you have code with something like

        With Selection.Find
            .Text = "^l"
            .Replacement.Text = "^p"
            ...
            ...
            .Execute Replace:=wdReplaceAll
        End With

    Make sure that there is a line

            .Wrap = wdFindStop

    above the .Execute line. wdFindStop tells Word not to continue beyond the end of the selection.

    Regards, Hans Vogelaar

    Saturday, December 22, 2012 2:29 PM
  • Thank you for your reply, which helps to resolve this problem. But it’s interesting to know that I had the following statement in the code:
    .Wrap = wdFindAsk
    This meant in earlier versions of Word that the menu was first performed only on the selection, ant the user was asked then to continue or not. MS have changed the meaning of the menus and commands, without paying attention that it actually sabotages a lot of existing custom macros.

    Regards
    MrProper

    MrProper

    Saturday, December 22, 2012 2:57 PM
  • You are correct.  In Word 2000, 2003 and 2007 a wdFindAsk when used with selected text will first replace in the selection and then ask the user if they want to run to the end of the document and then ask if they want to search from the beginning of the document.

    In Word 201/2013, unless wdFindStop is used, the end of the selection is ignored and the replacement is executed in all text from the selection start until the end of the document.

    I don't know why this change in behaviour occurs, but find it hard to believe and see no reason why it  was deliberate.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by MrProper Saturday, December 22, 2012 6:41 PM
    Saturday, December 22, 2012 3:24 PM
  • Thank
    you Greg,

    Well, this is about backward compatibility, which is still as important today
    as it always has been. I guess and hope that you have the right to report to
    Microsoft about such matters.

    MrProper

    Saturday, December 22, 2012 6:41 PM
  • Hi all

    I've gotten confirmation that this is a bug. No idea when it will be fixed, or in which versions... So just FYI.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 3, 2013 10:38 AM
    Moderator
  • Hi all

    Following is some VBA code that works around the bug. It asks at the end of the current selection and at the end of the document, if the search should be continued.

    Sub FindWdFindAsk()
        Dim rngSelection As word.Range
        Dim rngFind As word.Range
        Dim txtFind As String
        Dim found As Boolean
        Dim valDocEnd As Long
        
        txtFind = "the"
        valDocEnd = ActiveDocument.content.End
        If Selection.Type <> wdSelectionNormal Then
            MsgBox "No selection available to search"
            Exit Sub
        End If
        'We need to be able to always refer back to the original selection
        Set rngSelection = Selection.Range
        Set rngFind = rngSelection.Duplicate
        'This will let us know where we are in the document
        'We should see these numbers only once
        'if we let Word traverse the entire document
        Debug.Print rngFind.Start, rngFind.End
        
        rngFind.Find.ClearAllFuzzyOptions
        rngFind.Find.ClearFormatting
        'Search the selection in a loop.
        'As wdFindStop is being used, found will be False
        'when Execute reaches the end of the selection.
        Do
            found = ExecuteFind(rngFind, wdFindStop, txtFind)
            If found Then
                Debug.Print rngFind.Start, rngFind.End
                rngFind.Start = rngFind.End + 1
                rngFind.End = rngSelection.End
            End If
        Loop While found
        
        'Now ask the user if the search should continue beyond the selection.
        Dim userInput As VbMsgBoxResult
        userInput = MsgBox("The search has reached the end of the selection. Do you want to continue?", _
                    vbYesNo, "Workaround for wdFindAsk Word 2010/2013")
        'If yes, we let the search continue, this time using wdFindAsk
        'as it still works when the end of the document is reached.
        If userInput = vbYes Then
            Do
                rngFind.End = valDocEnd
                found = ExecuteFind(rngFind, wdFindAsk, txtFind)
                'Once the end of the document is reached, the user will be asked
                'if the search should continue from the beginning of the document.
                'As long as the found Range is not within the original starting point
                '(selection), the search continues. Else the loop is exited.
                If found Then
                    If Not rngFind.InRange(rngSelection) Then
                        Debug.Print rngFind.Start, rngFind.End
                        rngFind.Start = rngFind.End + 1
                        rngFind.End = valDocEnd
                    Else
                        Exit Do
                    End If
                End If
            Loop While found
    End If
    End Sub
    
    Private Function ExecuteFind(rngFind As word.Range, _
                                 wrap As WdFindWrap, _
                                 findText As String) As Boolean
        Dim found As Boolean
        
        With rngFind.Find
            .Text = txtFind
            .wrap = wrap
            .MatchWholeWord = True
            found = .Execute
        End With
        ExecuteFind = found
    End Function


    Cindy Meister, VSTO/Word MVP, my blog


    Thursday, January 3, 2013 2:12 PM
    Moderator