Consulting

Results 1 to 19 of 19

Thread: Word Crashes with VBA UndoRecord in Macro

  1. #1

    Word Crashes with VBA UndoRecord in Macro

    Hi,
    Why Word crashes when I run this macro?

    Sub test()
    
    
        Dim objUndo As UndoRecord
        Set objUndo = Application.UndoRecord
        objUndo.StartCustomRecord ("Selection.Find")
    
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "A"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    
    
    End Sub

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Perhaps there is a fault in your Office installation - it doesn't crash when I run your macro.

    Have you tried repairing the Office installation (via Start > Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change)?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thank you
    I did a repair installation, but Word still crashes
    This is a Word 2010
    I also checked in Word 2013 and it crashes

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    I tested with Word 2010. The macro ran fine and didn't crash. Conclusion: Your Office installation is faulty. If repairing Office didn't fix things, perhaps a 3rd-party Addin installed on your system is to blame.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    There is progress in the study of problem

    Word crashes in this line

        Selection.Find.Execute Replace:=wdReplaceAll
    Now I added the line highlighted
    and Word NOT crashes

    Sub test()
    
        Dim objUndo As UndoRecord
        Set objUndo = Application.UndoRecord
        objUndo.StartCustomRecord ("Selection.Find")
    
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "A"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.TypeText "S"
        Selection.Find.Execute Replace:=wdReplaceAll
    
    
    End Sub

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    All that does is overtype you selections with 'S', as a result of which the Find cannot possibly find anything in the selected range... You haven't actually fixed anything!
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    It also does not crash

    Sub test()
    
        Dim objUndo As UndoRecord
        Set objUndo = Application.UndoRecord
        objUndo.StartCustomRecord ("Selection.Find")
    
    
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "A"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.TypeText "A"
        Selection.Find.Execute Replace:=wdReplaceAll
    
    
    End Sub

  8. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    You are just wasting time. As I have already said twice, the original macro does not crash my system, so the problem is with your system. You should fix that instead of playing around with things that cannot fix the problem. Besides which, why do you have:
       Dim objUndo As UndoRecord
        Set objUndo = Application.UndoRecord
        objUndo.StartCustomRecord ("Selection.Find")
    There is no obvious reason for this code, since it's not used for anything.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    My conclusion is
    If the Undo list is empty the Word crashes
    But if I type any text, Undo list is not empty and Wort not crash
    And it indicates a bug in Word

    Anyway I do not know where to start looking for a solution

  10. #10
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    863
    Location
    Maybe check the registry for a resiliency item. Hit search then enter RUN. Type regedit. Select HKEY_CURRENT_USER then Software then Microsoft then Office then ("Version number") ie. 11.0 then Word. If there is a resiliency item it will be listed and possibly this may be your problem. If there is no resiliency listed then this is not your problem. HTH. Dave

  11. #11
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Or maybe the OP should:
    a) test whether the Custom Recording is already running; and
    b) set an end point for the Custom Record at some point
    Otherwise, it risks both instantiation multiple records and expanding forever...
    Sub Demo()
    Dim objUndo As UndoRecord
    Set objUndo = Application.UndoRecord
    With objUndo
      If .IsRecordingCustomRecord = False Then
      .StartCustomRecord ("Demo")
      'record whatever here
      .EndCustomRecord
    End With
    End Sub
    As I said, though, it's not apparent why it's even being used. Totally unnecessary for what has been posted.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    Dave,
    I found the key, what do I do with it?

    macropod,
    He still crashes


    Sub Demo()
    
        Dim objUndo As UndoRecord
        Set objUndo = Application.UndoRecord
        With objUndo
            If .IsRecordingCustomRecord = False Then
                .StartCustomRecord ("Demo")
    
                'record whatever here
                Selection.Find.ClearFormatting
                Selection.Find.Replacement.ClearFormatting
                With Selection.Find
                    .Text = "A"
                    .Replacement.Text = ""
                    .Forward = True
                    .Wrap = wdFindContinue
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchKashida = False
                    .MatchDiacritics = False
                    .MatchAlefHamza = False
                    .MatchControl = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                End With
                 ' Selection.TypeText "A"
                 Selection.Find.Execute Replace:=wdReplaceAll
    
                .EndCustomRecord
            End If
        End With
        
    
    End Sub


  13. #13
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    You still haven't expalined why you're using 'UndoRecord'. As I have already said, it is totally unnecessary for the code you posted. Did you re-start Word after adding the extra code? Have you tried:
    Sub Demo()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "A"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Find.Execute Replace:=wdReplaceAll
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  14. #14
    1. I using 'UndoRecord' because after the 'Find.Execute' I want to do a lot of actions,
    And I want the user to be able to cancel it all with a single click 'Undo'
    But just for 'Find.Execute' Ward crashes

    2. I m
    ust re-start Word because he crashes

    3.
    your code excellent but I must the 'UndoRecord'

  15. #15
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    863
    Location
    If you found a Word resiliency item it means that a Word file has had a "serious error". If you try to open the file with VBA your code will crash. The file must be manually opened and "yes" must be selected to remove the error. You may also just be able to select and delete it from the registry within regedit but I haven't been brave enough to try. There is also the deletesubkeytree VBA that might remove the item but again I haven't trialled it. Not sure if this has anything to do with your error but it seems Word is a funny thing and having a resiliency item listed does mean that a corrupted file does exist somewhere. Good luck. Dave

  16. #16
    This bug is still exists. And in Office 2013 too. You need the empty history in word and Selection.Find.Execute Replace as the first action with document after StartCustomRecord to reproducing.
    Any action between Selection.Find.Execute Replace and StartCustomRecord, that is saved in history, is workaround. for example, Selection.LanguageID = wdEnglishUS

  17. #17
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    AndTim,

    Please so us how you would write the OP's code. I am sure it would help may of our new to VBA readers.
    Thanks
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  18. #18
    This example crashes Word (only newly opened and/or with empty undo history):
    Sub testUR()
        Dim ur As UndoRecord
            Set ur = Application.UndoRecord
            ur.StartCustomRecord ("Undo")
    '    Selection.LanguageID = wdEnglishUS
            With Selection.Find
                    .ClearFormatting
                    .Replacement.ClearFormatting
                    .Wrap = wdFindContinue
                    .Text = "any text"
                    .Replacement.Text = "any replacement text"
                    .Execute Replace:=wdReplaceAll
            End With
        ur.EndCustomRecord
    End Sub
    With uncommented line this example NOT crashes Word.

  19. #19
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Thanks.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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