Changing Shape Names through VBA Macro

Started by Scott10284, July 07, 2017, 02:20:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Scott10284

Hello,

Can I write a macro that will scan every page of my drawing set, identify shapes that have a certain shape data field in them, and change the SHAPE NAME from its generic name (Sheet.1, Sheet.24, Sheet.576, etc.) to a desired name.

Specifically, I want to scan all the pages in my drawing set, look for shapes that have the shape data field Prop.PageNumber and make the shape name PAGE NUMBER.

Is this possible???

wapperdude

#1
You can use the following macro to change shape name.  Note, Visio makes a distinction between Name and NameU, so both are changed.


Sub Macro1()
'Macro renames all shapes = sheet.1
    Dim vShp As Shape
    Dim vPg As Page
   
   For Each vPg In ActiveDocument.Pages
        Set vShp = vPg.Shapes.ItemFromID(1)
        vShp.Name = "Desired Name"
        vShp.NameU = "Desired Name"
    Next
   
End Sub


Rather than setting vShp explicitly, add a 2nd, nested For...Next loop for all shapes on a page.  In this new loop, check shape if Property exists, then rename shape.  May need error handling for case when Property doesn't exist.


Sub Macro1()
'Macro renames all shapes that have the cell = Prop.Name
    Dim vShp As Shape
    Dim vPg As Page
   
    For Each vPg In ActiveDocument.Pages
        For Each vShp In vPg.Shapes
            On Error Resume Next
            If vShp.CellExists("Prop.Name", 1) Then
                vShp.Name = "Desired Name"
                vShp.NameU = "Desired Name"
            End If
        Next
    Next
   
End Sub
Wapperdude
Visio 2019 Pro

Scott10284

Wapperdude, it works perfectly!!

As always, thanks for your help!!!