Author Topic: Code to Modify ShapeSheet Cells of Many Shapes  (Read 5447 times)

0 Members and 1 Guest are viewing this topic.

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1721
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Code to Modify ShapeSheet Cells of Many Shapes
« on: May 13, 2008, 05:10:18 PM »
oompa_l was wondering how to set the Height formula for a bunch of shapes over in this thread: http://visguy.com/vgforum/index.php?topic=47.0

I'll talk about two ways of going about this right now.

Are the shapes all instances of the same master?

If so, then do this:
1. File > Shapes > Show Document Stencil to see the local stencil, which contains masters used in the document
2. Double-click the master icon
3. Edit the formula
   This will change the formula in all the instances in all of the pages in the document -- unless -- you've already overridden formulas in individual shapes.

Write a macro

Like Cyndi Lauper sang, Coders just want to have fun. We can always get more done, more quickly (and cause more trouble) with VBA. So let's have at it:

Code
'// Get a bunch of selected shapes
Dim sel as Visio.Selection
Set sel = Visio.ActiveWindow.Selection

'// 'Do' each shape in the selection:
Dim shp as Visio.Shape
For Each shp in sel

   '// Note: here are several examples that you can
   '//         comment-out and uncomment

   '// Set the value in inches:
   shp.Cells("Width").ResultIU = 2.0

   '//...or set the value in centimeters:
   'shp.Cells("Width").Result(Visio.visUnitCodes.visCentimeters) = 50.8

   '//...or set the formula
   'shp.Cells("Width").Formula = "GUARD(Prop.Row_1)"

   '//...or override a cell that already has a GUARDed formula:
   'shp.Cells("Width").FormulaForce = "GUARD(Prop.Row_1)"

   '// You can get at cells with indices as well:
   'shp.CellsSRC(Visio.VisSectionIndices.visSectionObject, _
   '                  Visio.VisRowIndices.visRowXFormOut, _
   '                  Visio.VisCellIndices.visXFormWidth).Result = 3.0

  '// Put the cursor in a ShapeSheet cell, then hit F1 to get the
  '// Section, Row, Column indices for a cell.

Next shp



For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

oompa_l

  • Jr. Member
  • **
  • Posts: 24
Re: Code to Modify ShapeSheet Cells of Many Shapes
« Reply #1 on: May 14, 2008, 01:51:47 PM »
where can i go for explanation on implementing macros?

I prefer option 1, but I am still having trouble understanding masters...I would assume tha a change in a master would affect all instances but I have found this not to be the case. Also, when I tried to make a shape from scratch and place in the master a formula that included a formula, in this case "=Prop._VisDM_length_in/50" I was told there was an error in the formula. My guess s that it's because there is no shapesheet data and so it doesnt know what I am referring to. It seems to be a chicken-egg scenario. My is Visio finicky, or what!

oompa_l

  • Jr. Member
  • **
  • Posts: 24
Re: Code to Modify ShapeSheet Cells of Many Shapes
« Reply #2 on: May 14, 2008, 02:19:39 PM »
i tried to find my way intuitively to making that macro but I got an error:

"Compiler Error
Invalid Outside Procedure"

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1721
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Code to Modify ShapeSheet Cells of Many Shapes
« Reply #3 on: May 14, 2008, 03:47:23 PM »
The whole bunch of code needs to be wrapped in a sub, ie:

Sub DoStuff

...
...

End Sub
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

oompa_l

  • Jr. Member
  • **
  • Posts: 24
Re: Code to Modify ShapeSheet Cells of Many Shapes
« Reply #4 on: May 15, 2008, 06:11:48 PM »
woh! it worked...

and...when you have a moment, I am still unsure how to build the formula into the master shape - I was getting an error before. see above.
« Last Edit: May 15, 2008, 06:30:34 PM by oompa_l »