Task filter configuring using C# or VBA RRS feed

  • Question

  • Hello! I use C# program to open a MS Project task with the desired unique ID. I use this code:

                                foreach (Project.Task task in msPrj.Tasks)
                                    if (task.UniqueID == Id)
                                        prjApp.SelectRow(task.ID, falseObj);

    But there are some flaws in its work. When the project has sub-task and all nested tasks lists revealed everything works correctly. But if minimize all subtasks, then the code will select the row where task need to be, but as the tasks lists is minimized it is not there. So, look at picture. 

    How can I configure a filter to the list unfolded to the desired objectives and the marker was on this task?

    • Edited by GfiFer Saturday, June 10, 2017 7:46 AM
    Saturday, June 10, 2017 7:43 AM

All replies

  • Hi GfiFer,

    I suppose you could add these methods before the For block in your code to make your approach work in your example (vba syntax):

        OptionsViewEx ProjectSummary:=False
        Sort Key1:="ID", Renumber:=False

    Your code presumes an equivalence between Task.ID and the Row number in the displayed table, which in my view is a flawed approach for any general-purpose solution.  Such equivalence only exists if 1)all of the subtasks are shown ("unfolded"); 2)the tasks are sorted by ID; 3)the project summary task is hidden; 4)there are no (hidden) external tasks; 5)there are no subprojects; 6)there are no blank lines inserted in the table;  etc.  The methods above fix only the first three conditions.

    I generally avoid any direct selections or manipulations of the displayed table (i.e. "foreground" processes) until the core (background) processes of the program are complete.  You haven't said what your code is doing after selecting the row.  If it is doing anything other than formatting the text in the table, then there is probably a more robust way to get what you want.

    Good luck

    Saturday, June 10, 2017 1:55 PM
  • Thank you very much! this is very useful information for me.

    Actually, my program has some objects, which are linked to tasks and, if necessary, my program opens up MS Project and allocate the required task for user. I tried to do this using hyperlinks, but they only work for ID, which may change. How to make a link to the task by unique ID I have not found, so I wrote this simple code.

    Please, tell me how can I deploy only that total task, inside of which is a necessary subtask using VBA?

    Sunday, June 11, 2017 11:07 AM
  • Pavel,

    I thought we had this all worked out with the hyperlinks. Too bad, that was a fun exercise :-)

    In your initial post above, you asked about using a filter so let's see if I understand what you are are trying to do.

    You have a Project file with multiple summary lines and subtasks under them. You have the unique ID of a task within that file. You want to select the task with that unique ID. Is that correct? Further, it also sounds like you want to filter the file to only show that selected task and the other subtasks under the summary line that contains the selected task. Is that correct?

    If that is not correct, could you send me an example of what you want to see?


    Sunday, June 11, 2017 3:30 PM
  • John, glad to see you! If you remember, we managed to create a link by ID, which may vary. Therefore, after the change of the project, the link is no longer relevant, as it will give you to different task. So I wrote a simple module for my C# program that will search for a task by given unique ID.

    Now, the question in this thread. Let's say I have a project like in the first screenshot and I want to select a task D1.1 by unique ID. Then after the code work I want to see the result as on the second screenshot

    Monday, June 12, 2017 3:51 PM
  • Pavel,

    Yes that can be done but not by using the SelectRow Method, at least initially. The easiest way to do what you want is to initially expand the file, as noted by Tom in his response, then select the desired task. Is there a reason you can't fully expand the file before attempting to find and select a task?

    What you can do is to start with a partially collapsed file, locate the desired task in the background, then expand only a particular part of the file that contains the desired task. For example, start with your first screen shot. In the background locate Task D1.1, then apply a filter to show that task along with it's summary (Task D1) and other subtasks under that summary (Task D1.2). You could even select Task D1.1 after the filter is applied.


    Monday, June 12, 2017 8:23 PM
  • Thank you, John, that's clear enough. The algorithm I understood before, I'm curious to know the VBA command to do this.
    Tuesday, June 13, 2017 9:23 PM
  • Pavel,

    The following VBA code will do what I explained in my last response. In order to run a test, I replicated your example file and then edited it to make sure some of the unique IDs were not same as the task IDs. In the example, Task D1.1 is unique ID 13.

    Sub Pavel()
    Dim t As Task
    Dim TskUID As String
    Dim ParID As Long
    'clear Flag1 for all tasks in case they were set
    For Each t In ActiveProject.Tasks
        t.Flag1 = False
    Next t
    'fully expand the file
    'set the value of the desired unique ID and then find it
    TskUID = CStr(13)
    SelectBeginning 'make sure find starts at first task
    FindEx Field:="UniqueID", test:="equals", Value:=TskUID
    'find the parent task associated witht the selected task
    ParID = ActiveSelection.Tasks(1).OutlineParent.ID
    'set a flag for the parent and all subtasks
    ActiveProject.Tasks(ParID).Flag1 = True
    For Each t In ActiveProject.Tasks(ParID).OutlineChildren
        t.Flag1 = True
    Next t
    'create and apply filter for all tasks with Flag1 set
    FilterEdit Name:="PavFil", taskfilter:=True, create:=True, overwriteexisting:=True, _
        FieldName:="Flag1", test:="equals", Value:="yes", ShowInMenu:=False, showsummarytasks:=True
    FilterApply Name:="PavFil"
    'use find method to select the task in the filtered set
    FindEx Field:="UniqueID", test:="equals", Value:=TskUID
    End Sub

    This is the sample file. Note the unique ID of Task D1.1

    This is what it looks like prior to running the macro (i.e. collapsed)

    This is what it looks like after running the macro. The task associated with unique ID 13 was found, the summary level of that task was identified, and that summary was then expanded with the original task (Task D1.1) selected

    Hope this helps


    Wednesday, June 14, 2017 3:06 PM
  • Thank you, John! it's gorgeous!
    Sunday, June 18, 2017 2:19 PM
  • Pavel,

    You're welcome and thanks for the feedback. If I answered your question, please consider marking my response as the answer.


    Sunday, June 18, 2017 3:44 PM