There are many more topics that could be covered to help get you started using VBA. Defining and using an application’s objects will be covered in the next few lessons as will learning to manage events within VBA.
Introduction to Automation
We’ve covered a few of the techniques that can be used for developing SmartShapes symbols. Now let’s cover how to use these symbols as part of a larger solution. Automation can be used to control Visio shapes, documents, and the Visio drawing environment, as well as other applications.
Following a brief introduction to general Automation (previously referred to as ActiveX Automation or OLE Automation), you will cover the basics of automation and Visio—the Visio object model and how to access the Visual Basic Editor (VBE).
A common application of automation and Visio technology is the automatic generation of Visio documents. It is common to store descriptive information in a database or a spreadsheet (describing a network, for example). It is very advantageous to get a graphical view of this data by creating a Visio document. Many times the generation of this document can be automated through the extraction of data and the creation of a document through automation. You can open a document, drop master shapes on the page, and position and size the newly created shape. All these steps can be done through automation and the techniques will be covered in Generating Drawings.
Once a drawing is created, you often need to ask questions or extract information about the drawing. These requests can be satisfied though automation. An example of this would be getting an inventory of the shapes in the drawing, starting an application such as Microsoft Excel, and creating a Bill of Materials with part numbers and price information. In Getting Data from Drawings, we will cover the basics of accessing document, page, shape, and cell properties.
As you work with drawings, you often need to know when something has occurred, a new piece of equipment has been added, or a network connection has been made. To validate the drawing, you can trap events and if needed, prompt the user for action. Managing Events covers these topics.
Let’s start by reviewing what is meant by Automation as well as some Visual Basic for Applications fundamentals.
These early exercises focus on the Visio object model and not on development environments. As such, examples are shown using Visual Basic for Applications. This should not be considered a recommendation that you use VBA for all of your application development. It’s just that VBA is a very convenient platform for learning about an application’s object model.
With Automation, one program can incorporate the functionality of another by using objects exposed by the other program.
Automation encompasses several services defined under the OLE 2 specification, such as linking and embedding objects and in-place editing. The foundation of this technology is COM (Component Object Model). COM defines the interfaces for the objects an application exposes. It is this interface structure that allows COM objects to make their services available to other applications.
The program that controls the automation is sometimes called a controller, client, or container application. This is a program you write in a language that supports Automation, for example, Visual Basic, Visual Basic for Applications, C#, or Microsoft Visual C++. You are going to focus on Visual Basic for Applications in this module, but you can use the Visio object model with Visual Basic, Visual C++, or any Microsoft Visual Studio .NET language as well.
The program being automated is sometimes called a server or source application. This is the program that exposes the objects for the controller application to use. The objects have properties and methods that the controller can use to manipulate the objects. The Visio application is the ActiveX server you are interested in today.
Some applications can act as both controllers and servers by embedding Visual Basic for Applications. Many of the Microsoft Office applications contain VBA, such as Word, Excel and Visio.
Programming a Server's Objects
Objects accessed through Automation remain with the object’s server. They are not incorporated in your program.
Everything you do with a server’s object is done by way of a reference to that object, which you assign to an object variable in your program. You manipulate the objects remotely, from your program, by setting properties, and invoking methods through the object variable.
An object reference is similar to a pointer; it is valid at run time but it is not something you can store between executions. Furthermore, having a reference doesn’t guarantee that you have an object. For example, you can get a reference to a shape on a page. However, if the user deletes the shape, you still have the reference but it’s not referring to anything useful.
The Visio drawing shows the Tech ED 97 General Session Theater which seats 10,000 and was the actual location for the original showing of this program.
This demonstration is a nice example of how multiple applications can work together by accessing the object models of each other’s application. In this scenario, Visio controls Excel by gathering information from the Visio drawing and writing it to an Excel spreadsheet. When a row is selected in the Excel spreadsheet there is code that controls Visio and selects and centers the drawing on the selected object. There is a third application which unfortunately cannot be demonstrated here, but was part of the original demo. That is the automated control of an air cannon gun that was on stage and used to fire t-shirts into the crowd. The air cannon is represented by a shape in the Visio drawing. As you move the pointer for the air cannon in Visio, the coordinates from the Visio drawing were being fed to the actual air cannon on the stage through Rockwell’s own automation interface for this device..
The drawing depicts seating, stage, demonstration machines, project units, controls, etc. Contents of the drawing can be pushed to an Excel spreadsheet by right clicking on the page and selecting "Create Asset Report". This add-on starts Excel, opens the template file TechED.xlt, and writes each component of the drawing to the spreadsheet.
Each item in the drawing has shape data which capture the manufacturer, description, number of individual items and unit cost. When placed into the spreadsheet the total cost of each item is also calculated. If any item is greater than $10,000 the row item is made Red, Bold, and a larger point size.
The Wet Bar is priced at $25,000 so it gets flagged in the spreadsheet.
From the spreadsheet there are events tied to the up and down arrow keys so that as you move through the spreadsheet using the arrow keys the corresponding Visio object is selected and centered in the drawing. NOTE: Be sure to set the option "Center selection on zoom" in the File > Options > Advancced tab for the centering to occur.
In the Visio drawing there is a T-shirt firing Air Gun on the stage. A control on the Air Gun allows you to direct the gun by moving it around the drawing. Right click on the Air Gun to either display its shape data or to display a form with the trajectory coordinates.
Try it! TechEd
Minimize all windows except the Visio window.
From your student CD, open <install path>\Demos\Vol3\Introduction to Automation\TechEd.vsd.
Right-click on the drawing page and select Create Asset Report.
This starts Microsoft Excel and creates a Bill of Materials for all of the objects in the drawing.
In Visio select File >Options > Advanced tab. Set Center Selection on Zoom. The demonstration will take advantage of this feature as you select the Visio drawing objects from the Excel spreadsheet.
Tile the Excel and Visio windows. Select a row in Excel. Notice that the corresponding item is selected in the Visio window (you may have to zoom in or out in Visio to get a better view).