VBA: Selecting Shape, Then Accessing It's Shape Data

Started by wxaggie11, March 15, 2011, 08:45:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wxaggie11

Howdy, I feel like I'm the only one who posts on this board, but I'm glad y'all don't mind and are able to help me in the right direction all of the time.

Anywho, I need to be able to select a shape from the active window and then, on the next page, drop its shape data onto the top of dropped masters on the next page. So far, I'm able to get the shape selected in the program. However, how does one go from a selection that the program understands, to a shape who's properties can be extracted and printed on new shapes?

I have properties A,B,C,D in Master Z on page 1, and want property A to go into Master A, B into Master B, C into Master C, and D into Master D on page 2. The selection is done by the user prior to running the macro.


Dim Sel As Visio.Selection
Dim Shp As Visio.Shape

Set Sel = Visio.ActiveWindow.Selection

For Each Shp In Sel
    'Do This
Next




Do I need to "set" Shp equal to something? And if so what?

Jumpy

When only one shape is selected, the master on page one you call it, that what is needed is:


Dim A as String, B as String, C as String, D as String
Dim shp as Shape, shp2 as Shape

Set shp = ActiveWindow.Selection(1)

A = shp.Cells("Prop.A").Formula
B = shp.Cells("Prop.B").Formula
C = shp.Cells("Prop.C").Formula
D = shp.Cells("Prop.D").Formula

'How to go on from here depends on how you want to distinguish the Shapes on Page 2

For Each shp2 in ActiveDocument.Pages(2).Shapes
  If shp2.Name = "A" Then
    shp2.Cells("Prop.MyProp").Formula = A
  ElseIf shp2.Name = "B" Then
   '...
   '...
   '...
  End If

Next
[\code]

aledlund

If you check the code with this project

http://visguy.com/vgforum/index.php?topic=1117.0

you'll find a couple of routines that David Parker graciously allowed me to use that copy properties between shapes.

al

wxaggie11

#3
Upon getting to the 2nd part you said, Shp2 is not being assigned a value, and so my error is that an object is required. How do I address the page, "CurrentPage.Index + 1" in the line ActiveDocument.Pages(2).Shapes ?

Good news though, the program is now getting the shape data from the selected shape on the first page.

Jumpy

If you only have two pages (thats what I assumed you had, therefore my example)
ActiveDocument.Pages(1) is the first
ActiveDocument.Pages(2) is the second page

If you know the names of the pages
ActiveDocument.Pages("PAGENAME") could work, too.

If not you can iterate through the pages like you iterate through the shaps

Dim pg as Page

For Each pg in ActiveDocument.Pages
  If pg.Name = "TheNameOfThePageINeed" Then     'OR pg.ID = TheNumberYouNeed
    'Do sth. wth. the page
  End If
Next

wxaggie11

Jumpy, the thing that keeps happening is name of the shape is not the same, and the loops end because it can't find a shape with the exact same name. However, all these shapes are actually masters.. So how do I reference the master in the loop by its name?

Jumpy

shp.master = "Mastername" ??


You will always have the problem of finding the right shape when you iterate through the page's shapes-collection. There are several possible ways:

- If you name each shape yourself  shp.Name = "shapename" will work, because you know the shapename.
- If every shape is different, comes from a different master shp.Master = "mastername" will work.
- If every shape has a different linecolor shp.Cells("LineColor").Formula = 1 could work.
- ...

You must find a way to identify your shapes, and with that we can't help without knowing/seeing your shapes. Maybe you coul upload an example.