locked
How to get the colums from a project view RRS feed

  • Question

  • Greetings,

    I have a PjView-object from type Microsoft.Office.Interop.MSProject.View
    and I can output the name of the view (MsgBox(PjView.Name.ToString)

    So far so good.

    But now I have two questions:
    1. How do I access the columns that are included in this view?
    2. If I have a column, how can I read the Title property (In Project: Field Settings -> Title)

    Thank you!

       Michael

    Monday, January 20, 2014 12:51 PM

Answers

  • This code will get the fields that exist on the Gantt Chart view in the current project:

    Public Sub GetCurrentViews()
    
    Dim vw As View
    Dim vw_sng As ViewSingle
    Dim vw_com As ViewCombination
    Dim tbl As Table
    Dim fld As TableField
    
    Set vw = ActiveProject.Views("Gantt Chart")
    
    If vw.Single Then
        Set vw_sng = vw
    Else
        Set vw_com = vw
        Set vw_sng = vw_com.TopView
    End If
    
    Set tbl = vw_sng.Table
    
        Debug.Print tbl.Name
            
        'step through each column on each table
        For Each fld In tbl.TableFields
        
            'check that it isn't the "Insert column" marker
            If fld.Field <> -1 Then
                'if it is an actual field, output field title
                Debug.Print "  " & fld.Title
                Debug.Print "  " & fld.Field & " - " & Application.FieldConstantToFieldName(fld.Field)
            End If
            
        Next fld
    
    End Sub

    Hope that helps,

    Andrew


    Andrew Simpson
    Founder – Eversight Ltd

    E: info@eversight.co.uk
    W: www.eversight.co.uk

    Download Eversight for MS Project — a free add-on which helps users build high quality schedules with MS project.

    • Marked as answer by Michael-Markus Monday, January 20, 2014 11:06 PM
    Monday, January 20, 2014 4:12 PM

All replies

  • Columns are included in the underlying table of the view, rather than the view itself.

    The code sample below shows how to step through the tables collection, then the fields collection, outputting the column title to the immediate window for each field:

    Public Sub GetCurrentViews()
    
    Dim tbl As Table
    Dim fld As TableField
    
        'step through each table in the active project
        For Each tbl In ActiveProject.TaskTables
            Debug.Print tbl.Name
            
            'step through each column on each table
            For Each fld In tbl.TableFields
            
                'check that it isn't the "Insert column" marker
                If fld.Field <> -1 Then
                    'if it is an actual field, output field title
                    Debug.Print "  " & fld.Title
                End If
                
            Next fld
        
        Next tbl
    
    End Sub

    I hope that helps,

    Andrew



    Andrew Simpson
    Founder – Eversight Ltd

    E: info@eversight.co.uk
    W: www.eversight.co.uk

    Download Eversight for MS Project — a free add-on which helps users build high quality schedules with MS project.

    Monday, January 20, 2014 1:27 PM
  • Hello Andrew,

    thank you very much for the code example.

    One question I would have:

    Now I have a view with the name "xyz". (PjView.Name = "xyz")

    How can I find the conection between the view "xyz" and the Fields in the tables?

    Thank you!

       Michael

    Monday, January 20, 2014 2:15 PM
  • This code will get the fields that exist on the Gantt Chart view in the current project:

    Public Sub GetCurrentViews()
    
    Dim vw As View
    Dim vw_sng As ViewSingle
    Dim vw_com As ViewCombination
    Dim tbl As Table
    Dim fld As TableField
    
    Set vw = ActiveProject.Views("Gantt Chart")
    
    If vw.Single Then
        Set vw_sng = vw
    Else
        Set vw_com = vw
        Set vw_sng = vw_com.TopView
    End If
    
    Set tbl = vw_sng.Table
    
        Debug.Print tbl.Name
            
        'step through each column on each table
        For Each fld In tbl.TableFields
        
            'check that it isn't the "Insert column" marker
            If fld.Field <> -1 Then
                'if it is an actual field, output field title
                Debug.Print "  " & fld.Title
                Debug.Print "  " & fld.Field & " - " & Application.FieldConstantToFieldName(fld.Field)
            End If
            
        Next fld
    
    End Sub

    Hope that helps,

    Andrew


    Andrew Simpson
    Founder – Eversight Ltd

    E: info@eversight.co.uk
    W: www.eversight.co.uk

    Download Eversight for MS Project — a free add-on which helps users build high quality schedules with MS project.

    • Marked as answer by Michael-Markus Monday, January 20, 2014 11:06 PM
    Monday, January 20, 2014 4:12 PM
  • Andrew, thank you very much!

      Michael

    Monday, January 20, 2014 11:06 PM