A question for the gurus. I have been using the now classic VBA Undo Group tricks on most of my Macros with excellent results. Like this:
Sub boostStart(Optional unDo$)
On Error Resume Next
'Thanks for the cursor, Shelby!
If Len(unDo) Then ActiveDocument.BeginCommandGroup unDo
Optimization = True
EventsEnabled = False
Sub boostFinish(Optional ByVal endUndoGroup As Boolean = False)
On Error Resume Next
Optimization = False
EventsEnabled = True
If endUndoGroup Then ActiveDocument.EndCommandGroup
And it works great. But I have run into a bit of trouble if another macro also uses it. For example, a commercial one that is locked up, but allows for calling its functions.
So if I have my own macro that does something to the document, then calls the outside macro that seemingly also calls "Optimization" and or "ActiveDocument.EndCommandGroup" the Undo becomes a mess with some parts being nicely grouped, but others not. Is there some other way to create an undo that works like a straight up copy of the document before an action and then returning to that point of necessary? Sure, saving works, but the risk of forgetting that (user error) is always a concern.
Thank you in advance.
So, do not use this way (involving two subs), anymore.
Start the Sub with `ActiveDocument.BeginCommandGroup "DoingSomething"` and end it with `ActiveDocument.EndCommandGroup`
You can use them only for optimization, if you want proceeding in this way.
Thanks for the turbo reply.
I get that, but a function (that cannot be edited, because it is third party and locked) that is called between these two has its own "EndCommandGroup" and "Optimization = False" inside. So what I need is somehow grouping all those commands (several BeginCommandGroup and EndCommandGroup together).
Actually, to be more precise, a couple of Undos would not hurt too much, as long as each function has one parent step and not each step inside a function after another calls the EndCommandGroup and disables optimization.
Since `ActiveDocument.EndCommandGroup` is the same for all of them, how VBA to know at what procedure to refer? It acts correctly only when the respective sub runs this line, without interfering another `ActiveDocument.BeginCommandGroup`...
I get that. Just looking for some way to work around it to avoid a mountain of undos, if possible.
I have run into this also. I normally have chosen not to do the undo since I have no control over the other code. I just do not want them conflicting.
I wish I have found a more graceful way to handle it.