Tips and examples for becoming a proficient Visio VB programmer

Posted on 2007-10-01
Last Modified: 2008-01-09
I am a proficient VB developer in Word, Access, Excel, and VB6.  I would like to become one in Visio.  I would like to know of a site or two that would have lots of examples of VB code doing commonplace things in Visio, like the code to drag and drop an item from a stencil to a document being a prime example.  Further, I wonder if it's possible to have Visio record my actions and give me the code it produced, the way Word and Excel do.  Thanks much, Peter Ferber
Question by:PeterFrb
    LVL 2

    Expert Comment

    You can use Visio the same as Excel etc.  You can get the VBA editor up by using Alt + F11, you can create macros using Tools>Macros>Record New Macro.  Below is some sample code to add three process boxes and name them Process 1, Process 2 and Process 3.  Have fun learn some new stuff.

     Application.Documents.OpenEx "basflo_m.vss", visOpenRO + visOpenDocked

        Application.ActiveWindow.Page.Drop Application.Documents.Item("BASFLO_M.VSS").Masters.ItemU("Process"), 3.937008, 10.334646

        Application.ActiveWindow.Page.Drop Application.Documents.Item("BASFLO_M.VSS").Masters.ItemU("Process"), 3.937008, 9.153543

        Application.ActiveWindow.Page.Drop Application.Documents.Item("BASFLO_M.VSS").Masters.ItemU("Process"), 3.937008, 7.972441

        Dim UndoScopeID1 As Long
        UndoScopeID1 = Application.BeginUndoScope("Drop On Page")
        Dim vsoShape1 As Visio.Shape
        Set vsoShape1 = Application.ActiveWindow.Page.Drop(Application.ConnectorToolDataObject, 3.937008, 10.03937)
        vsoShape1.CellsSRC(visSectionObject, visRowXForm1D, vis1DBeginX).ResultIU = 3.937008
        vsoShape1.CellsSRC(visSectionObject, visRowXForm1D, vis1DBeginY).ResultIU = 10.03937
        vsoShape1.CellsSRC(visSectionObject, visRowXForm1D, vis1DEndX).ResultIU = 3.937008
        vsoShape1.CellsSRC(visSectionObject, visRowXForm1D, vis1DEndY).ResultIU = 9.448819
        Dim vsoCell1 As Visio.Cell
        Dim vsoCell2 As Visio.Cell
        Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(4).CellsU("BeginX")
        Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(7, 2, 0)
        vsoCell1.GlueTo vsoCell2
        Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(4).CellsU("EndX")
        Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).CellsSRC(7, 3, 0)
        vsoCell1.GlueTo vsoCell2
        Application.EndUndoScope UndoScopeID1, True

        Dim UndoScopeID2 As Long
        UndoScopeID2 = Application.BeginUndoScope("Drop On Page")
        Dim vsoShape2 As Visio.Shape
        Set vsoShape2 = Application.ActiveWindow.Page.Drop(Application.ConnectorToolDataObject, 3.937008, 8.858268)
        vsoShape2.CellsSRC(visSectionObject, visRowXForm1D, vis1DBeginX).ResultIU = 3.937008
        vsoShape2.CellsSRC(visSectionObject, visRowXForm1D, vis1DBeginY).ResultIU = 8.858268
        vsoShape2.CellsSRC(visSectionObject, visRowXForm1D, vis1DEndX).ResultIU = 3.937008
        vsoShape2.CellsSRC(visSectionObject, visRowXForm1D, vis1DEndY).ResultIU = 8.267717
        Dim vsoCell3 As Visio.Cell
        Dim vsoCell4 As Visio.Cell
        Set vsoCell3 = Application.ActiveWindow.Page.Shapes.ItemFromID(5).CellsU("BeginX")
        Set vsoCell4 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).CellsSRC(7, 2, 0)
        vsoCell3.GlueTo vsoCell4
        Set vsoCell3 = Application.ActiveWindow.Page.Shapes.ItemFromID(5).CellsU("EndX")
        Set vsoCell4 = Application.ActiveWindow.Page.Shapes.ItemFromID(3).CellsSRC(7, 3, 0)
        vsoCell3.GlueTo vsoCell4
        Application.EndUndoScope UndoScopeID2, True

        Dim vsoCharacters3 As Visio.Characters
        Set vsoCharacters3 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).Characters
        vsoCharacters3.Begin = 0
        vsoCharacters3.End = 0
        vsoCharacters3.Text = "Process 1"

        Dim vsoCharacters4 As Visio.Characters
        Set vsoCharacters4 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).Characters
        vsoCharacters4.Begin = 0
        vsoCharacters4.End = 0
        vsoCharacters4.Text = "Process 2"

        Dim vsoCharacters5 As Visio.Characters
        Set vsoCharacters5 = Application.ActiveWindow.Page.Shapes.ItemFromID(3).Characters
        vsoCharacters5.Begin = 0
        vsoCharacters5.End = 0
        vsoCharacters5.Text = "Process 3"



    Author Comment

    What version of Visio do you use? I have Visio Standard, which does not have the Record Macro feature.  Buying the upgrade could well be worthwhile, for that feature alone?  Can you tell me what you have and its cost?  Thanks, Peter

    Author Comment

    I am having problems with your code: I believe the version of VB I'm using is different than yours, since I'm coming across commands that my compiler doesn't recognize.
    If there is any site with code snippits that perform specific finite tasks using a minimum amount of code, that would be ideal.  I've had this application for a *long* time: it's Visio Standard 5.0c, copyright 1991 - 1998.  Perhaps that's another reason to go for the upgrade.
    LVL 2

    Accepted Solution

    I am using Visio 2003 Standard (Version 11) which is available as an upgrade from Amazon for £75 but they quote 4-6 weeks delivery??  You might need to check what version you need to be able to upgrade?

    Author Comment

    I will do so.  The ability to record Visio macros is completely worth the expense.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now