Results 1 to 5 of 5

Thread: [Resolved]MS Project VBA, the red-headed stepchild

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Location
    Florida
    Posts
    12

    Resolved [Resolved]MS Project VBA, the red-headed stepchild

    I'm writing code that takes cell strings in Excel and attempts to match them with resource codes in MS Project. It's a pain and there is very little information I could find that helps. If anyone could give me a link of a forum that deals with MS Project I would really appreciate it. Here's a sample of my chicken scratch play code just to get anything to populate.
    VB Code:
    1. Dim ObjTask As String, SourceTask As Task
    2. Dim appXL As Excel.Application
    3. Dim R As Long, Names As String
    4. Dim hours As Double
    5. Dim N As Long
    6. Rws = 3
    7. Sheets("Summary Hrs").Select
    8. While Rws < 650
    9. TaskName = Cells(Rws, "B").Text
    10. If TaskName <> "" Then
    11. WindowActivate WindowName:="C:\Documents and Settings\me\Desktop\project_template.Published.mpp"
    12.  
    13. SelectTaskField Row:=1, Column:="Name", RowRelative:=False
    14. For R = 1 To ActiveProject.Tasks.Count
    15.  
    16.     ObjTask = ActiveProject.Tasks(R).Name
    17.     If ObjTask = TaskName Then
    18.         Sheets("Summary Hrs").Select
    19.         Rws2 = Rws + 1
    20.         While Cells(Rws2, "C").Text <> ""
    21.             rescode = Cells(Rws2, "C").Text
    22.             hours = Cells(Rws2, "E").Text
    23.             Names = rescode & ", " & Names
    24.             Names = Left$(Names, Len(Names) - Len(ListSeparator & " "))
    25.             Rws2 = Rws2 + 1
    26.         Wend
    27.         SetTaskField Field:="Resource Names", Value:=Names, AllSelectedTasks:=True
    28.         R = ActiveProject.Tasks.Count
    29.         'WindowActivate WindowName:="C:\Documents and Settings\me\Desktop\project_template.Published.mpp"
    30.             'SelectTaskField Row:=R, Column:="Name", RowRelative:=False
    31.             'SetTaskField Field:="Resource Names", Value:=AllResources, AllSelectedTasks:=True
    32.             'ResourceAssignment Resources:=rescode, Operation:=pjAssign
    33.             'WindowActivate TopPane:=False
    34.             'SetTaskField Field:="Work", Value:=hours, TaskID:=R, ProjectName:="C:\Documents and Settings\me\Desktop\project_template.Published.mpp"
    35.     End If
    36. Next R
    37. End If
    38. Rws = Rws + 1
    39. Wend
    40. End Sub
    I tried it a few ways. Just dumping all the gathered resource codes puts them in the first line my my project only. I'm sure it's just a simple error, it was working better earlier. After 8 hours of this, it's hard to keep starit what you've changed in your code and what made it break last.
    Any advice will help.
    Thanks,
    B
    Last edited by queenb2005; Jul 18th, 2005 at 06:59 AM. Reason: Resolved

  2. #2
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: MS Project VBA, the red-headed stepchild

    Sorry if this isn't an intelligent response, but I'm not very experienced in programming.

    You write:
    VB Code:
    1. For R = 1 To ActiveProject.Tasks.Count
    2.  
    3.     ObjTask = ActiveProject.Tasks(R).Name
    4.     If ObjTask = TaskName Then
    5.         Sheets("Summary Hrs").Select
    6.         Rws2 = Rws + 1
    7.         While Cells(Rws2, "C").Text <> ""
    8.             rescode = Cells(Rws2, "C").Text
    9.             hours = Cells(Rws2, "E").Text
    10.             Names = rescode & ", " & Names
    11.             Names = Left$(Names, Len(Names) - Len(ListSeparator & " "))
    12.             Rws2 = Rws2 + 1
    13.         Wend
    14.         SetTaskField Field:="Resource Names", Value:=Names, AllSelectedTasks:=True
    15.         R = ActiveProject.Tasks.Count 'Note this line
    16.        End If
    17. Next R

    When you set R to the ending condition of your for loop right in the middle of the loop itself, that should cause your loop to quit after the first iteration.

    Admittedly, I don't really see what your code is doing, I just went through to see if I could find any simple problems.
    Good Luck!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Location
    Florida
    Posts
    12

    Re: MS Project VBA, the red-headed stepchild

    I ended up re-wring that code a different way. It works now. The issue now is understanding MS Project. I am trying to select a cell, which I can, and then return a string for what text is in it, which I just can't figure out. Here is an exact picture of the project and what I can't identify.

    I am trying to select a resource code, store it as a string, match that string to a value in Excel spreadsheet, then fill out the hours accordingly. I just can't return a value, it seems so simple but I just can't make any code work.
    VB Code:
    1. Rws = 1
    2. ViewApply Name:="Tas&k Usage"
    3. SelectTaskField Row:=1, Column:="Name", RowRelative:=True
    4. For R = 2 To ActiveProject.Tasks.Count
    5.     ObjTask = ActiveProject.Tasks(R).Name
    6.     Sheets("Summary Hrs").Activate
    7.     Columns("B:B").Select
    8.     Selection.Find(What:=ObjTask, After:=ActiveCell, LookIn:=xlFormulas, _
    9.     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    10.     MatchCase:=False).Activate
    11.     Rws = ActiveCell.Row + 1
    12.     If Cells(Rws, "C") <> "" Then
    13.         SelectTaskField Row:=0, Column:="Name"
    14.         'below selects the resource code's cell
    15.         SelectTaskField Row:=1, Column:="Name"
    16.         'MyResource is nothing. I've tried different
    17.         'ways of using ActiveProject or ActiveSelection
    18.         'with no avail
    19.         MyResource = ActiveSelection.FieldName
    20.        
    21.     'this is where I go to excel, match the value
    22.     'grab the "hours" value, then stick it where it goes
    23.         SetResourceField Field:="Work", Value:=hours, AllSelectedResources:=True
    24.        
    25.     End If
    26. Next R
    So any suggestions????

    QueenB

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Location
    Florida
    Posts
    12

    Wink Re: MS Project VBA, the red-headed stepchild

    With the help of Project 2003 Inside Out, Using Excel Visual Basic Second Edition, both program's help files, and four pots of coffee it finally clicked. Here is how to specify a Project file and auto populate your resources and thier hours from an Excel spreadsheet before you start the project. I used Text10 as a static field in case more milestones are added.
    VB Code:
    1. Sub Resource_Load()
    2.  
    3. On Error Resume Next
    4. Application.ScreenUpdating = False
    5. Dim TaskNumber As Integer
    6. Dim R As Integer
    7. Dim N As Integer
    8. Rws = 1
    9. Rws2 = 1
    10. N = 0
    11.  
    12. PJFile = Application.GetOpenFilename("All Files (*.*),*.*", 1, "Select MS Project Template", MultiSelect = False)
    13. Dim appPJ As MSProject.Application
    14. Set appPJ = CreateObject("MSProject.Application")
    15. appPJ.Visible = True
    16. appPJ.FileOpen Name:=PJFile, ReadOnly:=False, FormatID:="MSProject.MPP"
    17. WindowSplit
    18.  
    19. ViewApply Name:="Tas&k Usage"
    20. SelectTaskField Row:=1, Column:="Name", rowrelative:=False
    21.  
    22. For R = 1 To ActiveProject.Tasks.Count
    23.     TaskNumber = ActiveProject.Tasks(R).Text10
    24.     R = TaskNumber
    25.     SelectTaskField Row:=R + N, Column:="Name", rowrelative:=False
    26.     ObjTask = ActiveProject.Tasks(R).Name
    27.     Sheets("Summary Hrs").Activate
    28.     Columns("A:A").Select
    29.     Selection.Find(What:=TaskNumber, After:=ActiveCell, LookIn:=xlFormulas, _
    30.     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    31.     MatchCase:=False).Activate
    32.     Rws = ActiveCell.Row
    33.     Rws2 = Rws + 1
    34.     If Cells(Rws2, "C") <> "" Then
    35.         While Cells(Rws2, "E").Text <> ""
    36.             rescode = Cells(Rws2, "C").Text
    37.             hours = Cells(Rws2, "E").Text
    38.             WindowActivate WindowName:=PJFile
    39.             SelectTaskField Row:=0, Column:="Name", rowrelative:=True
    40.             ResourceAssignment Resources:=rescode & "[0%]", Operation:=pjAssign
    41.             Rws2 = Rws2 + 1
    42.         Wend
    43.         SelectTaskField Row:=1, Column:="Name"
    44.         For Each A In ActiveProject.Tasks(R).Assignments
    45.             ResourceName = A.ResourceName
    46.             Sheets("Summary Hrs").Activate
    47.             Columns("C:C").Select
    48.             Selection.Find(What:=ResourceName, After:=Cells(Rws, "C"), LookIn:=xlFormulas, _
    49.             LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    50.             MatchCase:=False).Activate
    51.             Rws2 = ActiveCell.Row
    52.             hours = Cells(Rws2, "E").Value
    53.             WindowActivate WindowName:=PJFile
    54.             SetResourceField Field:="Work", Value:=hours, AllSelectedResources:=True
    55.             SelectTaskField Row:=1, Column:="Name"
    56.             N = N + 1
    57.             Rws2 = 1
    58.         Next A
    59.     End If
    60. Next R
    61.  
    62. End Sub
    Project is not too hard, just different.
    ~ Queen B

  5. #5
    New Member
    Join Date
    Jul 2016
    Posts
    1

    Re: MS Project VBA, the red-headed stepchild

    I've looked all over and haven't found a way to start a new thread. This one is close to what my subject is so I'll reply.

    If I run the below code on one MS Project file it works. On another one and I get a Run-time error '1101': The argument value is not valid. I've narrowed it down to the RowRelative:=False argument. Does anyone know why it would work on one file and not on the other? Thank you!

    Sub CountAllTasks()
    Dim T As Task
    Dim i As Integer
    Dim tCount As Integer
    Dim r As Long
    Dim sTest1 As String


    i = 0
    tCount = 0

    tCount = ActiveProject.tasks.Count


    For r = 0 To tCount

    SelectTaskField Row:=r, Column:="Name", RowRelative:=False


    sTest1 = ActiveCell.Task.Name
    On Error GoTo ErrHandler:


    If sTest1 <> "" Then
    i = i + 1
    End If
    Next r

    ErrHandler:
    MsgBox "Number of active tasks in this file: " & i, vbOKCancel
    SelectRow Row:=0, RowRelative:=False



    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width