Development

Tips & code for programming Visio & using the ShapeSheet

Maps & Geography

Maps & geography shapes & templates galore!

Network

Shapes & tools for Visio network diagrams.

News

Find out what’s new in the Visio world!

Shapes

Download useful & attractive Visio shapes. For free!

where can i buy windows 7 cheapest buy windows 7 upgrade from xp windows vista purchase price buy inventor 2010 pctools spyware doctor 5.5 buy windows 7 best price norton ghost 12 product key windows 7 price future shop windows xp price singapore windows 2008 web server download omnipage coupon buy windows 7 starter download adobe indesign cs3 for mac buy vista to windows 7 microsoft project 2003 download buy windows xp operating system windows 7 home premium cheap mathcad pricing photoshop buy mac discount autocad 2009 purchase microsoft money 2007 deluxe download photoshop mac autodesk maya 2009 price windows vista business download windows 7 price 64 bit purchase windows 7 ultimate full version buy windows 7 home premium key lightroom for apple purchase windows xp india adobe cs4 photoshop buy where to buy encarta 2009 purchase vista oem buy windows 7 tesco cheap adobe fireworks windows 7 price online best price corel draw 11 mac discount powerpoint buy microsoft office for mac canada buy microsoft project 2002 adobe production premium mac turbotax 2009 deluxe best price windows 7 ultimate 64 bit best price buy windows 7 amazon adobe flash pricing adobe dreamweaver sale purchase windows 7 ultimate buy quickbooks pro 2007 roxio copy & convert 3 buy download photoshop elements 6 steinberg nuendo price uniblue registrybooster 2009 download cheap photoshop programs adobe photoshop elements cheapest buy windows 7 future shop adobe presenter 7 download microsoft office 2003 sale buy quarkxpress 8 for mac purchase adobe cs3 design premium purchase after effects cs3 paperport professional 12 prices cakewalk download parallels desktop price buy microsoft windows 7 home premium cheap adobe after effects for mac how to purchase windows 7 online download microsoft streets and trips 2008 microsoft office 2003 cheapest buy windows 7 online singapore purchase autodesk 3ds max 2009 buy windows xp pro best price adobe flash cs3 buy uniblue registrybooster 2009 cheap windows 7 ultimate retail buy autodesk autocad buy microsoft office document imaging purchase microsoft office 2003 buy windows xp media centre edition discount microsoft windows 7 ultimate windows 7 price upgrade cheap adobe photoshop software burnaware professional price buy acronis true image 2010 windows 7 price change buy acrobat 7 where to buy adobe flash cs3 quicken 2010 price buy windows xp corporate buy office 2007 student product key windows 7 price hong kong buy windows 7 64 buy acrobat professional 9 symantec winfax pro 10.4 buy cheap windows 7 keys adobe photoshop cs4 buy buy microsoft visio 2007 turbotax deluxe 2009 download windows xp price in rupees buy lightroom software buy windows 7 buy windows 7 serial key steinberg nuendo 3 download download autodesk 3ds max 9 buy ms project online windows 7 price egypt windows 2008 enterprise download parallels mac download adobe lightroom sale cheap cubase 5 where to buy windows xp service pack 2 purchase windows vista download cheap windows 7 in singapore lowest price microsoft office purchase windows 7 license key where can i buy windows 7 starter edition buy i.r.i.s. readiris pro 11 cheap windows xp operating system buy office 2003 key where to buy windows xp cheap autocad mechanical 2009 download buy matlab software buy 3ds max 2010 student download dragon naturally speaking autocad inventor download get autocad 2010 buy adobe illustrator cs2 buy windows 7 mac windows xp price canada steinberg cubase sx3 download cheap windows 2003 enterprise download adobe illustrator cs3 can i buy windows xp online windows xp price in malaysia buy adobe cs4 suite best price adobe illustrator cs3 download font folio 11 acdsee pro license key cs3 production premium mac ms streets and trips 2008 download download adobe illustrator mac windows 7 price in india where can i buy cubase sx3 windows vista download iso roxio creator 2010 coupon code microsoft office price student windows 7 home premium 64 bit oem buy microsoft frontpage software buy windows xp 64 pro cheap windows 7 home premium full version buy burnaware professional cheap microsoft works 9 microsoft visio download adobe cs4 web premium oem download disk director cheap adobe creative suite cs4 microsoft money home and business download buy parallels desktop 5 for mac cheapest norton 360 3.0 lightroom price buy windows vista pro microsoft money buy online buy microsoft expression blend 3 buy windows 7 full version for students purchase lightroom 2 buy microsoft office ultimate pdf converter professional 6 promotional code buy adobe cs3 design premium buy indesign cs3 software buy windows vista ultimate upgrade acdsee photo editor download microsoft office 2003 professional cheap windows 7 for college students illustrator cs3 download download adobe indesign cs4 cyberlink powerdirector prices buy windows vista ultimate license buy access 2003 online windows 7 price guide mathcad price buy after effects cs4 mac cheap fl studio 8 xxl cheap photoshop elements 8 buy windows vista home premium oem buy wordperfect x3 buy windows 7 home premium 64 bit buy windows 7 brisbane buy windows 7 bangkok autodesk mudbox price purchase vista license cheap adobe premiere elements 8 download adobe creative suite 4 design premium best price microsoft excel 2003 buy microsoft project 2000 microsoft expression price discount photoshop elements 8 cheap maya software 3ds max sale buy windows 7 ebay windows 7 price 2010 deskshare videoeditmagic 4.3 price buy windows xp 2010 norton 360 price comparison buy windows xp used adobe contribute mac purchase windows 7 for mac buy windows vista home premium product key buy windows 2003 datacenter buy windows 7 professional cheap microsoft windows 7 ultimate buy sony vegas pro 8 purchase access 2007 final cut express 4 cost download microsoft visio 2007 buy onenote 2003 photoshop lightroom for mac symantec pcanywhere download windows 7 price pakistan buy premiere pro cs3 cheap windows 7 for students in australia web page maker 3 download buy windows xp home sp3 adobe photoshop best buy cheap windows 2008 enterprise buy windows 7 direct download download microsoft mappoint 2009 buy windows xp home download microsoft autoroute 2007 europe ashampoo burning studio download buy windows xp calgary archicad 13 download adobe cs4 design premium trial price photoshop elements 8 windows 7 home premium cheapest autodesk lustre pricing purchase archicad 13 acdsee pro 2 download windows 7 discount canada vmware workstation 7 download discount autodesk 3ds max 2009 buy autodesk inventor professional buy windows xp home full version buy windows 7 language pack buy microsoft office 07 buy microsoft excel 2007 download propellerhead reason 4 mappoint 2009 european buy access 2007 only buy frontpage 2003 purchase windows 7 ultimate online purchase microsoft windows vista buy dragon naturally speaking 10 oil price vista gadget pinnacle studio 14 best price cheap corel video studio pro x2 buy photoshop 7 for mac buy adobe cs3 design premium mac download ashampoo burning studio 9 cheap microsoft project 2007 dvd ripper platinum 5 buy adobe premiere pro cs4 download best price microsoft windows vista windows 7 discount for high school students purchase windows 7 professional (64 bit) compare price photoshop buy windows 7 philippines buy microsoft office powerpoint 2007 purchase windows xp sp3 buy photoshop elements 8 mcafee total protection 2009 download best buy autocad 2009 buy windows vista singapore purchase windows 7 now buy windows xp mumbai buy windows 7 ultimate 64 bit oem windows 7 price kenya ms works download adobe acrobat 9 oem buy windows 7 downloadable adobe cs4 web premium trial cheap windows 7 professional best price windows 7 home premium oem download adobe photoshop elements 6 navisworks manage price windows 7 price comparison buy guitar pro 5.2 download acronis true image 11 buy nero 8 ultra edition acdsee price download adobe robohelp 8 buy powerdirector 7 ultra imsi turbofloorplan home and landscape pro 12 buy buy microsoft expression studio 2 windows 7 discount for military buy windows 7 30 download pinnacle studio 12 ultimate adobe acrobat price comparison mappoint 2009 europe buy cheap avanquest mylogomaker professional 2 buy windows xp cheap microsoft office discount code buy windows 7 switzerland buy windows xp volume license buy norton 360 download cheap adobe photoshop cs3 download microsoft expression studio 3 buy office 2010 professional fl studio 8 xxl download buy microsoft office 2007 home and student cheap adobe cs4 design premium abbyy finereader 9.0 express edition where to buy streets and trips 2010 office 2007 price student buy roxio creator 2010 cheap adobe cs4 master collection mac buy windows xp ebay cheap indesign software buy corel photoimpact x3 download avanquest mylogomaker professional 2 cheap ashampoo burning studio 8 adobe captivate pricing autocad architecture oem buy microsoft office pro 2007 where can i buy powerpoint 2003 turbo tax cost buy windows 7 india online purchase windows 7 license online buy adobe flash cs4 buy windows vista ultimate product key buy cs4 mac buy adobe indesign cs4 best buy microsoft windows 7 ultimate cheap adobe dreamweaver cs4 mac cheap windows 7 canada roxio creator 2009 special edition microsoft office 2010 price in india windows 7 price canadian buy windows xp embedded download corel draw 12 adobe creative suite 4 production premium download buy adobe photoshop cs3 extended buy windows vista business 64 bit buy ms office 2003 pro best buy quicken 2010 buy microsoft office for mac cheap buy adobe after effects cs4 cheap norton 360 3.0 best price adobe cs4 master collection windows 7 price for teachers buy windows xp student purchase adobe acrobat 8 professional buy sql server guitar pro 5 mac download
Home » Code, Development

Connect All Shapes to Each Other

Submitted by Visio Guy on March 17, 2008 – 4:59 pm | | 8784 views 9 Comments

Read Full ArticleIf you’re a developer, looking to create Visio-based solutions, then you’re going to need to programmatically connect Visio shapes together. Wouldn’t be nice to have a snippet or two to start from?

Today’s post shows you VB and C# code that instructs Visio to connect every shape to every other shape, and to create a beautiful mess, like that shown in the image at left.

Now, I strongly recommend the Visio 2007 SDK. It is an excellent, free download, and it has lots of snippets, utilities, sample code and sample applications, including examples of how to connect Visio shapes with code.

Nevertheless, I thought I’d address a particular newsgroup question that I encountered some time ago, which was:

“How can I automatically connect every shape on a page to every other shape?”

This seemed like an interesting example to me, and one that might appeal to folks who are creating network-related Visio-based solutions. And even if it’s not exactly the code you need, it’ll surely be a good place to start from.

In addition to the newsgroup query, I was moved to write this article by a YouTube video that a few whacky guys at Microsoft put together, called: MEDC 2007 Worst Practices, or according to Public Speaking for Geeks: The Best Bad Tech Speakers Video…Ever. Have a gander at some truly nightmarish Visio network diagrams, just after the halfway point…

..

Features

While the code isn’t complicated, it’s not super-simple and short. That’s because I added a few features to show off some of the things you can do with Visio automation. Among these are:

  • Setting Layout & Routing settings for the page
  • Creating and Undo Scope, so your users can undo all of the changes made by the code with one press of Ctrl + Z
  • Rudimentary filtering of shapes by Shape.Type
  • Connecting shapes via two methods: the newer Visio 2007 AutoConnect method, or the older GlueTo method

Hopefully, you’ll find the code well-commented, but let’s just run over each feature quickly:

Layout & Routing

Code for the Layout and Routing settings can be found in the procedure: m_setPageLayoutSettings. Here, the connector style for the page is set so that the connectors are straight, and proceed from center of one shape to the center of the other. Also, the Jump Style for intersecting lines is set to Gap, as shown here:

Connect All Shapes to Each Other - Connector Gaps

Undo Scope

The Undo Scope code is found in the main procedure. When the code is finished running, you’ll see a named operation Undo Connect All Shapes to Each Other in the Edit menu. This makes things a lot easier, and more clear for your users.

Connect All Shapes to Each Other - Undo Scope

The shape filtering in m_getShapesToConnect demonstrates the use of the Shape.Type property. With this information, we can decide which shapes don’t need connecting. In our sample code, ignored shapes include: Foreign Objects, 1D shapes, or Guides.

Shape Filtering

Ignoring Foreign Objects, allows us to place a VB Button on the page that calls the VBA code when clicked, but the button won’t end up with connections being glued to it.

1D shapes are most likely connectors, and you won’t want to connect connectors to other connectors!

Guides are just visual aids, and are so uninteresting, they don’t even print! <wink wink>

Two Methods for Connecting

m_connectShapes illustrates how to determine which version of Visio you are running, and then two different ways to connect shapes together.

Just before finishing this article, I realized that if you’re running a version earlier than Visio 2007, you might have to comment out a portion of the code, because it won’t properly compile — the Visio 12 method: AutoConnect doesn’t exist in Visio 2003. See the procedure for more notes.

VB code

Below is the VBA code to get the job done. You can copy the code and directly paste it into the ThisDocument module of any document’s VBA project.

The code is easily converted to VB.NET. However, you’ll need to do the following:

  1. Add a reference to the Visio 12.0 Type Library (or Visio 11.0, or…)
  2. Add the line: Imports Visio = Microsoft.Office.Interop.Visio.

To connect the shapes, simply run the ConnectAllShapes subroutine.

Public Sub ConnectAllShapes()
 
    On Error GoTo Err
 
    '// Create an undo-scope, so that we can undo all the
    '// connections with just one Ctrl + Z:
    Dim UndoID As Long
    UndoID = Visio.Application.BeginUndoScope("Connect All Shapes to Each Other")
 
    '// This is where we really get the connecting done:
 
    '// Get a Visio Page object:
    Dim pg As Visio.Page
    pg = Visio.ActivePage
 
    '// Connect all shapes on the page:
    Call m_ConnectAllShapes(pg)
 
    Call Visio.Application.EndUndoScope(UndoID, True)
 
    Exit Sub
 
Err:
    Call Visio.Application.EndUndoScope(UndoID, False)
 
End Sub
 
 
Private Sub m_ConnectAllShapes(ByRef visPg As Visio.Page)
 
    Dim shpFrom As Visio.Shape, shpTo As Visio.Shape, shpConn As Visio.Shape, shp As Visio.Shape
    Dim collShapes As Collection
    Dim i As Integer, j As Integer
 
    '// Set the page-layout settings for routing-style,
    '// jump-style, etc.
    Call m_setPageLayoutSettings(visPg)
 
    '// Add all the non-connector shapes to a VB collection:
    collShapes = m_getShapesToConnect(visPg)
 
    '// Loop through the shapes in the shapes collection:
    For i = 1 To collShapes.Count
 
        shpFrom = collShapes.Item(i)
 
        '// Connect to all the other shapes:
        For j = i + 1 To collShapes.Count
 
            shpTo = collShapes.Item(j)
 
            Call m_connectShapes(shpFrom, shpTo)
 
        Next j
 
    Next i
 
End Sub
 
Private Sub m_connectShapes(ByRef shpFrom As Visio.Shape, ByRef shpTo As Visio.Shape)
 
    '// Visio 2007 introduced a new method for connection
    '// shapes. This proc looks at the Visio version and
    '// decides whether to use the old way or the new way.
 
    Dim pg As Visio.Page
    pg = shpFrom.ContainingPage
    '// Note: if you're not running Visio 2007, this might not
    '// even compile -- you'll have to comment-out the first part
    '// of the If-Then block...
    If pg.Application.Version = "12.0" Then
 
        Call shpFrom.AutoConnect(shpTo, visAutoConnectDirNone)
 
    Else
 
        '// Drop the built-in connector object somewhere on the page:
        shpConn = pg.Drop(pg.Application.ConnectorToolDataObject, 0, 0)
 
        '// Connect its Begin to the 'From' shape:
        Call shpConn.CellsU("BeginX").GlueTo(shpFrom.CellsU("PinX"))
        '// Connect its End to the 'To' shape:
        Call shpConn.CellsU("EndX").GlueTo(shpTo.CellsU("PinX"))
 
    End If
 
End Sub
 
Private Function m_getShapesToConnect(ByRef visPg As Visio.Page) As Collection
 
    Dim shp As Visio.Shape
    Dim collShapes As Collection
 
    collShapes = New Collection
 
    '// For this example, we will get all shapes on the page
    '// that ARE NOT of these:
    '//
    '//  1. Connectors
    '//  2. Foreign objects (like Buttons)
    '//  3. Guides
 
    For Each shp In visPg.Shapes
 
        If (shp.OneD = False) And _
           (shp.Type <> Visio.VisShapeTypes.visTypeForeignObject) And _
           (shp.Type <> Visio.VisShapeTypes.visTypeGuide) Then
 
            Call collShapes.Add(shp)
 
        End If
 
    Next
 
    m_getShapesToConnect = collShapes
 
End Function
 
Private Sub m_setPageLayoutSettings(ByRef visPg As Visio.Page)
 
    '// We can set layout and routing options for the page by
    '// accessing the ShapeSheet for the page, and setting cells
    '// in the Page Layout section.
    '//
    '// You can see the PageSheet by deselecting all shapes on the
    '// page, and choosing Window > Show ShapeSheet.
 
    '// Set page routing style to center-to-center:
    visPg.PageSheet.CellsSRC(Visio.VisSectionIndices.visSectionObject, _
                             Visio.VisRowIndices.visRowPageLayout, _
                             visPLORouteStyle).ResultIUForce = 16
 
    '// Set to connector intersection to 'gap':
    visPg.PageSheet.CellsSRC(Visio.VisSectionIndices.visSectionObject, _
                             Visio.VisRowIndices.visRowPageLayout, _
                             Visio.VisCellIndices.visPLOJumpStyle).ResultIUForce = 2
 
    '// Note: another way to access the PageSheet cells is by name, ie:
    '// visPg.PageSheet.Cells("RouteStyle").ResultIU = 16
    '// visPg.PageSheet.Cells("LineJumpStyle").ResultIU = 2
 
End Sub

C# Code

I created a partial class for a Windows Form, so that it was easy to call the code via a button click. You might have to mess with the namespace and class name to get it to work, but it should just be a few lines.

You’ll also have add a reference to the Visio 12.0 Type Library (or Visio 11.0, or…) in order to get the “using Visio…” line to work.

To get the ball rolling, make sure you have an instance of Visio running. There should be a document open that has some shapes that you don’t really care about. Then, just call the m_connectAllShapes proc and the connections will appear!

using System.Collections.Generic;
using Visio = Microsoft.Office.Interop.Visio;
 
namespace ConnectAllShapes
{
    partial class Form1
    {
 
    private const string VisioAppID = "visio.application";
 
    private void m_connectAllShapes()
    {
        // First, find a running instance of Visio:
        Visio.Application visApp = m_getVisio();
        if (visApp == null)
        {
            System.Console.WriteLine(
                "Couldn't find a running instance of Visio!");
            return;
        }
 
        int UndoID = -1;
 
        try
        {
            // Create an undo-scope, so that we can undo all the
            // connections with just one Ctrl + Z:
            UndoID = visApp.BeginUndoScope("Connect All Shapes to Each Other");
 
            // This is where we really get the connecting done:
 
            // Get a Visio Page object:
            Visio.Page pg = visApp.ActivePage;
 
            // Connect all shapes on the page:
            m_connect(pg);
 
            visApp.EndUndoScope(UndoID, true);
        }
        catch (System.Exception ex)
        {
            // Try to close the undo scope, but reject the changes:
            if ( (UndoID == -1) && visApp != null)
            {
                visApp.EndUndoScope(UndoID, false);
            }
 
            System.Console.WriteLine(
                "An error occurred!\n\n" + ex.Message);
        }        
    }
 
    private void m_connect(Visio.Page visPg )
    {
        Visio.Shape shpFrom, shpTo;
        List<Visio.Shape> collShapes;
  
        // Set the page-layout settings for routing-style,
        // jump-style, etc.
        m_setPageLayoutSettings(visPg);
 
        // Add all the non-connector shapes to a VB collection:
        collShapes = m_getShapesToConnect(visPg);
  
        // Loop through the shapes in the shapes collection --
        // connect the ith shape to each jth shape, so to speak:
        for(int i = 0; i < collShapes.Count; i++)
        {
            shpFrom = collShapes[i];
  
            // Connect to all the other shapes:
            for (int j = i + 1; j < collShapes.Count; j++)
                  {      
                shpTo = collShapes[j];      
                m_connectShapes(shpFrom, shpTo);                   
                  }
        }
 
    } 
 
    private void m_connectShapes(Visio.Shape shpFrom, Visio.Shape shpTo )
    {
        // Visio 2007 introduced a new method for connection
        // shapes. This proc looks at the Visio version and
        // decides whether to use the old way or the new way.
 
        Visio.Page pg = shpFrom.ContainingPage;
         // Note: if you're not running Visio 2007, this might not
        // even compile -- you'll have to comment-out the first part
         // of the If-Then block...
        if (string.Compare(pg.Application.Version, "12.0", true) == 0)
        {
            shpFrom.AutoConnect(shpTo, 
                (short)Visio.VisAutoConnectDir.visAutoConnectDirNone, null);
        }
        else
        {
            // Drop the built-in connector object somewhere on the page:
            Visio.Shape shpConn;
            shpConn = pg.Drop(pg.Application.ConnectorToolDataObject, 0, 0);
 
            // Connect its Begin to the 'From' shape:
            shpConn.get_CellsU("BeginX").GlueTo(shpFrom.get_CellsU("PinX"));
            // Connect its End to the 'To' shape:
            shpConn.get_CellsU("EndX").GlueTo(shpTo.get_CellsU("PinX"));
 
        }
    }
 
    private List<Visio.Shape>  m_getShapesToConnect(Visio.Page visPg ) 
    {
        List<Visio.Shape> collShapes = new List<Visio.Shape>();        
        
        // For this example, we will get all shapes on the page
        // that ARE NOT of these:
        //
        //  1. Connectors
        //  2. Foreign objects (like Buttons)
        //  3. Guides
 
        foreach (Visio.Shape shp in visPg.Shapes)
        {
            if ( (shp.OneD == 0 ) &&
                 (shp.Type != (short)Visio.VisShapeTypes.visTypeForeignObject) &&
                 (shp.Type != (short)Visio.VisShapeTypes.visTypeGuide) )
            {
                collShapes.Add(shp);
            }
 
        }
  
      return collShapes;
 
  }
 
    private Visio.Application m_getVisio()
    {
        Visio.Application visApp;
        object objVis;
 
        objVis = System.Runtime.InteropServices.
            Marshal.GetActiveObject(VisioAppID);
 
        visApp = (Visio.Application)objVis;
 
        return visApp;
    }
 
    private void m_setPageLayoutSettings(Visio.Page visPg)
    {
        // We can set layout and routing options for the page by
        // accessing the ShapeSheet for the page, and setting cells
        // in the Page Layout section.
        //
        // You can see the PageSheet by deselecting all shapes on the
        // page, and choosing Window > Show ShapeSheet.
 
        // Set page routing style to center-to-center:
        visPg.PageSheet.get_CellsSRC(
            (short)Visio.VisSectionIndices.visSectionObject,
            (short)Visio.VisRowIndices.visRowPageLayout,
            (short)Visio.VisCellIndices.visPLORouteStyle).ResultIUForce = 16;
 
        // Set to connector intersection to 'gap':
        visPg.PageSheet.get_CellsSRC(
            (short)Visio.VisSectionIndices.visSectionObject,
            (short)Visio.VisRowIndices.visRowPageLayout,
            (short)Visio.VisCellIndices.visPLOJumpStyle).ResultIUForce = 2;
                               
        // Note: another way to access the PageSheet cells is by name, ie:
        //visPg.PageSheet.get_Cells("RouteStyle").ResultIU = 16;
        //visPg.PageSheet.get_Cells("LineJumpStyle").ResultIU = 2;
   
    }
    }
}

…More comments after the C# code…

Related Posts

  1. Automatically Number & Connect Shapes – With Code!
  2. Create Visio Flowcharts Programmatically
  3. Save Time & Simplify Your VBA Code With CallByName
  4. Combo Box Table of Contents
  5. LengthIU Bug Workaround Code

9 Comments »

  • Art Braune says:

    Wow – this is the weirdest Visio request and solution I have seen so far…

  • Visio Guy says:

    Hi Art,

    Well storage is cheap these days…

    …and like I mentioned above, it might be a good piece of code to start with.

    A Visio developer could modify the shape-filtering in m_shapesToConnect to do something a bit more useful – like connect all Routers together or something.

    - Chris

  • gabriel says:

    Hi

    I have never used VBeditor before and I am trying to use your script. I was able to insert the script in the Visual Basic Editor through Visio 2007 but then I am not sure what to do after that. I chose the macro after this step but it didnt do anything. Can you explain to this dummy how to get it going?

    thanks!

  • Visio Guy says:

    Hi Gabriel,

    Well, that’s a big subject to cover, but I can point you to some articles that might help:

    John Goldsmith’s visLog has a nice introductory article to get you going on Visio’s ShapeSheet and VBA – Visual Basic for Applications – coding. See this article: Just for starters

    Also, a few articles on Visio Guy will be of help:
    VBA Macro Security
    Run VBA Code When Documents Open

  • gabriel says:

    hi

    i looked over the resources but still havent figured it out. I am not looking to learn vba, I just want to run this one script. Do I need to change variables in your script? Like since I’ve named my pages should I change the “pg” in “Call m_ConnectAllShapes(pg)” to the name of one of my pages?

    help?
    gabriel

  • Visio Guy says:

    Hi Gabriel

    You can get a page a number of ways:

    dim pg as Visio.Page
    Set pg = Visio.ActiveDocument.Pages.Item(3) ‘…index
    Set pg = Visio.ActiveDocument.Pages.Item(“Page-1″) ‘…name

    You can loop through all pages like this:

    For Each pg in doc.Pages

    Next pg

    - Chris

  • gabriel says:

    are there any other variables that need to be changed?

    i just want to connect my shapes to each other…

  • Thanks Chris, this did the trick.
    Used some parts of you code to connect shapes.
    Some small changes, and now it connects using the connection points instead of the shape itself.

    - Lars

  • Visio Guy says:

    Lars-Erik is BACK!

    I am glad you figured out something useful to do with this, and that you realized it was just supposed to be sample code for instructional purposes.

    Some people seemed to think the example itself was supposed to be for real-world situations! (Hehehe I’m just teasing Art Braune!)

    If people are wondering how to glue to specific connection points, then here’s some more info.

    Say we’ve got two shapes. One has a connection point named “out” and the other has a connection point named “in”. The cell names for these points would look like:

    Shape 1
    Connections.out (or “.out.x”)
    Connections.out.y

    Shape 2
    Connections.in (or “.in.x”)
    Connections.in.y

    You can glue a connector to the specific points by using these cell names:

    // Connect its Begin to the ‘From’ shape: shpConn.get_CellsU(“BeginX”).GlueTo(shpFrom.get_CellsU(“Connections.out”));

    // Connect its End to the ‘To’ shape: shpConn.get_CellsU(“EndX”).GlueTo(shpTo.get_CellsU(“Connections.in”));

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.