none
MS Project 2016 - VBA exception / Run-Time error '424' RRS feed

  • Question

  • Hello,

    i am using MS project 2016 with a Macro which seems to work but i start using it with sub-project and sometime, there is an exception when i open the project's file. It is a simple programme as you can see below. It is strange behaviour, as when i open the project as double clicking on the icon, this exception do not occurs. However, when i open MS Project, browse to the file and open it, the exception occurs !!! Also, i have remove the external link and the exception do not occurs any more...

    Thursday, October 26, 2017 1:07 AM

Answers

  • Since you're running this in an event handler (or two) in which you have the project being calculated as a given parameter (pj)

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete
    End Sub

    pass that parameter to the PercentWorkCompleteToPhysicalPercentComplete and replace ActiveProject with the actual project.

    Modify PercentWorkCompleteToPhysicalPercentComplete  thus:

    Sub PercentWorkCompleteToPhysicalPercentComplete(pj as project)
        If Not pj Is Nothing Then
            Dim Tsk As Task
            For Each Tsk In pj.Tasks

    ... etc

    and call it like this:

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    Remark: "If Not pj Is Nothing Then" - I've found that sometimes even though an event handler passes an object to you, the object might still be null/nothing sometimes...

    /Lars Hammarberg


    //Lars Hammarberg www.oneagency.se

    Tuesday, October 31, 2017 6:18 PM
  • Thank. Sorry for the delay.  I have done this : And it is working fine. Great. Thank you very much

    Just one question. Do i still need to pass object by value in the object (bold below).

    1---------------- There is a class module

    Public WithEvents Project As Project

    Private Sub Project_Calculate(pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    2--------------- There is an object

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    3------------ There is a module

    Sub PercentWorkCompleteToPhysicalPercentComplete(pj As Project)
        If Not pj Is Nothing Then
            Dim Tsk As Task
            For Each Tsk In pj.Tasks
                If Not Tsk Is Nothing Then
                    If Tsk.Summary = False Then
                        If Tsk.ExternalTask = False Then
                            If Tsk.Active = True Then
                                Tsk.PhysicalPercentComplete = Tsk.PercentWorkComplete
                            End If
                        End If
                    End If
                End If
            Next Tsk
            '- Affichage d'un compteur dans la barre de statut
            Static count As Integer
            count = count + 1
            Application.StatusBar = "Percent WorkComplete To Physical Percent Complete " + CStr(count)
        End If
    End Sub
    Saturday, November 4, 2017 10:06 PM
  • Hello,

    i want to run this macro each time the project need to be calculated (and when i press the calculate button). This is what the macro is doing (i use it for many year). I started using external links last month. But when i try to use link between project, the problem is described occurs. 

    I use VBA because i have not the choice but i want to know as least as possible about it. The solution "Lars Hammarberg" gave me works just fine. The problem do not occurs any more and the macro is running each time the project need to be calculated and when i press the calculated button. 

    So i am happy.

    Monday, November 6, 2017 2:01 AM

All replies

  • Unlike an empty task line, you can't directly test an activeproject object that doesn't exist yet.  Notwithstanding the rest of your code, I would fix this by testing indirectly through an error-handler something like this:

    Dim p As Project
    
    On Error Resume Next
        p = ActiveProject
        If Err.Number <> 0 Then
            On Error GoTo 0
            'proceed with code
        Else
            On Error GoTo 0
            'bypass code
        End If

    No doubt there are other, more-efficient approaches.

    Good luck, tom


    Thursday, October 26, 2017 2:36 PM
  • Rolle,

    You mention something about a subproject. It would help to understand your file structure. Are you running the macro on a master file with inserted subprojects or on individual subprojects, or what?

    And how are you initiating the macro? You mention double clicking on the icon or browsing and opening. Please expand on that.

    On the surface it appears that the exception is occurring because the file cannot be found possibly due to a linked structure.

    For reference, when including a screen shot, please grab only the relevant part of the screen. Full screen shots are too small to provide useful information.

    John

    Thursday, October 26, 2017 4:21 PM
  • Hello, 

    Sorry for the delay. 

    i am running the macro on a individual subproject (which as a dependency to a milestone of a master project - which embedding this subproject-).

    The macro is initiated at startup i think (because exception occurs when i open the project). Here is the code (the purpose is to copy PercentWorkComplete to PhysicalPercentComplete for each task).

    1---------------- There is a class module

    Public WithEvents Project As Project

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete
    End Sub

    2--------------- There is an object

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete
    End Sub

    3------------ There is a module

    Sub PercentWorkCompleteToPhysicalPercentComplete()
        If Not ActiveProject Is Nothing Then
            Dim Tsk As Task
            For Each Tsk In ActiveProject.Tasks
                If Not Tsk Is Nothing Then
                    If Tsk.Summary = False Then
                        If Tsk.ExternalTask = False Then
                            If Tsk.Active = True Then
                                Tsk.PhysicalPercentComplete = Tsk.PercentWorkComplete
                            End If
                        End If
                    End If
                End If
            Next Tsk
            '- Affichage d'un compteur dans la barre de statut
            Static count As Integer
            count = count + 1
            Application.StatusBar = "Percent WorkComplete To Physical Percent Complete " + CStr(count)
        End If
    End Sub

    The exceptions occurs because  in line "If Not ActiveProject Is Nothing Then" the value of "ActiveProject" is "Object required"



    Saturday, October 28, 2017 8:35 PM
  • When i open this subproject within MS Project (open,....) the exception always occurs

    however, when i use file explorer and double clicking on the subproject file, then the subproject open correctly, without exception. Moreover, i can see in the status bar that the macro has run once....

    Saturday, October 28, 2017 8:47 PM
  • Rolle,

    It's been a while since I worked with Event macros but based on your last post I created the following test case.

    You indicated that the project of interest is a subproject in a dynamic master with an external link from master to subproject. It's unclear from your post which way the link goes (i.e. master to subproject or subproject to master) but for this example I chose the milestone in the master as the external predecessor to a task in the subproject, as shown in the first screen shot (split view showing both master and subproject).

    Although not explicitly stated in your post, it sounds like you want to run the macro anytime the subproject file is opened. Since the Open Event is a Project event, it doesn't need a class module, regardless of whether the subproject is opened from the Project ribbon or directly from Windows explorer (i.e. Project not running). So, this is the basic macro without the extra stuff. It runs no matter how the subproject is opened, however, it will NOT run when the master is opened even though the master opens the subproject, in the background.

    Hope this helps.

    John


    Sunday, October 29, 2017 11:39 PM
  • Since you're running this in an event handler (or two) in which you have the project being calculated as a given parameter (pj)

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete
    End Sub

    pass that parameter to the PercentWorkCompleteToPhysicalPercentComplete and replace ActiveProject with the actual project.

    Modify PercentWorkCompleteToPhysicalPercentComplete  thus:

    Sub PercentWorkCompleteToPhysicalPercentComplete(pj as project)
        If Not pj Is Nothing Then
            Dim Tsk As Task
            For Each Tsk In pj.Tasks

    ... etc

    and call it like this:

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    Remark: "If Not pj Is Nothing Then" - I've found that sometimes even though an event handler passes an object to you, the object might still be null/nothing sometimes...

    /Lars Hammarberg


    //Lars Hammarberg www.oneagency.se

    Tuesday, October 31, 2017 6:18 PM
  • Thank. Sorry for the delay.  I have done this : And it is working fine. Great. Thank you very much

    Just one question. Do i still need to pass object by value in the object (bold below).

    1---------------- There is a class module

    Public WithEvents Project As Project

    Private Sub Project_Calculate(pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    2--------------- There is an object

    Private Sub Project_Calculate(ByVal pj As Project)
        PercentWorkCompleteToPhysicalPercentComplete pj
    End Sub

    3------------ There is a module

    Sub PercentWorkCompleteToPhysicalPercentComplete(pj As Project)
        If Not pj Is Nothing Then
            Dim Tsk As Task
            For Each Tsk In pj.Tasks
                If Not Tsk Is Nothing Then
                    If Tsk.Summary = False Then
                        If Tsk.ExternalTask = False Then
                            If Tsk.Active = True Then
                                Tsk.PhysicalPercentComplete = Tsk.PercentWorkComplete
                            End If
                        End If
                    End If
                End If
            Next Tsk
            '- Affichage d'un compteur dans la barre de statut
            Static count As Integer
            count = count + 1
            Application.StatusBar = "Percent WorkComplete To Physical Percent Complete " + CStr(count)
        End If
    End Sub
    Saturday, November 4, 2017 10:06 PM
  • Rolle,

    I'm sorry but I'm still lost on what exactly you are trying to do. Just like the Open Event code I suggested earlier, the Calculate Event is a Project level event and therefore doesn't need a class module to run it. If you substitute "Calculate" for "Open" in the macro code I suggested, instead of firing on file open it will fire any time a change is made that will cause a re-calculation. It doesn't matter if the file is opened from Project (i.e. File > Open) or from Windows Explorer, which seemed to be the issue you were having.

    John

    Sunday, November 5, 2017 8:45 PM
  • Hello,

    i want to run this macro each time the project need to be calculated (and when i press the calculate button). This is what the macro is doing (i use it for many year). I started using external links last month. But when i try to use link between project, the problem is described occurs. 

    I use VBA because i have not the choice but i want to know as least as possible about it. The solution "Lars Hammarberg" gave me works just fine. The problem do not occurs any more and the macro is running each time the project need to be calculated and when i press the calculated button. 

    So i am happy.

    Monday, November 6, 2017 2:01 AM
  • Rolle,

    I'm glad it is working for you however, since Lars is the one who answered your question, you might consider giving him the credit instead of marking your response as the answer.

    John

    Monday, November 6, 2017 2:11 PM