IT tutorials
 
Office
 

Microsoft Visio 2010 : Introducing Automation and VBA Code (part 1) - Exploring the VBA Development

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
4/18/2014 9:36:01 PM

You’ve seen that the ShapeSheet is a nifty tool you can use to turn your Visio shapes into intelligent graphical mechanisms. For some tasks, using a programming language is more appropriate. Luckily, Visio has a built in development environment, just like other applications in the Microsoft Office suite.

Visual Basic for Applications, or VBA, is a programming language similar to BASIC, which has been around for eons and is renowned for its friendly, English-based syntax.

Exploring the VBA Development

Because the VBA environment is built in to Visio, you can start using it in a matter of seconds. It is a wonderful tool for experimenting with Visio’s object model, properties and methods, and offers a quick way to get your developer feet wet, so to speak.

Exploring the VBA Development Environment
1.
Start with the drawing in which you’ve been creating the notes shape.

2.
Create a few copies of the notes shape and perhaps draw some rectangles on the page as well. Make sure that the shapes have text in them.

3.
Start the VBA environment. In the Code group on the Developer tab, click the big Visual Basic button. Or you can do what I do many times every day: press Alt+F11. The Microsoft Visual Basic for Applications development environment appears in its own application window, separate from Visio

4.
Make sure the Project Explorer and Immediate windows are both visible. You can turn them on via the View menu. The Project Explorer is usually on the left, and the Immediate window is at the bottom.

5.
The Immediate window is great for exploring bits of automation code. You can type single lines of code into the Immediate window and learn Visio VBA bit by bit. For example, in the Immediate window, type

? Visio.ActivePage.Shapes.Count

Then press Enter. The number of shapes on the active Visio page is printed on the next line, as shown in Figure 1.

Figure 1. Using the Immediate window to query Visio. In this window, “?” is shorthand for “Print” and instructs VBA to output the result of the following statement.

6.
You can get the name of the current page by entering the following:

? Visio.ActivePage.Name

7.
Query Visio for the number of pages in your document by typing

? Visio.ActiveDocument.Pages.Count

8.
Try outputting the text from various shapes by entering

? Visio.ActivePage.Shapes.Item(1).Text

You can change the number in the parentheses to get the nth shape on the page.

9.
Switch back to Visio quickly by pressing Alt+Tab. This is a good shortcut because you need to frequently switch between VBA and Visio.

10.
Draw two rectangles on the page and make sure they are selected.

11.
Back in VBA, instruct Visio to connect the two rectangles as follows:

ActiveWindow.Selection(1).AutoConnect    ActiveWindow.Selection(2),
visAutoConnectDirDown

Note that there is no ? at the beginning of this statement. You aren’t asking for an output; you are telling Visio to do something. In this case, to connect the first selected item to the second, in the down direction. Note that you used shorthand by not typing out Item. Instead of Selection.Item(1), you can type Selection(1). Figure 2 shows the result.

Figure 2. Connecting two selected shapes using a code snippet in the Immediate window.

12.
The Immediate window is fine for testing bits of code, but eventually you will need several lines of code, and you will want to save them. You can create procedures that will be saved with the document. Let’s make a simple one now.

Double-click on ThisDocument in the Project Explorer. A blank white area occupying the bulk of the VBA window appears, along with a blinking text cursor. This is the code module called ThisDocument.

13.
In the code window, type Public Sub HelloVisio and press Enter. VBA adds empty parentheses to the end of your line and closes the procedure with the line End Sub.

14.
Between the two lines, type the following code so that your subroutine looks like this:

Public Sub HelloVisio()
Dim s As String
s = "Page: " & Visio.ActivePage.Name
s = s & " has " & Visio.ActivePage.Shapes.Count
s = s & " shapes."
MsgBox s
End Sub

A few notes about this: s is a variable, which you declared to be a string in the first line, using the Dim statement. You then built s into a message by concatenating bits of text. For strings, you use the & operator to add chunks of text together. So ”Visio” & “Guy” = “Visio Guy”. Finally, you invoke a pop-up message box to display the text stored in s. The code in HelloVisio will pop up a message displaying the name of the active page, and the number of shapes it contains.

15.
Save your document. Click the disk icon in the top-left corner of the VBA window, and your code will be saved inside of the Visio document. Having two separate application windows for Visio and VBA makes it feel like the code is separate from the diagram, but that is not the case. Code and diagram are saved in the same file, and you can save from Visio or from the VBA environment.

Note that Visio doesn’t have a central repository for macros like Word does—there is no “Normal.dot” for holding code that can be accessed from all documents. However, stencils can also have VBA code, which can serve a similar purpose.

16.
Run your new sub. First, place the cursor anywhere inside your new block of code. Then click the Play button in the toolbar; it’s the one with the right-pointing arrow and shows the ToolTip Run Sub/User Form(F5). I find it much easier to just press F5 to run code. A message box pops up declaring something like “Page-1 has 12 shapes.” Try adding and deleting shapes from the active page and re-running your subroutine.

You can also run macros from Visio by clicking the Macros button on the View and Developer tabs.

17.
You can also send information from a subroutine to the Immediate window. This lets you analyze a Visio diagram programmatically. Add this procedure after the HelloWorld subroutine:

Public Sub ListShapes()
Dim i As Integer
Dim shp As Visio.Shape
For Each shp In Visio.ActivePage.Shapes
i = i+1
Debug.Print i & ". " & shp.Text
Next

End Sub

Debug.Print instructs VBA to output information to the Immediate window.

18.
Place your cursor inside ListShapes and run the procedure. Figure 3 shows a possible result.

Figure 3. Sub ListShapes outputs the text of every shape on the page to the Immediate window. Because item 7 lists no text, it must be the connector, since it is the only shape in the drawing that has no text!

 
Others
 
- Microsoft Access 2010 : Introduction to Relational Database Design (part 2) - Normalization and Normal Forms
- Microsoft Access 2010 : Introduction to Relational Database Design (part 1) - Rules of Relational Database Design
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 6) - Adding Right-Click Actions to the SmartShape
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 5) - Modifying the Text Block Using the ShapeSheet
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 4) - Linking Subshape Text to Shape Data Fields
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 3) - Controlling Grouped Shapes with the ShapeSheet
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 2) - Creating Smart Geometry in the ShapeSheet
- Developing Custom Microsoft Visio 2010 Solutions : Creating SmartShapes with the ShapeSheet (part 1) - Introducing the ShapeSheet
- Developing Custom Microsoft Visio 2010 Solutions : Introducing the Notes Shape, Using the Developer Ribbon Tab
- Microsoft Excel 2010 : Working with Graphics - Inserting Clip Art, Inserting a Picture from File
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us