none
Creating a custom field

    Pertanyaan

  • Good afternoon,

    I'm using vba to transfer data about projects from Excel to MS Project. The code is working greatly, it takes data from the column and creates tasks on MS Project and I now want to add a description for each task.

    These descriptions are contained in a column in MS Excel, so I should just transfer them, the problem is that there is no "Task Description" field in MS Project, so I wanted to customize one, using GetField and SetField. The problem is that I'm not sure I understood how these work and I tried to write the code following an example I found online:

    projectField = FieldNameToFieldConstant("Task Description", pjProject)
    
    prProject.Tasks(task_counter).SetField FieldID:=projectField, Value:="Example"

    The problem is that as soon as it runs in the first line, it gives me "run-time error 1004".

    Any ideas?

    Thanks!


    • Diedit oleh moncho52 Senin, 14 Mei 2018 13.52
    Senin, 14 Mei 2018 10.08

Jawaban

  • moncho52,

    I'm curious as to why you don't like the Notes field. It is the only text field that can hold tons of text data whereas the other 30 extra text field (i.e. Text1 through Text30) can only hold up to 255 characters each.

    You probably get the error because you haven't pre-defined the "task description" field in the Project file. But there is a much easier way to do it.

    Let's say you renamed the Text1 field as "Task Description". I would then use this syntax:

    prProject.Tasks(task-counter).Text1="Example"

    Hope this helps.

    John

    • Ditandai sebagai Jawaban oleh moncho52 Rabu, 16 Mei 2018 13.58
    Selasa, 15 Mei 2018 18.30

Semua Balasan

  • moncho52,

    There definitely is a "task description" field, it's called the Task Name field. If you need to add more information about each task, you can use the Notes field.

    As far as the run-time error, it would be a lot easier to help figure that out if we could see your full VBA code. And we also need information on what columns contain what data in Excel.

    John


    Senin, 14 Mei 2018 13.52
  • John,

    thanks for your answer. I used the Notes field, but I don't really like it and I wanted to create a customized field in which I can insert a better explanation/description of the task. The code is the one that follows:

    For i = 1 To wbPac.Worksheets.Count k = 4 prProject.Tasks.Add (wbPac.Worksheets(i).Name) prProject.Tasks(task_counter).OutlineLevel = 1

    task_counter = task_counter + 1

    Do While k < 2000 If IsEmpty(wbPac.Worksheets(i).Cells(k, 7).Value) Then k = 3000 Else exRes = wbPac.Worksheets(i).Cells(k, 4).Value exProj = wbPac.Worksheets(i).Cells(k, 5).Value exStatus = wbPac.Worksheets(i).Cells(k, 6).Value exStartDate = wbPac.Worksheets(i).Cells(k, 7).Value exEndDate = wbPac.Worksheets(i).Cells(k, 8).Value exDur = wbPac.Worksheets(i).Cells(k, 9).Value If Left(wbPac.Worksheets(i).Cells(k, 4), 3) = "DUM" And (wbPac.Worksheets(i).Cells(k, 7) = wbPac.Worksheets(i).Cells(k + 1, 7)) Then prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 2 k = k + 1 status = 0 ElseIf wbPac.Worksheets(i).Cells(k, 4) <> wbPac.Worksheets(i).Cells(k + 1, 4) And (status = 0) Then status = 0 prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 2 k = k + 1 ElseIf wbPac.Worksheets(i).Cells(k, 4) = wbPac.Worksheets(i).Cells(k + 1, 4) And (status = 0) Then status = 1 prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 2 task_counter = task_counter + 1 prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 3 k = k + 1 ElseIf wbPac.Worksheets(i).Cells(k, 4) = wbPac.Worksheets(i).Cells(k + 1, 4) And (status = 1) Then prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 3 k = k + 1 ElseIf wbPac.Worksheets(i).Cells(k, 4) <> wbPac.Worksheets(i).Cells(k + 1, 4) And (status = 1) Then status = 0 prProject.Tasks.Add (exRes) prProject.Tasks(task_counter).OutlineLevel = 3 k = k + 1 End If If exStatus = "C" Then exProj1 = exProj & " - C" prProject.Tasks(task_counter).ResourceNames = exProj1 Else prProject.Tasks(task_counter).ResourceNames = exProj End If prProject.Tasks(task_counter).Start = exStartDate + prProject.DefaultStartTime prProject.Tasks(task_counter).Duration = exDur & "giorni" projectField = FieldNameToFieldConstant("Task Description") prProject.Tasks(task_counter).SetField FieldID:=projectField, Value:="Example" task_counter = task_counter + 1 End If Loop Next i


    The "If" are required to set up the proper OutlineLevel, depending the data that is extracted from Excel. wbPac is the Excel workbook from which I'm parsing the data. If I debug the error, it highlights the line starting from "projectField".

    I'm not super expert in vba, so I don't really know if it is the proper way to use "SetField".

    Thanks for your help

    Selasa, 15 Mei 2018 07.08
  • moncho52,

    I'm curious as to why you don't like the Notes field. It is the only text field that can hold tons of text data whereas the other 30 extra text field (i.e. Text1 through Text30) can only hold up to 255 characters each.

    You probably get the error because you haven't pre-defined the "task description" field in the Project file. But there is a much easier way to do it.

    Let's say you renamed the Text1 field as "Task Description". I would then use this syntax:

    prProject.Tasks(task-counter).Text1="Example"

    Hope this helps.

    John

    • Ditandai sebagai Jawaban oleh moncho52 Rabu, 16 Mei 2018 13.58
    Selasa, 15 Mei 2018 18.30
  • John, 

    thank you very much for your help. Yeah, I did not rename the field in "task description", honestly I had no idea I had to do it.

    I am not using "Note" because it was asked me to have a better naming of the column in which I will copy the data. I could just probably rename the "Note" in "Task Description" and then use that.

    Thank you very much for your help, it was extremely useful!

    Greetings,

    Andrea

    Rabu, 16 Mei 2018 13.07
  • Andrea,

    You're welcome and thanks for the feedback.

    You can't rename the Notes field because it is not an extra field, like a text field or a number field, but you can change the column title. Right click on the Notes field column heading and then select "Field settings". Change the Title to "Task Description".

    If I answered your question please consider marking one of my responses as the answer.

    John

    Rabu, 16 Mei 2018 13.21