none
How to run a macro automatically before saving the project RRS feed

  • Question

  • Hello dear experts,

    I am working with Project Professional and Project Server 2007. I am trying to create a macro that is run automatically before the project is saved. Up to now I am following the advices here (http://masamiki.com/project/project_open.htm) described.

    However, in a collateral discussion in another thread (http://social.technet.microsoft.com/Forums/en-US/projectserver2010general/thread/f8b46b5b-d801-4255-9cf6-47a4750cf665) I have been suggested to avoid any action on BeforeSave if the ActiveProject is the Checked Out Eglobal.

    As I am very new on the macro world, I would appreciate very much your advice on how could I control it. Here is the macro I have up to now:

    Private Sub Project_BeforeSave(ByVal pj As Project)
    If ProjectName = "Checked Out Eglobal" Then
    Exit Sub
    End If
    MsgBox "Test: the Project has just been saved"
    End Sub

    Instead of the MsgBox I will have the action I wish: to run a macro. I have therefore two questions:

    1. Is the grammar of the macro right? Will it exit if the Eglobal is checked out?
    2. What is the instruction to run a macro?

    Any answer will be welcome.

    Fabian

    Monday, January 24, 2011 12:04 PM

Answers

  • Not my day - sorry:

    If there is no active project, it can't check. There isn't any error handling in this piece of code. The minimum in this case is:

    Private Sub Project_BeforeSave(ByVal pj As Project)

    On Error Resume Next ' will skip if, if EGlobal not opened

    If ActiveProject.Name = "Eglobal" Then
    MsgBox "Do not touch Eglobal. Macro will be finished"
    Exit Sub
    End If

    On Error GoTo 0   ' will reenable errors for rest of macro

    Call Unpublish_Tasks
    MsgBox ActiveProject.Name & " has been updated and saved"
    End Sub

    This should work. If not, I have to follow up tomorrow, since I have no access to a project server today, but I will tomorrow.

    Barbara

    Monday, January 24, 2011 3:56 PM
    Moderator

All replies

  • Hi Fabian,

    try it first locally. Don't open Enterprise Global for that. Open VB Editor.

    Put your code into ProjectGlobal (Global.MPT) - Microsoft Project Objects - ThisProject (Global.MPT).
    Replace "Checked Out Eglobal" by any another project name, so your code can by like this:

    Private Sub Project_BeforeSave(ByVal pj As Project)
    If ProjectName = "MyProject" Then
    MsgBox "Macro will be finished" 'you want to know if you are here with this project
    Exit Sub
    End If
    MsgBox "Test: the Project has just been saved"
    End Sub

    Open "MyProject" and any other project and save them. You should see what you expected. If everything is fine, open EGlobal and move your code from ProjectGlobal (Global.MPT) - Microsoft Project Objects - ThisProject (Global.MPT) to VBAProject (Global.... + cache ...) - Microsoft Project Objects - ThisProject (Global.... + cache ...) ).

    By the way: if you are only updating published field for existing tasks, it is not mandatory to avoid any action if you have EGlobal opened (there are no tasks in there ;-). But if you change anything else which could affect EGlobal (perhaps in future), it is easier to prepare your code for this right now. 

    Let us know if you need more input!
    Barbara

    Monday, January 24, 2011 12:41 PM
    Moderator
  • Hello Barbara

    It is nice to see you around ;)

    I have tested the subroutine as you wrote it but it did not work. I mean, the message "Project has been saved" pops up, no matter how. If I change the name "MyProject" for "Whatever" (just to avoid any mistake with the default project name) and I create a project "Whatever", it still goes on and pops the message up when I save the forenamed project.

    About your appreciation on the macro I am trying to install (to update Publish field), I am glad if I do not have to exclude the Eglobal, as it looks like it is not that easy to exclude a project from an automatic run (see above). However, as you also say, it is convinient to know how to avoid changes in the Eglobal, just in case. So, I will continue with the tests.

    What about the second question? How is a macro by another macro called/run? I will order the VBA Programming for Microsoft Office Project 1998-2007 from MSProjectExperts, but it may take long before I receive it...

    Monday, January 24, 2011 1:58 PM
  • Hi Fabian,

    sorry, I missed your second question. To run a macro from within another, you have nearly written it on your own. it is "Call <MacroName>".

    With your fisrt question, I have to apologize. I have checked by typing some code, but I modified yours in my post. Use "If ActiveProject.Name" instead of Projectname .... Sooooorry!

    If it still doesn't work, check the notation for projectname by changing
    MsgBox "Test: the Project has just been saved"
    to
    Msgbox ActiveProject.Name & " Test: the Project has just been saved"

    Good luck!
    Barbara

    Monday, January 24, 2011 2:20 PM
    Moderator
  • Hello Barbara,

    Now it has worked as it should. But when I install the macro in the Eglobal, it shows an error. Here is the present macro:

    Private Sub Project_BeforeSave(ByVal pj As Project)
    If ActiveProject.Name = "Eglobal" Then
    MsgBox "Do not touch Eglobal. Macro will be finished"
    Exit Sub
    End If
    If MsgBox("Do you still want to publish 100% completed tasks?", 260, "Publish all tasks?") = vbYes Then
    Exit Sub
    End If
    Call Unpublish_Tasks
    MsgBox ActiveProject.Name & " has been updated and saved"
    End Sub

    When I have an opened project, all works fine: as I save it, I am prompted to activate the macro or not. If I do, the macro is run. But when I close Project, an Error 424 "Object Missing" appears. When debugging, the If ActiveProject.Name = "Eglobal" Then line is marked. I can finish the macro and then Project is closed.

    Where lays the problem?

    Best regards,

    Fabian

    Monday, January 24, 2011 3:41 PM
  • Not my day - sorry:

    If there is no active project, it can't check. There isn't any error handling in this piece of code. The minimum in this case is:

    Private Sub Project_BeforeSave(ByVal pj As Project)

    On Error Resume Next ' will skip if, if EGlobal not opened

    If ActiveProject.Name = "Eglobal" Then
    MsgBox "Do not touch Eglobal. Macro will be finished"
    Exit Sub
    End If

    On Error GoTo 0   ' will reenable errors for rest of macro

    Call Unpublish_Tasks
    MsgBox ActiveProject.Name & " has been updated and saved"
    End Sub

    This should work. If not, I have to follow up tomorrow, since I have no access to a project server today, but I will tomorrow.

    Barbara

    Monday, January 24, 2011 3:56 PM
    Moderator
  • Hi Barbara,

    do not apologize, I am learning a lot ;-)

    Grüsse

    Fabian

    Monday, January 24, 2011 4:04 PM