Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA-Word bug: Undo list get error if use UndoRecord to record "Replace All" while there is a "Apply Quick Style" in the undo list. How to avoid?

On Word window, do something like typing, format font, paragraph... to ensure the undo list is not empty, and then change style of some text by clicking any Style on Ribbon. An entry named "Apply Quick Style" appear in the undo list. Then run macro like:

Sub SampleMacro()
Dim myUndoRecord As UndoRecord

Set myUndoRecord = Application.UndoRecord
myUndoRecord.StartCustomRecord ("VBA - Format Text")
'I do a lot of step here, but for this example, just simple like below
Selection.Characters(1).Bold = True     'just for example
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "Find Text"
    .Replacement.Text = "Replace Text"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
'Word undo list get error after below step
Selection.Find.Execute Replace:=wdReplaceAll
'no crash, no error message, but the entry "Apply Quick Style" 
'become to "Replace All", and Word can't go back before that entry
myUndoRecord.EndCustomRecord
End Sub

After this line of code:

Selection.Find.Execute Replace:=wdReplaceAll

The entry named "Apply Quick Style" in the undo list will become to "Replace All" and I can't undo (by press Ctrl-Z, or click arrow button on Quick Access Toolbar) to go back any step before that entry "Replace All". It's allway appear in undo list and Word will not go back anymore. How to avoid this bug? I'm using Word 2016 pro 64 bit

Additional information: using copy paste by press Ctrl+C, Ctrl+V (from other document to current document) instead of "Apply Quick Style" also get error, the entry "Paste" in undo list also rename to "Replace all". The different is still able to go back before that entry. Maybe there is another way will get error if use UndoRecord to record "Replace all".

like image 618
Mr.Tien Avatar asked Oct 12 '25 21:10

Mr.Tien


1 Answers

Update 10/01

A working work around

The problem is specific to wdReplaceAll. If that specific wdConstant is omitted or replaced then "Apply Quick Style" will not be renamed and the undo stack remains accessible.

Lucky for us, Find.Execute returns a Boolean value (True for success). That means, we can loop with wdFindOne to replace all matches and use .Execute = False as the exit condition.

Add the word Do above your With block and replace the line with .Execute.

Do
    With Selection.Find
         [....]
    End With
Loop While Selection.Find.Execute(Replace:=wdReplaceOne)

A word of caution: some circumstances will create an infinite loop (such as replacing "A" with "A"). As such, you should consider using a second exit condition or replacing wdFindContinue with wdFindAsk or wdFindStop.


Update 9/30

(Edit 10/01) Oops!! The Apply Quick Style entry was not renamed (good) but I wrongly assumed the undo stack could be reached when the same limitation exists (bad). Also, testing today, I learned there is a third condition: .Execute must not find any matches (worse). Suffice it to say, this is an exemplary demonstration of how not to test a solution, I hope everyone learned thier lesson!

When I create a new document and follow your steps, I can consistently reproduce the issue you describe. Thank you for making that easy!

As much as I am able to replicate the problem, I am also able prevent it by meeting two conditions.

  • Ensure Replace All is listed in the Undo Stack above Apply Quick Style.

  • Replace All is applied to the entire document (the problem persists if Replace All is applied to a Selection within the document)

This method worked regardless if it was done manually with 'Ctrl + H' or if it was part of a macro. Replacing a character with the same character was sufficient.


A screenshot showing the execution point after the problematic line:

Can be avoided


The Undo Stack contains an intentionally placed Replace All to preserve Apply Quick Style. This article is for Excel but it's relevant for Word.

https://excel.tips.net/T002060_Preserving_the_Undo_List.html

In short, you're on your own.

You have two options: revert to a previously saved version; or, write a macro that mimics Undo and ensure this macro is running before you start the one that messes with the Undo list

like image 86
ProfoundlyOblivious Avatar answered Oct 14 '25 15:10

ProfoundlyOblivious