PDA

View Full Version : Word Crashes with VBA UndoRecord in Macro



vbRichard
12-03-2014, 11:38 AM
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

macropod
12-03-2014, 09:13 PM
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)?

vbRichard
12-03-2014, 11:48 PM
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

macropod
12-04-2014, 12:23 AM
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.

vbRichard
12-04-2014, 01:03 AM
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

macropod
12-04-2014, 02:30 AM
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!

vbRichard
12-04-2014, 04:09 AM
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

macropod
12-04-2014, 04:21 AM
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.

vbRichard
12-04-2014, 04:54 AM
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

Dave
12-04-2014, 02:55 PM
:doh: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

macropod
12-04-2014, 03:18 PM
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.

vbRichard
12-04-2014, 03:44 PM
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

macropod
12-04-2014, 06:50 PM
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

vbRichard
12-04-2014, 10:45 PM
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 must re-start Word because he crashes

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

Dave
12-05-2014, 07:23 AM
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

andtim
07-04-2017, 01:45 AM
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

SamT
07-04-2017, 06:33 AM
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

andtim
07-05-2017, 11:59 PM
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.

SamT
07-06-2017, 03:57 AM
Thanks.