![]() GlueTo(shpNew.CellsU(& quot PinX"))Įnd If '// Set-up variables for the next round. GlueTo(shpLast.CellsU(& quot PinX"))Įnd If '// Second, the End cell of the connector, '// glued dynamically to shpNew: Call shpConn.CellsU(& quot EndX "). GlueTo(shpDec.CellsU(& quot Connections.X2"))Įlse '// Glue dynamically: Call shpConn.CellsU(& quot BeginX "). '// 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 "). For the 'No' on the Decision '// shape, we specifically glue to the '// connection point on the right side of '// the shape. '// 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. '// Connect shapes: If (i & lt > 1) Then '// Drop a connector on the page: Set shpConn = pg.Drop(conn, 0, 0) ShpNew.Cells(& quot Prop.Cost ").ResultIU = i shpNew.Cells(& quot Prop.Duration"). for later Else Set shpNew = pg.Drop(mstProcess, x, y)Įnd If '// Set custom properties, illustrating '// two methods: ResultIU and Result: ShpNew.Text = i & "?" Set shpDec = shpNew '//.save dec. '// Drop a new shape - either a Process or a '// Decision shape: If i = DecisionStepNumber Then Set shpNew = pg.Drop(mstDecision, x, y) ![]() Y = pg.PageSheet.CellsU( "PageHeight").ResultIU - 1 X = pg.PageSheet.CellsU( "PageWidth").ResultIU / 2 '// However, some rough positioning will help auto- '// layout to do a better job. '// Note: if we use auto-layout (see end of this '// procedure), then x- and y aren't super-critical. '// We'll draw on the first page of the document, '// which is probably the only page in the document! Set pg = (1) '// Must be at least 2 less than NumShapes.ĭim i As Integer Dim x As Double, y As Double '//.drop locations '// Test case to illustrate adding a decision shape. Note, it's '// not typed as a master! Set conn = '// - Dim mstProcess As Visio.Masterĭim conn As Variant '//.note - not a Visio.Master '// Get masters for Process and Decision: Set mstProcess = _ĭ(MasterProcessName)ĭ(MasterDecisionName) If (doc.Name = FlowchartStencilName) Then Set docFlowStencil = docĮxit For End If Next Set doc = Nothing '// Step 2: get the masters and connect. '// Search open documents for our flowchart stencil: For Each doc In Visio.Documents '// english, or short filenames, and will work for '// non-english versions of Visio:Ĭonst FlowchartTemplateName$ = "Basic Flowchart.vst"Ĭonst FlowchartStencilName$ = "BASFLO_M.VSS"Ĭonst MasterDecisionName$ = "Decision" '// Open a new document: Dim doc As Visio.Document '// We use 'universal names', which are usually U.S. '// Step 1: get the flowchart template and stencil. 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! Hopefully the comments in the code will explain this more clearly. 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. 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. This unfortunately required some If and Select Case blocks that clutter the code to some extent. The more complicated bits involve the decision branch. Just be sure that it is at least 2 less than NumShapes, or you will run in to errors! ![]() 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: The decision step is artificially created at step #3. When you run the code, you’ll the code will create a simple flowchart that looks something like this: If you inspect the code, you’ll see that the program flow directly follows the outline above. Nevertheless, it doesn’t stray too far from the core concepts that you’ll need to understand in order to automate Visio diagram creation: This isn’t the simplest example, because I thought it would be important to show how a decision-branch might be handled. I 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |