none
Run Macro on Publish RRS feed

  • Question

  • I would like to run a macro in project when the file is published.

    I have a series of projects stored in enterprise server and each time I publish one of the plans, I would like that to trigger a macro to also run. The macro happens to save the timline as an image but that is not the important bit.

    Is there an action like "Private Sub Project_BeforeSave(ByVal pj As Project)" but for Publish? It doesn't matter if it is before or after publish as long as it is triggered at the same time

    Matt

    Monday, July 6, 2015 1:15 PM

All replies

  • Yes, I think it's:

    Private Sub ProjectBeforePublish(pj, Cancel)
        
    End Sub

    or might be:

    Private Sub Project_BeforePublish(pj, Cancel)
        
    End Sub

    See https://msdn.microsoft.com/en-us/library/ff863393.aspx

    pj is the Object for the project being published. Set Cancel=True to stop the publish.


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Monday, July 6, 2015 9:47 PM
    Moderator
  • How would I reference that to kick off a separate module?
    Wednesday, July 8, 2015 3:22 PM
  • Private Sub Project_BeforePublish(pj, Cancel)
        MyOwnSub
    End Sub

    Just call the Sub. Put the sub in its own module.


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Thursday, July 9, 2015 1:22 AM
    Moderator
  • I can't get it to work. I even tried

    Private Sub Project_BeforePublish(pj, Cancel)

    MsgBox ("hello world")

    End Sub

    Didn't get anything pop up. I am using MSProject 2013 if that makes a difference?

    Thursday, July 9, 2015 9:25 AM
  • You had the macro in the ThisProject file? If so, then I can't help you, I don't have a Project Server instance readily available at the moment.

    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Friday, July 10, 2015 2:23 AM
    Moderator
  • I have the macro I want to run in a separate module first line of which is  'Sub Sniptimeline()'

    In the 'ThisProject' file I have

    Private Sub Project_BeforePublish(pj, Cancel)

    Call Sniptimeline

    End Sub

    When I publish the file, it does just that, no macro running

    Friday, July 10, 2015 7:39 AM
  • BeforePublish was introduced for Project 2007 and no indication it has been removed. I don't have an instance readily available to test.

    Try adding a breakpoint on the call statement to see if anything happens.

    I have made it work in the past, so keep experimenting. Have you been able to get a beforesave event work?


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Friday, July 10, 2015 11:09 PM
    Moderator
  • Matty Boy,

    Pardon me for jumping in but I wonder if you have set up the Event procedure correctly. Except for a few select Events, just dumping them into the ThisProject object under VBAProject doesn't work. Most Events require setting up a separate class module and a few other things. If you haven't done that, you will get the exact response you describe, that is, no response.

    If you not familiar with setting up an Event procedure, take a look at the VBA help article on "Using Events with Application and Project Objects".

    Or, Jack Dahlgren created a website several years ago that lays out instructions on how to set up Event procedures along with several example of useful Project macros. It's a bit dated but I think it is still very useful: http://masamiki.com/project/macros.htm

    Just a thought.

    John

    Saturday, July 11, 2015 3:52 PM
  • Hi John,

    The BeforePublish is a Project event that can just paste into the ThisProject file. Its only Task events that need the extra class file.


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Monday, July 13, 2015 8:12 AM
    Moderator
  • Rod,

    Okay, thanks for the clarification. It just seemed like it might be relevant.

    John

    Monday, July 13, 2015 3:08 PM
  • Thank you for helping. I can't get it to work so I'll try a different Route

    Thank you

    Matt

    Thursday, July 23, 2015 7:11 AM