• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Visio Guy

Smart graphics for visual people




  • Home
  • Hire Me
    • Hire Me
    • Résumé
  • Products
    • Products
    • Bubble Revision Shape
    • Layers to Pages Utility
    • Rack Unit Dimension Line
    • Radial Elements Tool with up to 100 Wedges
    • Text on a Circle Visio SmartShape
  • Index
    • Articles by Date
    • YouTube – VisioGuy
    • Download Information
    • Suggestion Box
    • Shop
    • Visio Art
    • Visio Links
    • – Visio Shapes & Stencils
    • – Visio Templates & Drawings
  • About
    • About
    • Donate
    • GitHub
    • jsFiddle
    • Reading List
    • Subscribe & Follow
      • – E-mail
      • – facebook
      • – Twitter
      • – RSS
    • Privacy Policy
  • Discussion Forum
You are here: Home / Development / Code / Create Visio Flowcharts Programmatically

Create Visio Flowcharts Programmatically

September 13, 2006 By Visio Guy 51 Comments

Read Full ArticleI hear quite often the question; “How do I do something in Visio programmatically?” Since many of those requests pertain to flowcharts and organizational charts, I thought I’d conjure up a fairly simple example that illustrates the creation of a flowchart with Visual Basic for Applications (VBA) code.

This isn’t the simplest example, because I thought it would be important to show how a decision-branch might be handled.

Nevertheless, it doesn’t stray too far from the core concepts that you’ll need to understand in order to automate Visio diagram creation:

  • Open a template
  • Get stencils and master objects
  • Drop shapes on the page
  • Set shape text and Custom Properties
  • Connect the shapes

If you inspect the code, you’ll see that the program flow directly follows the outline above.

When you run the code, you’ll the code will create a simple flowchart that looks something like this:

Simple Flowchart

The decision step is artificially created at step #3. By artificially, I mean that there is no data-source or supreme ethereal logic that dictates “that a decision should be here.” It’s simply the product of our defined constant:

Const DecisionStepNumber% = 3

which you can change at your leisure. Just be sure that it is at least 2 less than NumShapes, or you will run in to errors!

The more complicated bits involve the decision branch. This unfortunately required some If and Select Case blocks that clutter the code to some extent. These decision blocks control how the connections are programmed, to which shapes the connections are glued, how x- and y-offsets are specified, and what the text on the connectors should be. For instance, the “No” connector connects to the right-connection point of the Decision shape, whereas the rest of the connections connect dynamically to the shapes.

Hopefully the comments in the code will explain this more clearly. Below, I’ve included a link to a Visio diagram with more instructions, a convenient “Draw” button in place, and a ready-to-go VBA project that you can inspect and alter to your liking!

Here’s the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Sub CreateFlowchart()
 
  '// Step 1: get the flowchart template and stencil.
  '// -------------------------------------------------
  '// Define some name-constants.
  '// We use 'universal names', which are usually U.S.
  '// english, or short filenames, and will work for
  '// non-english versions of Visio:
  Const FlowchartTemplateName$ = "Basic Flowchart.vst"
  Const FlowchartStencilName$ = "BASFLO_M.VSS"
  Const MasterProcessName$ = "Process"
  Const MasterDecisionName$ = "Decision"
 
  '// Open a new document:
  Dim doc As Visio.Document
  Dim docFlowTemplate As Visio.Document
  Dim docFlowStencil As Visio.Document
  Set docFlowTemplate = Visio.Documents. _
  Add(FlowchartTemplateName)
 
  '// Search open documents for our flowchart stencil:
  For Each doc In Visio.Documents
        If (doc.Name = FlowchartStencilName) Then
      Set docFlowStencil = doc
      Exit For
        End If
    Next
  Set doc = Nothing
 
  '// Step 2: get the masters and connect.
  '// ------------------------------------------------
  Dim mstProcess As Visio.Master
    Dim mstDecision As Visio.Master
    Dim conn As Variant '//...note - not a Visio.Master
 
  '// Get masters for Process and Decision:
  Set mstProcess = _
      docFlowStencil.Masters.ItemU(MasterProcessName)
  Set mstDecision = _
      docFlowStencil.Masters.ItemU(MasterDecisionName)
 
  '// Get the built-in connector object. Note, it's
  '// not typed as a master!
  Set conn = Visio.Application.ConnectorToolDataObject
 
  '// Step 3: Drop the masters
  '// -----------------------------------------------
 
  Const NumShapes% = 7
 
  '// Test case to illustrate adding a decision shape.
  '// Must be at least 2 less than NumShapes.
  Const DecisionStepNumber% = 3
 
  Dim i As Integer
  Dim x As Double, y As Double '//...drop locations
 
  Const dx# = 1.5
  Const dy# = 1
 
  Dim pg As Visio.Page
  Dim shpNew As Visio.Shape
  Dim shpLast As Visio.Shape
  Dim shpConn As Visio.Shape
  Dim shpDec As Visio.Shape
 
  '// We'll draw on the first page of the document,
  '// which is probably the only page in the document!
  Set pg = docFlowTemplate.Pages.Item(1)
 
  '// Note: if we use auto-layout (see end of this
  '// procedure), then x- and y aren't super-critical.
  '// However, some rough positioning will help auto-
  '// layout to do a better job.
 
  '// Get the center, top of the page:
  x = pg.PageSheet.CellsU("PageWidth").ResultIU / 2
  y = pg.PageSheet.CellsU("PageHeight").ResultIU - 1
 
  For i = 1 To NumShapes
 
    '// Drop a new shape - either a Process or a
    '// Decision shape:
    If i = DecisionStepNumber Then
      Set shpNew = pg.Drop(mstDecision, x, y)
      shpNew.Text = i & "?"
      Set shpDec = shpNew '//...save dec. for later
    Else
      Set shpNew = pg.Drop(mstProcess, x, y)
      shpNew.Text = "Do Step " & i
    End If
 
    '// Set custom properties, illustrating
    '// two methods: ResultIU and Result:
    shpNew.Cells(& quot;Prop.Cost").ResultIU = i
    shpNew.Cells(& quot;Prop.Duration").
          Result(Visio.VisUnitCodes.visElapsedHour) = i
 
    '// Connect shapes:
    If (i & lt;> 1) Then
 
      '// Drop a connector on the page:
      Set shpConn = pg.Drop(conn, 0, 0)
  
      '// Glue the connector to shpLast and shpNew:
      '// Note about glueing: By glueing to the PinX
      '// or PinY of a shape, we get 'Dynamic Glue'
      '// automatically. For the 'No' on the Decision
      '// shape, we specifically glue to the
      '// connection point on the right side of
      '// the shape.
      '// First, the Begin cell of the connector,
      '// glued conditionally to shpLast:
      If (i = DecisionStepNumber + 1) Then
        '// Glue to the right side:
        Call shpConn.CellsU(& quot;BeginX").
        GlueTo(shpDec.CellsU(& quot;Connections.X2"))
      Else
        '// Glue dynamically:
        Call shpConn.CellsU(& quot;BeginX").
        GlueTo(shpLast.CellsU(& quot;PinX"))
      End If
 
      '// Second, the End cell of the connector,
      '// glued dynamically to shpNew:
      Call shpConn.CellsU(& quot;EndX").
      GlueTo(shpNew.CellsU(& quot;PinX"))
 
    End If
 
    '// Set-up variables for the next round.
    Set shpLast = shpNew
  
    '// x, y, and shpLast depend on our
    '// decision-branch:
    Select Case i
 
      Case DecisionStepNumber
        x = x + dx
      Case (DecisionStepNumber + 1)
        x = x - dx
        y = y - dy
        shpConn.Text = "No"
        Set shpLast = shpDec
      Case Else
        y = y - dy
      End Select
 
    Next i
 
  '// Step 4: Layout the shapes and deselect.
  '// ------------------------------------------------
 
  '// Page-layout is one simple call, but look and
  '// see what our x and y drops have done first,
  '// then uncomment the next line:
  'pg.Layout
 
  '// Deselect all shapes, so it looks better:
  Visio.ActiveWindow.DeselectAll
 
  '// Tile the windows so the user doesn't freak out!
  Call Visio.Windows.
  Arrange(Visio.visArrangeTileVertical)
 
End Sub
Sub CreateFlowchart()

  '// Step 1: get the flowchart template and stencil.
  '// -------------------------------------------------
  '// Define some name-constants.
  '// We use 'universal names', which are usually U.S.
  '// english, or short filenames, and will work for
  '// non-english versions of Visio:
  Const FlowchartTemplateName$ = "Basic Flowchart.vst"
  Const FlowchartStencilName$ = "BASFLO_M.VSS"
  Const MasterProcessName$ = "Process"
  Const MasterDecisionName$ = "Decision"

  '// Open a new document:
  Dim doc As Visio.Document
  Dim docFlowTemplate As Visio.Document
  Dim docFlowStencil As Visio.Document
  Set docFlowTemplate = Visio.Documents. _
  Add(FlowchartTemplateName)

  '// Search open documents for our flowchart stencil:
  For Each doc In Visio.Documents
        If (doc.Name = FlowchartStencilName) Then
      Set docFlowStencil = doc
      Exit For
        End If
    Next
  Set doc = Nothing

  '// Step 2: get the masters and connect.
  '// ------------------------------------------------
  Dim mstProcess As Visio.Master
    Dim mstDecision As Visio.Master
    Dim conn As Variant '//...note - not a Visio.Master

  '// Get masters for Process and Decision:
  Set mstProcess = _
      docFlowStencil.Masters.ItemU(MasterProcessName)
  Set mstDecision = _
      docFlowStencil.Masters.ItemU(MasterDecisionName)

  '// Get the built-in connector object. Note, it's
  '// not typed as a master!
  Set conn = Visio.Application.ConnectorToolDataObject

  '// Step 3: Drop the masters
  '// -----------------------------------------------

  Const NumShapes% = 7

  '// Test case to illustrate adding a decision shape.
  '// Must be at least 2 less than NumShapes.
  Const DecisionStepNumber% = 3
 
  Dim i As Integer
  Dim x As Double, y As Double '//...drop locations
 
  Const dx# = 1.5
  Const dy# = 1
 
  Dim pg As Visio.Page
  Dim shpNew As Visio.Shape
  Dim shpLast As Visio.Shape
  Dim shpConn As Visio.Shape
  Dim shpDec As Visio.Shape

  '// We'll draw on the first page of the document,
  '// which is probably the only page in the document!
  Set pg = docFlowTemplate.Pages.Item(1)
 
  '// Note: if we use auto-layout (see end of this
  '// procedure), then x- and y aren't super-critical.
  '// However, some rough positioning will help auto-
  '// layout to do a better job.

  '// Get the center, top of the page:
  x = pg.PageSheet.CellsU("PageWidth").ResultIU / 2
  y = pg.PageSheet.CellsU("PageHeight").ResultIU - 1
 
  For i = 1 To NumShapes
 
    '// Drop a new shape - either a Process or a
    '// Decision shape:
    If i = DecisionStepNumber Then
      Set shpNew = pg.Drop(mstDecision, x, y)
      shpNew.Text = i & "?"
      Set shpDec = shpNew '//...save dec. for later
    Else
      Set shpNew = pg.Drop(mstProcess, x, y)
      shpNew.Text = "Do Step " & i
    End If

    '// Set custom properties, illustrating
    '// two methods: ResultIU and Result:
    shpNew.Cells(& quot;Prop.Cost").ResultIU = i
    shpNew.Cells(& quot;Prop.Duration").
          Result(Visio.VisUnitCodes.visElapsedHour) = i

    '// Connect shapes:
    If (i & lt;> 1) Then
 
      '// Drop a connector on the page:
      Set shpConn = pg.Drop(conn, 0, 0)
  
      '// Glue the connector to shpLast and shpNew:
      '// Note about glueing: By glueing to the PinX
      '// or PinY of a shape, we get 'Dynamic Glue'
      '// automatically. For the 'No' on the Decision
      '// shape, we specifically glue to the
      '// connection point on the right side of
      '// the shape.
      '// First, the Begin cell of the connector,
      '// glued conditionally to shpLast:
      If (i = DecisionStepNumber + 1) Then
        '// Glue to the right side:
        Call shpConn.CellsU(& quot;BeginX").
        GlueTo(shpDec.CellsU(& quot;Connections.X2"))
      Else
        '// Glue dynamically:
        Call shpConn.CellsU(& quot;BeginX").
        GlueTo(shpLast.CellsU(& quot;PinX"))
      End If
 
      '// Second, the End cell of the connector,
      '// glued dynamically to shpNew:
      Call shpConn.CellsU(& quot;EndX").
      GlueTo(shpNew.CellsU(& quot;PinX"))
 
    End If

    '// Set-up variables for the next round.
    Set shpLast = shpNew
  
    '// x, y, and shpLast depend on our
    '// decision-branch:
    Select Case i
 
      Case DecisionStepNumber
        x = x + dx
      Case (DecisionStepNumber + 1)
        x = x - dx
        y = y - dy
        shpConn.Text = "No"
        Set shpLast = shpDec
      Case Else
        y = y - dy
      End Select

    Next i

  '// Step 4: Layout the shapes and deselect.
  '// ------------------------------------------------
 
  '// Page-layout is one simple call, but look and
  '// see what our x and y drops have done first,
  '// then uncomment the next line:
  'pg.Layout
 
  '// Deselect all shapes, so it looks better:
  Visio.ActiveWindow.DeselectAll
 
  '// Tile the windows so the user doesn't freak out!
  Call Visio.Windows.
  Arrange(Visio.visArrangeTileVertical)

End Sub

Interesting flowchart links:

  • CVF 3.0 Code Visual to Flowchart. A source-code flowcharting tool from Fatesoft.
  • Fast Flowcharts An article from Mai-lan’s blog about using Visio’s mutli-form “Flowchart shapes” master.
  • Unistep Software for animating process flow, uml sequence and other types of Visio flowcharts.
  • John Marshall’s excellent collection of Visio 3rd Party links. Search for ‘flow’ and you’ll get lots of results!
  • A nice user tutorial from Informit.com on creating Visio flowcharts and using custom properties, but also dabbles in ShapeSheet and report-generation.

Download “CreateFlowchart.zip”

s!Aj0wJuswNyXlhTC2nQjcRebWyXVB – Downloaded 3007 times –
  • Tweet
  • More
  • Pocket
  • Share on Tumblr
  • Print
  • Email

Related posts:

  1. Connect All Shapes to Each Other
  2. The Hidden World of Visio Shapes
  3. Customized Visio HTML Export
  4. Circular Text Generator (version 1)
  5. Text to the Bottom of the Shape

Filed Under: Code Tagged With: Code, Connectors, Create Diagrams Programmatically, Custom Properties, Drop Visio Shapes, Flowchart, Programming, Shape Data

Previous Post: « The Hidden World of Visio Shapes
Next Post: Export All Pages in Document »

Reader Interactions

Comments

  1. Kunapa Sai says

    May 12, 2021 at 9:20 am

    Hi Visio Guy,

    We are planning to generate a Visio file from Excel Macro Enabled file. Data is entered as per the guidelines from Microsoft and we can generate Visio file by exporting the data to Visio manually. However, when we tried to automate this process, its failing as Excel and Visio are expected to be On Focus while the operations are going on.

    Please can you share your thoughts on how to handle this? Our requirement is to generate Visio files using this Macro Enabled Excel file data unattended at a server.

    Thanks in advance,
    Kunapa

« Older Comments

Leave a Reply Cancel reply

Primary Sidebar

Buy Text on a Circle Shape
Article — Video — Purchase

Categories

Buy my book!

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tag Cloud

A/V Artistic Effects BPM Code Connectors Control Handles Countries Custom Patterns Custom Properties Data Graphics Data Linking Data Visualization David Edson David Parker Fill Format Formulas Functions Geometry Gradient Images Links Maps Multi-shapes Network Programming repeating shapes Resources Right-Click Actions Scale Shape Data ShapeSheet ShapeSheet Formulas ShapeSheet Functions SharePoint shiny SmartShapes Sport Sports Text Themes Tools Transparency User-defined Cells Visio 2007 Visio SmartShapes

Top Posts & Pages

  • - Visio Shapes & Stencils
  • - Visio Templates & Drawings
  • Sankey Diagram Shapes for Visio
  • Bubble Revision Shapes
  • Dynamic Updating Org Charts in Visio!
  • Text Along a Connector's Path in Microsoft Visio 2010
  • Amazon AWS Visio Shapes
  • Visio Network Server Shape Icon Customization Tool
  • Audio Visual Components Shapes
  • Go 3D with Free Isometric Piping Shapes for Visio

www.visguy.com - Visio Guy - since 2006