Volume 3: Programming with the Visio Object Model disclaimer



Download 0.78 Mb.
Page14/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   10   11   12   13   14   15   16   17   ...   36

Adding Data to Shapes


Earlier you learned about getting cells that represent formulas. Sometimes you may need to add cells to a shape, just as when you’re developing a shape in Visio.

For example, if a shape does not have Shape Data, you can add a Shape Data section, add rows to it, and then set formulas of cells in those rows.

Adding Sections and Rows


To add a section, you use the AddSection method and specify the type of section . Constants for these are defined in the VBA object browser.

The example uses visSectionProp, which is the constant for the Shape Data section.

Unlike adding a section in the ShapeSheet (which automatically gives you one row), when adding a section from your program you need to add the rows explicitly. To do this in a shape data section, use the AddNamedRow method, specify the section, the row name, and a row tag of zero.



Note:

The row tag should be zero for any kind of row except the Geometry section rows; for those it’s used to indicate the row type, such as LineTo.

Once you’ve added the row, you can access its cells in the usual way. You don’t need to explicitly add cells to the new row.

If you try to add a section that a shape already has, you’ll get an error. To avoid this, either use the SectionExists property to test whether the shape already has the section, or just add the row. If the section doesn’t already exist, it will be created automatically.


Lab 3.3: "Hello World"



Points to Consider


Do the procedure manually first, and note each step and the names of your template, stencil, and master.

Use X and Y coordinate variables to make dropping the four masters on the page more efficient.

Comments have been developed in a partially completed exercise to guide you through the process. Do not look at the solution code before attempting the exercise.


Review: Generating Visio Drawings



Questions


  1. If you opened a new blank drawing, how many items would the documents collection contain?

  2. What does a Cell object represent?

  3. Why are drop-coordinates always in inches?

  4. What object property would you use to replace a guarded cell formula?

Answers


  1. There would be only one document in the Documents collection since a blank drawing has no stencils opened.

  2. A Cell is an object representing a single cell in the ShapeSheet.

  3. Visio maintains internal units in inches.

  4. FormulaForce or ResultForce

9.

Getting Data From Drawings





Overview


Now you’re going to approach Visio objects from a slightly different perspective—by retrieving information about the objects that already exist in the drawing. Here’s what you are going to cover in this section:

You’ll learn about how to iterate through a collection so you can get something from, or do something to, each object in the collection.

You’ll read briefly about documents, pages, and shapes, and the kinds of information you can get from them that you might use in a program, plus you’ll discover another way of getting a Cell object using CellsSRC.

It is frequently necessary to access objects selected in the drawing. The Selection object, or set of shapes selected in a window, can be accessed through the Selection property of the Window object. The Selection can be accessed through automation or can be created through automation.

Iterating Through a Collection


You’ve seen how you can retrieve objects from collections, by name or by index. You can iterate through a collection to perform the same operation on every member, or check each member until you find the one you want.


Some Things to Remember about Collections

Most Visio collections are indexed starting at 1.

Generally, only the UI object collections are indexed starting at 0, but be careful. Hyperlink collections also start with 0. Be sure to check before you start writing code.

The Count property returns the number of items in the collection at the time you get the property; however if you add or remove objects from the collection, the value of Count will change. This could make a difference in the example above, where Count is evaluated for each pass through the loop.

Collections associated with a valid Visio object always exist, but the collections may be empty, in which case Count returns 0.

A good way to iterate through collections like page or shape collections is with the For Each… Next statement. Use it to build your object references and process shape data.

When you delete objects from a collection using an iteration loop, decrement the counter rather than incrementing it. For example:

For i = oDocs.Count to 1 Step -1
A collection is not like an array; items don’t keep a fixed index position. Instead, as you delete an item, the next item moves into that position. But if you’ve incremented your counter, you skip that item and delete the next one, until sooner or later you overreach Count and try to delete an item with an invalid index, which causes an error.

Try it! Collections

  1. Open the file <install path>Demos\Vol3\Getting Data from Drawings\ Collections.vsd.

  2. Run the sample code via right-click on the page. Each macro is an example of working with collections.

  3. ColorShapes adds a color to each shape.

  4. Form some group shapes on the page. Use CountShapes to count all of the shapes in the groups, including the groups themselves.

  5. DeleteShapes deletes all of the shapes on the page.

Note that the For loop in the code counts down from the highest indexed shape.



Download 0.78 Mb.

Share with your friends:
1   ...   10   11   12   13   14   15   16   17   ...   36




The database is protected by copyright ©ininet.org 2024
send message

    Main page