« Microsoft Project Server Infrastructure Update | Main | California Fire Season »

Microsoft Project Undo Levels and Macros

One of the new features of Project 2007 is that it allows more than one level of Undo. This is of great comfort to those of us who have made more than one error while working on a file. It also has a benefit for those who automate microsoft project with VBA or VSTO as typically there are many things done during the execution of a macro. In Project 2003 this often meant that macros were irreversible unless you somehow built your own log of the changes and then went back through it to restore things to their prior state.

Taking advantage of this new capability within a macro is easy. There are two methods you need to know:

Application.OpenUndoTransaction ("name of the transaction")


Use these to bracket the code you are going to run in your macro for example:

Sub renameTasks()
Application.OpenUndoTransaction ("rename tasks")
For Each Task In ActiveProject.Tasks
Task.Name = "foo"
Next Task
End Sub

Then when you click on the undo button, all of the changes within that transaction will be reversed in a single click. This is very valuable when you are making a very large number of changes - for example if you modified all of tasks in your macro.

You might be wondering what the transaction name is used for. It is the name which shows up when you click on the undo button, so make it as descriptive as possible. You could also use it if you want to go back to a certain place. To determine where the place is you can read the list of transactions in the undo list. Here is a code sample which shows a message box with all of the undo transactions. If you run it, you can see what you have done so far. Note that many of the names are not very useful for knowing what has been done as they don't include which task the change was made to. Still they offer enough breadcrumbs to see where you have been:

Sub showUndo()
Dim numUndo As Integer
Dim i As Integer
Dim undoString As String
numUndo = Application.GetUndoListCount
For i = 1 To numUndo
undoString = undoString & Application.GetUndoListItem(i) & vbCrLf
Next i
MsgBox undoString
End Sub

Using the name of your transactions you can find out how far back you need to go to reverse the effects of your code even if the user has performed some steps in between. An example would be something like this:

Sub backToStart()
Application.OpenUndoTransaction ("very start")
'do lots of things to the file
Application.OpenUndoTransaction ("middle")
For Each Task In ActiveProject.Tasks
Task.Name = "foo"
Next Task
End Sub

You would then look through the list of transactions until you find the one with the name you like and then reverse that many transactions. It would be great if you could undo out of order, but I have not found any way of doing that. Just like a stack of cards, you have to take the top one off in order to get to the ones underneath.

There are a few things to be careful about undo. First, be aware of the number of undos. Once a transition drops off the bottom of the list it is gone. Second, a project save will clear the undo list. Third, large numbers of undo may affect performance as all that information needs to be held temporarily. Check it with some samples to see the effects in your environment with the types of changes you are going to make.

  • VBA to VSTO Tutorial Part Two - Adding a Command Bar and Buttons
  • VBA Writing to a text file (MS Project, Excel)
  • MS Project VBA - Trim Function
  • Setting Microsoft Project Level Custom fields using VBA
  • Iterating through Microsoft Project Subprojects
  • Analyze Microsoft Project Resource Usage Data In Excel
  • Using a ComboBox in a Microsoft Project Userform
  • VBA and Visual Basic For ... to ... statements
  • Making the move from VBA to VSTO in Microsoft Project
  • Office VBA for Mac After 2008

  • Comments (3)

    muthu kumar:

    Thanks for the good information. It was very helpful.

    Regards, Muthu

    i always wonder how can i eradicate macros in MS Project, good thing you brought it up.


    Thanks very much for the starter on MS Project event programming... been looking for that for a while but never hit your web site.

    Easy to understand, easy to replicate and I got it to work.

    Bravo for the great documentation work!


    Post a comment

    (Comments are moderated to fight SPAM and will be published after I have a chance to approve them. Thanks for waiting.)


    The previous article is Microsoft Project Server Infrastructure Update.

    The next article is California Fire Season.

    Current articles are in the main index page and you can find a complete list of articles in the archives.

    Creative Commons License
    This weblog is licensed under a Creative Commons License.
    Powered by
    Movable Type 3.34