We help IT Professionals succeed at work.

Tips and examples for becoming a proficient Visio VB programmer

1,744 Views
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
Comment
Watch Question

. .

Commented:
Hi,
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.Windows.ItemEx("Drawing5").Activate
    Application.ActiveWindow.Page.Drop Application.Documents.Item("BASFLO_M.VSS").Masters.ItemU("Process"), 3.937008, 10.334646

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

    Application.Windows.ItemEx("Drawing5").Activate
    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"

Regards

EuroHammer
PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
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
PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
PeterFrbData anslyst, tableau visualization developer, vb certified

Author

Commented:
I will do so.  The ability to record Visio macros is completely worth the expense.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.