Excel add-in with seamless Undo: possible?
I am considering implementing an Excel add-in using COM (not VBA) that will manipulate data in the worksheet.
I am going to need this add-in to seamlessly integrate with the Undo stack. More specifically:
- Any changes this add-in makes to the data need to be undoable by the user (through the standard Undo action)
- The items on the Undo stack before the add-in action takes place need to be preserved
In my (albeit perfunctory) research so far, it is unclear whether or not Excel can allow for this. If it does not, this is a showstopper, and the add-in will have no value.
My question: is it possible? This is more of a "yes or no" question than a "how" question, as I need to know if I am embarking on a wild goose chase or not. However, any pointers on how it can be done would be a bonus.
Seems like it is not possible after all.
The Application.OnUndo method clears the current undo stack and places itself on top. There doesn't seem to be anything else related to customizing undo.
It is possible.
Application.OnUndo registers an undo sub for the currently executing sub:
sub ImMakingChanges() cells(1,1).interior.color = vbyellow application.onundo "Undo the stupid color", "RemoveMyStupidChanges" end sub sub RemoveMyStupidChanges() cells(1,1).interior.colorindex = xlnone end sub
Obviously, saving a previous state in the wild is generally a nightmare. But here you go.
Also, your undo sub will need to be visible to the general public so that Excel can find and call it.
Apparently, this is not possible and there are no plans to support it even in Office 2013, as per the response by an MSFT employee on this thread.
Seems like it is possible after all
This can be done, but you'll have to write a pretty involved Undo handler. Here's a link that well tell you exactly how: http://www.jkp-ads.com/Articles/UndoWithVBA01.asp
Fortunatly the link has full sourcecode and it looks ironclad so you should have no problems. I'll be happy to hear how/if this worked for you.
PS: here's the same link in Dutch: http://www.jkp-ads.com/Articles/UndoWithVBA01NL.asp