none
How to Read (not change) the definition of all views in a plan. RRS feed

  • Question

  • I want to be able to export via VBA a read out of each view's details: 
    * Name 
    * Table 
    * Group 
    * filter 

    I can get the name with the following code
      
     Dim v As Variant 
     For Each v In ActiveProject.ViewList 
         Print #n, "View: " & v 
     Next v 

    However I am stuck when it comes to reading out the information about how the view is defined. 

    https://msdn.microsoft.com/en-us/library/office/ff865765.aspx suggests that I should be able to read / get the details fo tables, filter, group etc but I can't work out how! 

    Any help gratefully received as I am something of a beginner in VBA :) 

    Thanks 

    Miles 

    Tuesday, May 17, 2016 7:07 AM

All replies

  • Hi Miles,

    The reference you quoted was a good one - one of three I would have sent you to.  The others are:

    https://msdn.microsoft.com/en-us/library/office/ff865951.aspx and 

    https://msdn.microsoft.com/en-us/library/office/ff865951.aspx

    ViewList is exactly as its name implies, a list of names (i.e. strings/text values.)  To find the component members of the various views, you need to examine the objects themselves.  Below is a little code to get you started - it just prints the itemized names to the Immediate window.  You can replace the "Debug.Print" statements with whatever other output you want to use.  (Please forgive the rudimentary error handling.  I don't do much of it in vba.)

    Here is the code:

    Sub VList() Dim vc As ViewCombination Dim vs As ViewSingle Dim f As Filter Dim g As Group Dim gc As GroupCriterion Dim tb As Table Dim tbf As TableField On Error Resume Next 'Print View Definitions Debug.Print "Active Project: "; ActiveProject.Name Debug.Print vbCr & "Combination Views:" For Each vc In ActiveProject.ViewsCombination Debug.Print "Combination View: " & vc.Name Debug.Print "Top View: "; vc.TopView.Name Debug.Print "Bottom View: "; vc.BottomView.Name; vbCr Next vc Debug.Print "Single Views:" For Each vs In ActiveProject.ViewsSingle Debug.Print "" Debug.Print "Single View: " & vs.Name Debug.Print "Type (0=Task, 1=Resource, 2=Other): "; vs.Type Debug.Print "Filter: "; vs.Filter If Err.Number <> 0 Then Err.Clear Debug.Print "Filter: N/A" End If Debug.Print "Group: "; vs.Group If Err.Number <> 0 Then Err.Clear Debug.Print "Group: N/A" End If Debug.Print "Table: "; vs.Table If Err.Number <> 0 Then Err.Clear Debug.Print "Table: N/A" End If Next vs 'print Table Definitions For Each tb In ActiveProject.TaskTables Debug.Print vbCr & "Table: "; tb.Name For Each tbf In tb.TableFields Debug.Print "Field: "; tbf.Index; " "; FieldConstantToFieldName(tbf.Field); " ("; tbf.Title; ")" Next tbf Next tb 'Print Group Definitions For Each g In ActiveProject.TaskGroups 'Debug.Print vbCr & "Group: "; g.Name For Each gc In g.GroupCriteria 'Debug.Print "Criteria: "; gc.Index; " "; gc.FieldName; " Ascending: "; gc.Ascending Next gc Next g End Sub

    Here is the output for the default blank project:

    Active Project: Project1

    Combination Views:
    Combination View: Gantt with Timeline
    Top View: Gantt Chart
    Bottom View: Timeline

    Single Views:

    Single View: Gantt Chart
    Type (0=Task, 1=Resource, 2=Other):  0 
    Filter: All Tasks
    Group: No Group
    Table: Entry

    Single View: Timeline
    Type (0=Task, 1=Resource, 2=Other):  0 
    Filter: N/A
    Group: &No Group
    Table: Table: N/A

    Table: Entry
    Field:  1  ID ()
    Field:  2  Indicators ()
    Field:  3  Name (Task Name)
    Field:  4  Duration ()
    Field:  5  Start ()
    Field:  6  Finish ()
    Field:  7  Predecessors ()
    Field:  8  Resource Names ()

    I commented-out the output for groups since I didn't want to clutter this space with the many custom groups that are auto-copied to the default project from my global file.  Obviously you can restore the statements as needed. As you can see, it's pretty straightforward to read groups and tables, but filter criteria are a different animal.  I've not found an equally straightforward method for reading them, so my filter variable remains unused.

    Tuesday, May 17, 2016 3:30 PM
  • Thank you, I will try this out later and also share the rather less elegant code that I've developed so far to do some of this :)  Maybe it will allow people to point and laugh but it's all about learning :) :))
    Wednesday, May 18, 2016 6:44 AM