Page tree
Skip to end of metadata
Go to start of metadata

Folder Access

 ' Code Module
Sub Main

        Debug.Clear
        EnumProjectItems1 MWOffice.Project.UserFolders

        Debug.Clear
        EnumProjectItems2 MWOffice.Project.UserFolders

        Debug.Clear
        EnumFolderSchematics1 MWOffice.Project.UserFolders

        Debug.Clear
        EnumFolderSchematics2 MWOffice.Project.UserFolders

        Debug.Clear
        DumpUserFolder MWOffice.Project.UserFolders, " "
End Sub

' Enum the project items in a folder
Sub EnumProjectItems1(obFolder As MWOffice.UserFolder)
        Dim obProjectItem As MWOffice.ProjectItem
        For Each obProjectItem In obFolder.ProjectItems
                Debug.Print obProjectItem.Type
                Debug.Print obProjectItem.Name
        Next obProjectItem
End Sub

' Enum the project items in a folder as an object, this allows calling
' a method common to all project items in the folder
Sub EnumProjectItems2(obFolder As MWOffice.UserFolder)
        Dim obObject As Object
        For Each obObject In obFolder.ProjectItems
                Debug.Print obObject.Name
                obObject.NewWindow

                Dim obProjectItem As MWOffice.ProjectItem
                Set obProjectItem = obObject
                Debug.Print obProjectItem.Type

        Next obObject
End Sub

' Executes a method on all the schematics in a folder by checking the project item type
Sub EnumFolderSchematics1(obFolder As MWOffice.UserFolder)
        Dim obProjectItem As MWOffice.ProjectItem
        For Each obProjectItem In obFolder.ProjectItems
                If obProjectItem.Type = MWOffice.mwPIT_Schematic Then
                        Dim obSchematic As MWOffice.Schematic
                        Set obSchematic = obProjectItem
                        Debug.Print obSchematic.Name
                        obSchematic.NewWindow
                End If
        Next obProjectItem
End Sub

' Executes a method on all the schematics in a folder by casting project item to schematic
' and checking for error.
Sub EnumFolderSchematics2(obFolder As MWOffice.UserFolder)
        On Error Resume Next
        Dim obSchematic As MWOffice.Schematic
        For Each obSchematic In obFolder.ProjectItems
                If Not obSchematic Is Nothing Then
                        Debug.Print obSchematic.Name
                        obSchematic.NewWindow
                End If
                Set obSchematic = Nothing
        Next obSchematic
End Sub

' Print the items in the given folder to the output window
Sub DumpUserFolder(obParentFolder As MWOffice.UserFolder, ByVal Tabs As String)

        ' Display the Name
        Debug.Print Tabs & obParentFolder.Name

        Tabs = Tabs & "   "

        ' Display the ChildFolders
        Dim obUserFolder As MWOffice.UserFolder
        For Each obUserFolder In obParentFolder.Folders
                DumpUserFolder obUserFolder, Tabs
        Next obUserFolder

        ' Display the ProjectItems
        Dim obProjectItem As MWOffice.ProjectItem
        For Each obProjectItem In obParentFolder.ProjectItems
                DumpProjectItem obProjectItem, Tabs
        Next obProjectItem

End Sub

' Dump a project item to the output window
Sub DumpProjectItem(obProjectItem As MWOffice.ProjectItem, Tabs As String)

        ' Display the Name
        Debug.Print Tabs & obProjectItem.Name
End Sub

 

You can access the user folders with MWOffice.Project.UserFolders collection.

 

Adding Items to a Project Folder

 ' Code Module
Sub Main
        Debug.Clear
        MWOffice.Project.UserFolders.Folders.RemoveAll
        MWOffice.Project.UserFolders.ProjectItems.RemoveAll
        AddItemsToFolder MWOffice.Project.UserFolders

        EnumNodes MWOffice.Project.UserFolders.TreeNode.Children, " "
End Sub

Sub AddItemsToFolder(obUserFolder As MWOffice.UserFolder)

        obUserFolder.Folders.RemoveAll
        obUserFolder.ProjectItems.RemoveAll

        ' Add a child folder to add the items to
        Dim obChildFolder As MWOffice.UserFolder
        Set obChildFolder = obUserFolder.Folders.Add("test")

        ' Add a sub folder
        obChildFolder.Folders.Add "Child1"

' Note the Project needs to have these items in it or the Add will fail

        ' Add the project items
        obChildFolder.ProjectItems.Add Project.Schematics("Schematic 1")
        obChildFolder.ProjectItems.Add Project.SystemDiagrams("System Diagram 1")
        obChildFolder.ProjectItems.Add Project.EMStructures("EM Structure 1")
        obChildFolder.ProjectItems.Add Project.Netlists("Netlist 1")
        obChildFolder.ProjectItems.Add Project.DataFiles("Data File 1")
        obChildFolder.ProjectItems.Add Project.Graphs("Graph 1")
        obChildFolder.ProjectItems.Add Project.Annotations(1)
'       obChildFolder.ProjectItems.Add Project.Graphs(1).Measurements(1)
        obChildFolder.ProjectItems.Add Project.OptGoals(1)
        obChildFolder.ProjectItems.Add Project.YieldGoals(1)
        obChildFolder.ProjectItems.Add Project.OutputFiles(1)
        obChildFolder.ProjectItems.Add Project.Wizards("AWR Load Pull Wizard")
'       obChildFolder.ProjectItems.Add Project.Wizards("AWR Load Pull Wizard").WizardInstances(1)
        obChildFolder.ProjectItems.Add Project.DesignNotes(1)
        obChildFolder.ProjectItems.Add Project.GlobalEquations
        obChildFolder.ProjectItems.Add Project.OutputEquations

        ' Select the Schematic added to the folder
        Dim obProjectItem As MWOffice.ProjectItem
        Set obProjectItem = obChildFolder.ProjectItems.Item("Schematic 1")
        Debug.Print obProjectItem.Name
        Debug.Print obProjectItem.TreeNode.Text
        obProjectItem.TreeNode.Select

        ' Expand the Schematic node
        Dim obTreeNode As MWOffice.TreeNode
        Set obTreeNode = obProjectItem.TreeNode
        obTreeNode.Expand True
End Sub

Sub EnumNodes(Nodes As MWOffice.TreeNodes, Indent As String)

        Dim Node As MWOffice.TreeNode
        For Each Node In Nodes
                If Node.Visible Then
'                       Debug.Print Indent & Node.Name
                        Debug.Print Indent & Node.Text
                End If
                EnumNodes Node.Children, Indent + "   "
        Next Node

End Sub

To add project item to Folder use an user folders tree node with MWOffice.Project.UserFolders.TreeNode.

 

Recursively Searching Project Folders

The following code goes through each project folder and deletes empty folders from the bottom up (so that any top level folders that contained only empty folders are deleted).

 Function parseFolder(Optional ufs As UserFolders) As Boolean

        Dim uf As UserFolder

        If (ufs Is Nothing) Then
                Set ufs = Project.UserFolders.Folders
        End If

        For Each uf In ufs
                Debug.Print "Parsing " + uf.Name
                parseFolder(uf.Folders)
                If (uf.ProjectItems.Count = 0) And (uf.Folders.Count = 0) Then
                        Debug.Print "Removing " + uf.Name
                        ufs.Remove(uf.Name)
                End If
        Next uf


End Function