none
VBA for ProjectBeforePublish not firing RRS feed

  • Question

  • I cannot get a VBA procedure, ProjectBeforePublish, to fire in Project Pro 2016.  This is part of our Project Online.

    I have written a few simple MS Project Macros, but I have not written any for the ProjectBeforePublish event.


    I am creating a VBA procedure to populate a field right before the Publish happens.


    This is what I have so far in VBA Project (Global(+non-cahched Enterprise))  --> MSoft Project Objects --> This Project:

    *******************

    Private Sub ProjectBeforePublish(ByVal pj As Project, Cancel As
         Boolean)

              'VBA fires before Publish
               

                Dim UpdateCount As Integer: UpdateCount = 0
               MsgBox "Project Before Publish Started"

               If UpdateCount = 0 Then
                     MsgBox "Inside If Statement"
               End If

    End Sub

    ***************************

    I have looked for examples on ProjectBeforePublish, but have not come up with anything tangible.

             


    Thank you in advance !
    Wednesday, May 3, 2017 9:54 PM

Answers

  • Hi John,

    you need to enable Events first, see https://msdn.microsoft.com/en-us/library/office/ff863805.aspx

    For your sample:

    1. Create a new class module and call it e.g. clsEvents
    2. Paste following code in there
    Public WithEvents ProjApp As MSProject.Application
    
    Private Sub ProjApp_ProjectBeforePublish(ByVal pj As Project, Cancel As Boolean)
     'VBA fires before Publish
                
    
                Dim UpdateCount As Integer
                UpdateCount = 0
                MsgBox "Project Before Publish Started"
    
                If UpdateCount = 0 Then
                      MsgBox "Inside If Statement"
                End If
    
    End Sub

     3. Create a module with this code

    Public ProjEvents As New clsEvents
    
    Sub EnableEvents()
       Set ProjEvents.ProjApp = MSProject.Application
    End Sub

     4. Paste the following code in VBA Project (Global(+non-cahched Enterprise))  --> MSoft Project Objects --> This Project

    Sub Project_Open(ByVal pj As Project)
        EnableEvents
    End Sub

    Does that work?
    Barbara

    • Marked as answer by JohnH4 Monday, May 8, 2017 1:41 AM
    Thursday, May 4, 2017 6:40 AM
    Moderator

All replies

  • Hi John,

    you need to enable Events first, see https://msdn.microsoft.com/en-us/library/office/ff863805.aspx

    For your sample:

    1. Create a new class module and call it e.g. clsEvents
    2. Paste following code in there
    Public WithEvents ProjApp As MSProject.Application
    
    Private Sub ProjApp_ProjectBeforePublish(ByVal pj As Project, Cancel As Boolean)
     'VBA fires before Publish
                
    
                Dim UpdateCount As Integer
                UpdateCount = 0
                MsgBox "Project Before Publish Started"
    
                If UpdateCount = 0 Then
                      MsgBox "Inside If Statement"
                End If
    
    End Sub

     3. Create a module with this code

    Public ProjEvents As New clsEvents
    
    Sub EnableEvents()
       Set ProjEvents.ProjApp = MSProject.Application
    End Sub

     4. Paste the following code in VBA Project (Global(+non-cahched Enterprise))  --> MSoft Project Objects --> This Project

    Sub Project_Open(ByVal pj As Project)
        EnableEvents
    End Sub

    Does that work?
    Barbara

    • Marked as answer by JohnH4 Monday, May 8, 2017 1:41 AM
    Thursday, May 4, 2017 6:40 AM
    Moderator
  • Thank you Barbara !!

    This works just as advertised!   Just plug + play !

    I wanted to let you know I really appreciate your posts.  I picked up on another post you had regarding setting the value of a task ECF in a loop.  Another big help.   

    Only problem was I was up until 3AM programming all of this...it was too exciting to put down !

    Thank you again - huge help !

    Monday, May 8, 2017 1:46 AM