Creating Subprocess Diagrams Programmatically in Visio 2010

Summary:  Learn how to create subprocess diagrams programmatically in Microsoft Visio 2010.

Applies to: Office 2010 | SharePoint Server 2010 | Visio 2010 | Visio Premium 2010

In this article
Overview
Creating a Process Diagram
Creating a Subprocess Page
Moving a Shape to a Subprocess Page
Moving a Selection of Shapes to a Subprocess Page
Conclusion
Additional Resources

Published:  March 2011

Provided by:  Saul Candib, Microsoft Corporation

Contents

  • Overview

  • Creating a Process Diagram

  • Creating a Subprocess Page

  • Moving a Shape to a Subprocess Page

  • Moving a Selection of Shapes to a Subprocess Page

  • Conclusion

Overview

This article describes how to use the Visual Basic for Applications (VBA) object model to create a Microsoft Visio 2010 subprocess diagram. A subprocess diagram is usually part of a larger process-overview diagram based on one of the Visio Flowchart templates, which are often used to model large processes. Large processes are often composed of subprocesses that can be thought of as self-contained units. You can create detailed subprocess diagrams and link them together in a larger process-overview diagram.

Note

The subprocess feature is available only in Visio Premium 2010.

Subprocesses on the overview diagram are represented by a single shape that is linked to the subprocess page. To open the subprocess page, you press the Ctrl key and then click the linked shape, or right-click the linked shape and then click the named hyperlink on the shortcut menu. You can put subprocess drawings on separate pages in a single document or, if you want to use the same subprocess in multiple diagrams, you can create the subprocess diagram once, save it, and then link to it from subprocess shapes in other diagrams.

The general procedure to create a subprocess diagram is the same whether you create the diagram manually or programmatically: you add a page to an existing diagram and then add shapes to that page to represent the subprocess. To create and link to subprocess diagrams manually, you use commands on the Process tab on the Visio 2010 ribbon. To create and link to subprocess diagrams programmatically, you use VBA code.

In earlier versions of Visio, you use the Shape.AddHyperlink method to add a hyperlink to the shape on the main process page that you want to link to the subprocess page. Then, you set the SubAddress property value of the Hyperlink object returned by the AddHyperlink method to the subprocess page. However, in the Visio 2010 VBA object model, there are three new methods that you can use instead:

  • Shape.CreateSubProcess—Creates and returns a new subprocess page that is linked to the existing shape on the process page.

  • Shape.MoveToSubprocess—Moves the existing shape to a specified page, drops a replacement shape on the source page, and then links the replacement shape to the target page. Returns the selection of moved shapes on the target page.

    Note

    As explained later in this article, the existing shape must be a container shape.

  • Selection.MoveToSubprocess—Moves the selection to a specified page, drops a replacement shape on the source page, and then links the replacement shape to the target page. Returns the selection of moved shapes on the target page.

In general, you use the following procedure to create a subprocess diagram programmatically.

To create a subprocess diagram programmatically

  1. Create a process-diagram document in Visio 2010, adding shapes, as appropriate, to represent the overall process.

    You can do all or part of this diagram creation programmatically.

  2. Identify the shape or shapes in the process diagram that you want to represent and link to one or more subprocess or subprocesses, each one on a separate subprocess page.

  3. Use the CreateSubProcess method on each such shape to create a new subprocess page in the same document.

  4. Pass the newly created page to the Shape.MoveToSubprocess or Selection.MoveToSubprocess method, together with the object (for example, a Visio master) that you want to use to take the place of the previously specified shape.

    This object that you pass to the selected method will represent the subprocess on the source page and will link to the subprocess page.

The following sections provide more information about these methods and how to use them. The next section provides sample code that creates a simple subprocess diagram.

Creating a Process Diagram

To demonstrate how the code shown later in this article works, it is useful to create a flowchart diagram that models a simple process. To create this drawing, open Visio, create a new diagram from the Flowchart template, open the Visual Basic Editor, and then run the following code.

Note

You could use the Documents.Add method to create a flowchart diagram programmatically. However, for the purposes of this demonstration, it is more convenient to create the drawing manually and then add the code. That way, the code is in the same document in which you are working—which, among other things, enables you to use the ThisDocument object in your code.

Important

Although the units on the drawing page are metric, you must specify the xPos and yPos parameters in the Shape.Drop method in inches.

Public Sub CreateSampleFlowchart()

    Dim vsoDocumentStencil As Document
    Dim vsoStencilDocument As Document
    Dim vsoPage As Page
    Dim vsoMasterProcess As Master
    Dim vsoMasterDecision As Master
    Dim vsoMasterSubProcess As Master
    Dim vsoMasterContainer As Master
    Dim vsoShapeProcess1 As Shape
    Dim vsoShapeProcess2 As Shape
    Dim vsoShapeProcess3 As Shape
    Dim vsoShapeProcess4 As Shape
    Dim vsoShapeToSubProcessA As Shape
    Dim vsoShapeToSubProcessB As Shape
    Dim vsoShapeDecision1 As Shape
    Dim vsoShapeDecision2 As Shape
    Dim vsoShapeSelection1 As Shape
    Dim vsoShapeSelection2 As Shape
    Dim vsoShapeContainer As Shape
    Dim vsoSelection As Selection
   
    Dim intDiagramServices As Integer
    intDiagramServices = ActiveDocument.DiagramServicesEnabled
    intDiagramServices = visServiceVersion140
    
    ' Specify masters to drop.
    Set vsoMasterProcess = Visio.Documents("BASFLO_M.VSS").Masters("Process")
    Set vsoMasterDecision = Visio.Documents("BASFLO_M.VSS").Masters("Decision")
    Set vsoMasterSubProcess = Visio.Documents("BASFLO_M.VSS").Masters("Subprocess")
    
    ' Set the Page object equal to the first page.
    Set vsoPage = ThisDocument.Pages(1)
    
    ' Create the first process shape.
    Set vsoShapeProcess1 = vsoPage.Drop(vsoMasterProcess, 3, 4.5)
    vsoShapeProcess1.Text = "Process 1"
        
    ' Create and connect the first decision shape.
    Set vsoShapeDecision1 = vsoPage.DropConnected(vsoMasterDecision, vsoShapeProcess1, visAutoConnectDirRight)
    vsoShapeDecision1.Text = "Decision"
    
    ' Create and connect the second process shape.
    Set vsoShapeProcess2 = vsoPage.DropConnected(vsoMasterProcess, vsoShapeDecision1, visAutoConnectDirRight)
    vsoShapeProcess2.Text = "Process 2"
    
    ' Create and connect one shape to link to a subprocess.
    Set vsoShapeToSubProcessA = vsoPage.DropConnected(vsoMasterProcess, vsoShapeDecision1, visAutoConnectDirDown)
    vsoShapeToSubProcessA.Text = "SubProcessA"
    
    ' Create and connect another shape to link to a subprocess.
    Set vsoShapeToSubProcessB = vsoPage.DropConnected(vsoMasterProcess, vsoShapeToSubProcessA, visAutoConnectDirDown)
    vsoShapeToSubProcessB.Text = "SubProcessB"
    
     ' Create and connect the second decision shape.
    Set vsoShapeDecision2 = vsoPage.DropConnected(vsoMasterDecision, vsoShapeProcess2, visAutoConnectDirRight)
    vsoShapeDecision2.Text = "Decision"
    
    ' Create and connect the third process shape.
    Set vsoShapeProcess3 = vsoPage.DropConnected(vsoMasterProcess, vsoShapeDecision2, visAutoConnectDirRight)
    vsoShapeProcess3.Text = "Process 3"
    
    ' Create and connect the fourth process shape.
    Set vsoShapeProcess4 = vsoPage.DropConnected(vsoMasterProcess, vsoShapeDecision2, visAutoConnectDirDown)
    vsoShapeProcess4.Text = "Process 4"
    
    Set vsoShapeSelection1 = vsoPage.Shapes.Item(6)
    Set vsoShapeSelection2 = vsoPage.Shapes("Process.8")
    
    ActiveWindow.DeselectAll
    Set vsoSelection = ActiveWindow.Selection
    
    ' Select two shapes to add to a container.
    vsoSelection.Select vsoShapeSelection1, visSelect
    vsoSelection.Select vsoShapeSelection2, visSelect
      
    ' Draw a container around the shapes.
    Set vsoStencilDocument = Application.Documents.OpenEx(Application.GetBuiltInStencilFile(visBuiltInStencilContainers, visMSMetric), visOpenHidden)
    Set vsoShapeContainer = Application.ActivePage.DropContainer(vsoStencilDocument.Masters.ItemU("Container 5"), vsoSelection)
    vsoShapeContainer.Text = "SubProcess Container"
    vsoStencilDocument.Close

End Sub

When you are finished, your drawing should look like Figure 1.

Figure 1. Sample flowchart drawing

Sample flowchart drawing

In this sample drawing, one branch of a decision shape leads to a container shape that encapsulates a subprocess. You must use a container shape to represent this subprocess because this article describes how the Shape.MoveToSubprocess method works, and that method works only on container shapes.

The other branch leads to a second process shape (Process 2) which, in turn, leads to another decision shape and two other processes.

Creating a Subprocess Page

Now you are ready to create a subprocess page. In fact, you will create two of these pages, because your drawing contains shapes that represent and link to two separate subprocesses. As previously mentioned, Visio provides the Shape.CreateSubprocess method to create a new subprocess page that is linked to the source shape. As shown in the following code, the method takes no parameters and returns a Page object that represents the newly created subprocess page.

If the source shape contains text, Visio assigns that text as the name of the new page. If the source shape does not contain text, Visio assigns a default name, such as "Page-2" or "Page-3," depending on the number of existing pages. The following code goes a bit further and directly assigns more descriptive names to the returned pages.

Paste the following code into the Visual Basic Editor and run it.

Public Sub CreateSubProcessPages()

    Dim vsoShape As Shape
    Dim vsoPage1 As Page
    Dim vsoPage2 As Page
    
    ' Create and name the first subprocess page.
    Set vsoShape = ActivePage.Shapes("Container 5")
    Set vsoPage1 = vsoShape.CreateSubProcess()
    vsoPage1.Name = "SubProcessPage1"
    
    ' Create and name the second subprocess page.
    Set vsoShape = ActivePage.Shapes("Process.4")
    Set vsoPage2 = vsoShape.CreateSubProcess()
    vsoPage2.Name = "SubProcessPage2"

End Sub

Now you have two new subprocess pages. The next step is to move the shapes that make up each subprocess onto their respective pages. To do so, you will use the two MoveToSubprocess methods described earlier in this article, one on the Shape object, and the other on the Selection object.

Moving a Shape to a Subprocess Page

To move a shape from the main process page to a subprocess page, you can use the Shape.MoveToSubprocess method. This method is designed specifically to move container shapes to a subprocess page, on the assumption that the subprocess is composed of the members of the container. Containers, a new feature in Visio 2010, are shapes that visually contain and logically relate other shapes on the page. They typically have a visible boundary around their contents, and they may have a text heading. They offer special actions for working with the shapes they contain. The Shape.MoveToSubprocess method does not work on shapes that are not containers. For more information about how to work with containers, see the Additional Resources section of this article.

The Shape.MoveToSubprocess method takes the parameters shown in Table 1.

Table 1. Shape.MoveToSubprocess parameters

Name

Required/Optional

Data type

Description

Page

Required

Page

The subprocess page to which the shape should be moved. You cannot pass the current page.

ObjectToDrop

Required

[UNKNOWN]

The replacement shape to drop.

NewShape

Optional

Shape

Out parameter. Returns the shape that is linked to the subprocess page.

ObjectToDrop is typically a Visio object, such as a Master or MasterShortcut object. However, it can be any OLE object that provides an IDataObject interface. If ObjectToDrop is null, Visio drops a default shape.

The following code example shows how to use the Shape.MoveToSubprocess method to move a container shape from the main process (source) page to a subprocess page. It drops a SubProcess master shape on the source page to take the place of the container shape and link to the subprocess page.

Paste the following code into the Visual Basic Editor and run it.

Public Sub MoveShape()

    Dim vsoPage As Page
    Dim vsoShape As Shape
    Dim vsoMaster As Master
    Dim vsoShapeNew As Shape

    ' Specify the container shape, the master shape to take its place, and the subprocess page.
    Set vsoMaster = Visio.Documents("BASFLO_M.VSS").Masters("SubProcess")
    Set vsoShape = ActivePage.Shapes("Container 5")
    Set vsoPage = ActiveDocument.Pages("SubProcessPage1")
    
    vsoShape.MoveToSubprocess vsoPage, vsoMaster, vsoShapeNew

End Sub

After you run the code, your source page should look like Figure 2.

Figure 2. Source page

Source page

The subprocess page should look like Figure 3.

Figure 3. Subprocess page

Subprocess page

Moving a Selection of Shapes to a Subprocess Page

To move a selection of shapes from the main process page to a subprocess page, you can use the Selection.MoveToSubprocess method. Selection.MoveToSubprocess will operate on any selection of two or more Shape objects that are any combination of plain shapes and containers, or on a selection of a single container Shape object, whether or not it contains member shapes.

The Selection.MoveToSubprocess method takes the parameters shown in Table 2.

Table 2. Selection.MoveToSubprocess parameters

Name

Required/Optional

Data type

Description

Page

Required

Page

The subprocess page to which the selection should be moved. You cannot pass the current page.

ObjectToDrop

Required

[UNKNOWN]

The replacement shape to drop.

NewShape

Optional

Shape

Out parameter. Returns the shape that is linked to the new page.

ObjectToDrop is typically a Visio object, such as a Master or MasterShortcut object. However, it can be any OLE object that provides an IDataObject interface. If ObjectToDrop is null, Visio drops a default shape.

The following code example shows how to use the Selection.MoveToSubprocess method to move a selection of shapes from the main process (source) page to a subprocess page. It selects a group of shapes and then drops a SubProcess master shape on the source page to take the place of the selected shapes and to link to the subprocess page.

Paste the following code into the Visual Basic Editor and run it.

Public Sub MoveSelection()

    Dim vsoSelection As Selection
    Dim vsoPageSource As Page
    Dim vsoPageTarget As Page
    Dim vsoShapeNew As Shape
    Dim vsoMaster As Master
    Dim vsoShapeSelection1 As Shape
    Dim vsoShapeSelection2 As Shape
    Dim vsoShapeSelection3 As Shape
    Dim vsoShapeSelection4 As Shape
    
    Set vsoPageSource = ThisDocument.Pages("Page-1")
    Set vsoPageTarget = ThisDocument.Pages("SubProcessPage2")
    Set vsoMaster = Visio.Documents("BASFLO_M.VSS").Masters("SubProcess")
    
    ' Specify the shapes to select.
    Set vsoShapeSelection1 = vsoPageSource.Shapes("Process.4")
    Set vsoShapeSelection2 = vsoPageSource.Shapes("Decision.10")
    Set vsoShapeSelection3 = vsoPageSource.Shapes("Process.12")
    Set vsoShapeSelection4 = vsoPageSource.Shapes("Process.14")
    
    ActiveWindow.DeselectAll
    Set vsoSelection = ActiveWindow.Selection
    
    vsoSelection.Select vsoShapeSelection1, visSelect
    vsoSelection.Select vsoShapeSelection2, visSelect
    vsoSelection.Select vsoShapeSelection3, visSelect
    vsoSelection.Select vsoShapeSelection4, visSelect
    
    vsoSelection.MoveToSubprocess vsoPageTarget, vsoMaster, vsoShapeNew
    
End Sub

After you run the code, your source page should look like Figure 4.

Figure 4. Source page

Source page

The subprocess page should look like Figure 5.

Figure 5. Subprocess page

Subprocess page

Conclusion

This article explains how to use three new methods in the Visio 2010 Visual Basic for Applications (VBA) object model to create a Visio 2010 subprocess flowchart drawing. The methods create new subprocess pages and move shapes and selections of shapes to those pages.

Additional Resources

Adding Structure to Diagrams in Visio 2010

Visio Developer Center

Visio Insights Blog

Office Client Developer Content Blog