Referencing first shape on a page after changing format

Started by WCTICK, February 24, 2023, 03:35:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WCTICK

I have a macro that will import data from an excel spreadsheet to create an Organization Chart.

When the import is complete, the first shape on each page has an ID of one.

However, I want to change the shape type that is created by the import for all shapes and change the font.  After that is done, the top shapes on each page have different ID numbers.

I have a script to select the top shape using the ItemFromID(1) method as shown below and get some data from it.

Set vShp = pg.Shapes.ItemFromID(1)
    PgName = vShp.CellsU("Prop.Department").ResultStr(" ")

How do you select the top shape if the ID is no longer 1?


wapperdude

Basically, you need to !lol thru all shapes on the page.  Only the the shape with the needed properties would be chosen. 

The trick is identifying the right shape.  Here are possibilities: 
  1). Does the top shape have a unique master relative to all the other shapes on the page.  If so, search for shape with that master.
  2). Is the shape top most on the page, then you could use pinY value.  If so, search shapes for largest PinY.
  3). Is it the only shape that gets update?  Then you could use largest ID.
  4). During the update process, you could capture the ID of the replacement shape for shape 1.
  5). Is it always the 1st shape to get replaced.  If so, you might be able to "cheat"...
           > use shapes.count to find # of shapes on a page.
           > assuming the initial placement resulted a non-gapped placement then new ID ought to be shape.count + 1, all things being normal.

Hope this helps / sparks some ideas.
Visio 2019 Pro

Croc

You can try one more way.
Organization Chart is known to replace shapes, but not connectors, when the format changes. This way you can remember the connector ID connected to the top shape in the ShapeData of the page.
After changing the format, find the shape to which this connector is connected.
Here are two procedures.
Sub SaveFirstConnectorID()
    Set shp = pg.Shapes.ItemFromID(1)   'ActiveWindow.Selection(1)
    ActivePage.PageSheet.Cells("Prop.cn1id") = shp.FromConnects(1).FromSheet.ID 'connector ID
End Sub

Sub GetTopNameID()
    cnID = ActivePage.PageSheet.Cells("Prop.cn1id") 'connector ID
    Debug.Print ActivePage.Shapes.ItemFromID(cnID).Connects(1).ToSheet.NameID   'Top shape nameID
End Sub

Yacine

... and since we are collecting possibilities, you could as well insert an extra step between import and formatting, where you mark the top most shapes by means of an according shapesheet field.
eg:
Set vShp = pg.Shapes.ItemFromID(1)
vShp.AddNamedRow(visSectionProp, "top", visTagDefault)
It doesn't even need a value.

You retrieve the top shapes by
If vShp.CellExists("prop.top", False) then ...


Benefit of this method is that you can even change the layout to a horizontal one. The position on the page doesn't matter anymore.
Yacine