Undo in Excel

Hi all,

I'm doing my first VSTO project for Excel. I need my users to be able to click the "Undo" button even after my code has been touched. In a previous post, Aburstein suggested looking at "application.onundo". This may indeed give me what I need. The problem I'm having is that the arguements to the method require a procedure name that gets executed if the user hits undo. It seems, though, that this procedure must be in a VBA module, not in my VSTO code.

My questions are: (an answer to any one of them may help)

  • Can I create a method in my VSTO code that will be called, so that I don't have to create any VBA
  • Can I create VBA code for my Excel project from my VSTO code.
    • If so, can this VBA code refer back to other VSTO code.
  • Is there another way to handle the undo problem -- see the following:

In a simple case, I want to do some formatting if my user changes a value in a cell. However, I can well imagine that they might incorrectly enter something and want to undo it. In that case all I need to do is to remember the previous value of the cell and replace it if they hit undo.

I'd really appreciate any help or even references or possible directions. I'd also appreciate any suggestions as to a completely different approach if anything comes to mind.

Thanks,

Art




Answer this question

Undo in Excel

  • Larza

    John,

    Thanks for the response -- I had about given up. By OM do you mean the Object Model

    The formatting, for now, is just shading. It responds to a change cell event. When my code is triggered by the event, the undo button becomes disabled.

    I saw something in some documentation that said that undo becomes disabled when any code is executed. However, based on your answer it seemed that this might not be true. I just tried some other simple code and it does appear that I can do some things and the undo stays enabled.

    The specific thing I was trying was to shade the row that the entry was made in. This disabled the undo button.

    It also appears that I do have a lot more to try out and to learn.

    -----------------

    As long as you were foolish enough to answer me, permit me to ask another question. In trying to solve this problem I thought that I could write a VBA macro into the workbook from my VSTO code. I could not figure out a way to do that. Finally I tried a "send keys" desperation move. I wound up with some unstable projects that would occassionally lock up. Do you have any thoughts on that That is, either how to write a VBA macro into the workbook

    ------------------

    Thanks again for taking the time to look at my question.

    Art



  • Jonathan C.

    Hi Art,

    Usually, when VSTO does things with OM, each OM call gets recorded on the undo stack and can then be "undone" just like any user change. What kinds of formating changes you are making Are these changes causing the undo stack to become invalid (is the undo button disabled)

    Thanks,

    John Shepard


  • SAVAJX1

    In your situation, would it work to build your project based on an existing Excel document that already includes your macros

    -- Kirk Hilse


  • John Lynch

    Kirk,

    Thanks for the suggestion. Yes, it would definitely be possible, and it's likely I will do that or do the same thing with a new Excel document. I just wanted to have as few steps as possible and it seemed like something that shouldn't be that hard.

    Art



  • Undo in Excel