VBA Code Does Not Find User Definded Shapes under My Shapes

Started by DinoCobolMan, September 06, 2017, 01:59:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DinoCobolMan

I can change the line color, text color, fill color or delete all the Database shapes on a page with both
VBA programs listed below.  Database is found in Visio's Basic Flow Chart Shapes.

However, when I use my own shapes that are stored in MY SHAPES neither of the programs will work.
My User Defined Shapes are not grouped together so this isn't this issue.   Are additional statements needed
in my code to point to my User Defined Shapes stored under MY SHAPES

Public Sub MyColor()
Dim shp As Visio.Shape
Dim i As Integer

ActiveWindow.DeselectAll
For i = ActivePage.Shapes.Count To 1 Step -1  '/ Count backwards
    Set shp = ActivePage.Shapes.ItemU(i)
    If Not (shp.Master Is Nothing) Then
        If shp.Master.Name = "Database" Then
     '        shp.CellsU("FillForegnd").FormulaForceU = "RGB(255,0,0)"
     '        shp.CellsU("FillForegnd").FormulaForceU = "2"
     '        shp.Delete
     '        shp.Cells("Char.Color").FormulaForce = "1"
     '        shp.Cells("LineColor").FormulaForce = "1"
              shp.Cells("LineColor").Formula = "rgb(255,6,20)"
        End If
    End If
Next i
Set shp = Nothing
End Sub


Public Sub Delete2()
Dim pag As Page
Dim shp As Shape
Dim i As Integer

' Set reference to correct page
Set pag = Application.ActivePage

For i = pag.Shapes.Count To 1 Step -1   '/ Count backwards
    Set shp = pag.Shapes.ItemU(i)
       If Not (shp.Master Is Nothing) Then
          If shp.Master.NameU = "Database" Then
     '        shp.CellsU("FillForegnd").FormulaForceU = "RGB(255,0,0)"
     '        shp.CellsU("FillForegnd").FormulaForceU = "2"
     '         shp.Cells("Char.Color").FormulaForce = "1"
     '         shp.Cells("LineColor").FormulaForce = "1"
              shp.Cells("LineColor").Formula = "rgb(255,6,20)"
     '        shp.Delete
          End If
    End If
Next i
Set shp = Nothing
End Sub

wapperdude

Try a simple test...
Tile the drawing window and the vba window so you can see both.
In the drawing window select one of your MyShape, "Database" shapes.  Now, in vba modify the program to disable the looping, but keep the interior loop statements.  This way the program will only act on the selected shape.  Add some debug.print statements, e.g., debug.print shp.name, debug.print shp.master.name.  Results will be (normally) at bottom of vba window.  Do they match what you're expecting?

Note:  oh can step thru the code, one line at a time using <f8> key.  Note also, once a line has executed, you can hover mouse over a parameter to see what value was assigned to it.

Wapperdude
Visio 2019 Pro

Yacine

public sub listShapesMasterNames
dim shp as shape
for each shp in activepage.shapes
  debug.print shp.master.name; shp.master.nameU
next shp
end sub

This should show you why your routine is not working.
Yacine

DinoCobolMan

Yancine,
Yes, your code worked and at that point I understood what Wapperdude was explaining.   I tested  your code on a shape named Flat2, but the name that was displayed is Flat2.50
I changed my code to Flat2.50  and my code worked and found the Flat2.50.   Is there a way to reference a Flat2.* like a wild card to catch a Flat2.1 to Flat2.999999? 

According to Visio Super Utilities the Flat2 is named as follows:
Report for stencil: VBA INTERVALS.vss
  Master name  = Flat2
  Master nameU = Flat2
  Master baseID = {40C79B16-E6CA-48D0-95CB-937BAD2B3263}
  Master prompt =
  Shape keywords = ""
  Shape copyright = "Copyright 1999 Visio Corporation.  All rights reserved."
  Icon size = Normal(32x32)



DinoCobolMan

I found another page named "Shapes & Templates / Re: Shape name changes when dropped on page" that seemed to be talking about my same issue. 
Within my Stencil, I renamed Flat2 to User.Flat2 and my code worked and found User.Flat2.   Is this a solution that should always work? 
I have hundreds up custom stencils shapes with assigned names.  So, by just my adding the User. prefix, all my shape name reference issues should be solved?

Public Sub Delete2()
Dim pag As Page
Dim shp As Shape
Dim i As Integer

' Set reference to correct page
Set pag = Application.ActivePage

For i = pag.Shapes.Count To 1 Step -1   '/ Count backwards
    Set shp = pag.Shapes.ItemU(i)
       If Not (shp.Master Is Nothing) Then
          If shp.Master.NameU = "User.Flat2" Then
                   shp.Delete
          End If
    End If
Next i

Set shp = Nothing
End Sub









wapperdude

This is not correct.  It is a wrong interpretation of what was presented.  User.name is a shapesheet entry created to hold the desired "name identifier" for the shape.  It is entry in the User-Defined section of the shapesheet.  It is not the literal name of the shape.

The whole point of the reference was to avoid using named shapes.  Instead, put the name inside the shape, i.e., in the shapesheet.  That way it won't change when you drag and drop.

The answer to both of your questions is No.

Wapperdude
Visio 2019 Pro

DinoCobolMan

Wapperdude,

Once again, thank you for responding to one of my posting.

I could not find a way to assign a custom or Unique ID within the ShapeSheet.

If I name a field POLICY_NUMBER and store in a file, table, database, indexed file, XML file or HTML, it will have the same meaning and the same name of POLICY_NUMBER.   

I read the Visio Guy article What's My Shape's ID and tried to rename my a stencils shape.

I dragged my shape Flat2 to a Visio page and it was named

    SCALES WORK AREA.vsdm:Page-2:Flat2.104<Shape>

I renamed Flat2.104 to Flat2 with Developer – Shape Name.
   SCALES WORK AREA.vsdm:Page-2:Flat2<Shape>

I dragged my Flat2 shape to a my Stencil, save the stencil, closed Visio, re-opened Visio, dragged Flat2 to my page, and now my name of my is
   
    SCALES WORK AREA.vsdm:Page-2:Flat2.131<Shape>

I am just going to finish my charts manually.   

Thank you for your time.