Working with Controls at Run Time

3 4

An ActiveX control typically exposes properties and methods you can use at run time to work with the control programmatically. For example, a listbox control has a ListIndex property that returns the index of the selected item and a Text property that returns the text of the item at that index.

About Control Names

A control has two names: a Microsoft Visio name and a Microsoft Visual Basic for Applications (VBA) name. Initially, these names are identical, consisting of the control type plus an integer that makes the name unique. For example, the first listbox control you insert into a drawing is given the name ListBox1, and you can use this name to refer to the control in both VBA code and in Visio. (Visio shapes follow a different naming convention. For details about the naming conventions, see Chapter 16, Working with Visio Document, Page, and Shape objects.)

Although initially set to the same value, the two names are programmatically distinct and cannot be used interchangeably.

  • You use a control's VBA object name to refer to the control in VBA code. You change this name by setting the control's (Name) property in the VBA properties window in the Visual Basic Editor. You cannot use a control's VBA object name to get the corresponding Shape object from a Visio collection, such as the Shapes collection; instead, you must use the control's Shape.Name property—for example, ListBox1.Shape.Name.
  • You use a control's Visio name to get the Shape object that represents the control from a Visio collection, such as OLEObjects or Shapes. You change this name by editing it in the Name box in the Special dialog box in Visio (select the control and click Special on the Format menu) or by setting the control's Shape.Name property in VBA. You cannot use a control's Visio name to refer to the control in VBA code.

Tip


For your own convenience, if you change one name from the default value, you might want to change the other name so that the control's VBA and Visio names are identical.

Getting a Control from the OLEObjects Collection

You can get the Shape object that represents a control from the OLEObjects collection of a Microsoft Visio Document, Page, or Master object. You can also get a control from the Shapes collection of a page or master. However, it's faster to use the OLEObjects collection because it contains only linked or embedded objects, whereas the Shapes collection also includes all of the linked or embedded objects plus the Visio shapes—potentially many more objects.

The OLEObjects collection contains an OLEObject object that represents each linked or embedded object in a Visio document, page, or master, plus any ActiveX controls. The Object property of an OLEObject returns a reference to the linked or embedded object that you can use to access the object's properties and methods.

You can retrieve a control from the OLEObjects collection by its index within the collection or by the name assigned to the control in Visio. Initially, this name is identical to the value of the control's VBA object name, as described in the previous topic, About Control Names. For example, the following statements get a shape named ListBox1:

 Dim g_listbox As Object Set g_listbox = _       Document.OLEObjects("ListBox1").Object 

If you want to perform the same operation on all controls, iterate through the OLEObjects collection and check the ForeignType property of each OLEObject object to see whether the visTypeIsControl bit is set. If ForeignType and visTypeIsControl are both True, the object is an ActiveX control.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net