## Project: "For Each" Assignments Loop Results in Error

Q125303

The information in this article applies to:

- Microsoft Excel for Windows, version 5.0
- Microsoft Project for Windows, version 4.0
- Microsoft Visual Basic Programming System, Applications Edition, version 1.0

## SYMPTOMS

When you use a For Each loop in an Visual Basic, Application Edition, macro in Microsoft Excel to loop through the assignments for a task or resource in Microsoft Project, you receive the following error message:

> WINPROJ.EXE caused a general protection (GP) fault in module COMPOBJ.Dll...

## CAUSE

This problem occurs only if you run the macro from a Microsoft Excel module.

## WORKAROUND

To work around this problem, use a For...Next loop with an integer variable to index the assignments collection. For example, use a macro in Microsoft Excel similar to the following:

    Sub TestMacro1()
       Dim oMSProject As Object, oAssmt As Object, n As Integer
       Set oMSProject = GetObject(, "MSProject.Application")
       For n = 1 To oMSProject.ActiveProject.Tasks(1).Assignments.Count
           Set oAssmt = oMSProject.ActiveProject.Tasks(1).Assignments(n)
           MsgBox oAssmt.ResourceName
       Next n
    End Sub

## STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

## MORE INFORMATION

The "For Each...Next" statement is a programming control structure in Visual Basic, Applications Edition, that is used to loop through a collection or list. For example, in the Visual Basic macro for Microsoft Excel below, the object variable "oAssmt" loops through the collection of assignments for task 1 of the active project in Microsoft Project, and is used to display the name of each resource assigned to task 1:

    ' This Microsoft Excel macro assumes Microsoft Project is running,
    ' and there is at least one resource assigned to task 1
    ' in the active project.
    '
    Sub TestMacro1()
        Dim oMSProject As Object, oAssmt As Object
        Set oMSProject = GetObject(, "MSProject.Application")
        For Each oAssmt In oMSProject.ActiveProject.Tasks(1).Assignments
           MsgBox oAssmt.ResourceName
        Next oAssmt
    End Sub

NOTE: This code works correctly if it is run from a Microsoft Project module.

## REFERENCES

For more information, see the "For Each...Next Statement" topic in Microsoft Project Visual Basic Reference Help.

Additional query words: VB4

Keywords :
Issue type : kbbug
Technology :

Last Reviewed: November 3, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use.