Fill a property on shapes by vba code

Hey guys, Im on a new project in my company and I need to make some things in Visio 2016.

I want to write a vba code that create a property in shape data and then fill it with the container's shape name. Is there possible?

Ive already wrote a code that creat a property "Setor" in all shapes on the page, but I need to fill this property with the name of a container the shapes are into.

The code that Im using is this:

Private Sub Setorizacao()

Dim visShape As Visio.shape
Dim visPage  As Visio.Page



Set visPage = ActivePage

For Each visShape In ActivePage.Shapes
   
   If visShape.CellExists("LockWidth", False) Then
   
   Call InitSetor(visShape)
   
   End If
   
   Next
   
   
           
End Sub

Sub InitSetor(ByRef shape As Visio.shape)

' Create Prop.Setor if they don't exist; set both fields to null

    If Not shape.CellExistsU("Prop.Setor", False) Then
        shape.AddNamedRow visSectionProp, "Setor", visTagDefault
    End If
    shape.CellsU("Prop.Setor").FormulaU = ""
    shape.CellsU("Prop.Setor.Label").FormulaU = Chr(34) & "Setor" & Chr(34)
   
   
End Sub



Thank you in advance.
Lucas TarettiAsked:
Who is Participating?
 
Scott HelmersConnect With a Mentor Visio Consultant, Trainer, Author, and DeveloperCommented:
A question and a suggestion:

Question: is there a reason why you're using VBA to add the shape data field and formula? Usually a better alternative is to build a master or two that already contain the fields you need, including embedded formulas, if necessary. Then put the masters into a custom stencil and the shapes do the right thing automatically when you drag them onto a page.

Suggestion: You don't actually need VBA code for a shape to know what container it's in; there are ShapeSheet formula that do this work for you -- but you also need to know a bit about how containers are constructed. Here's the short version:

The header in a container is a subshape of the container shape and the container can 'read' the text from the header by getting a reference to the subshape's text. In the attached example, the container is Sheet.3 and its header is Sheet.6. Consequently, I created a User field like this:
container
Then, in the contained shape, the Shape Data field Value cell contains this formula:contained shape
BTW, IFERROR() is just a convenience. By using this function, if the shape is not in a container the Shape Data value is "" but if it is in a container, the Shape Data value is read from the User cell in the container. The real magic here is the CONTAINERSHEETREF() function that returns a reference to the container.

Want a great model for all of this? Look at Visio swimlane diagrams. When you drag any flowchart shape into a lane, the Shape Data field called Function automatically displays the text from the header of the swimlane it resides in.

Final comment: if you have to use VBA instead of creating a custom stencil with prebuilt shapes, you can still use the above info. You'll just need to go through a bit of extra work to get the sheet number of the container header and then build the two required formulas, one for the container's User cell and the other for the Shape Data field. Let me know if you need help with that.
Containers-with-shapes-that-display.vsdx
0
 
NorieVBA ExpertCommented:
Doesn't your code already set the value of the property to 'Setor'?

If it does and you want to change 'Setor' to the name of the shape's container perhaps you could try something like this.
shape.CellsU("Prop.Setor.Label").FormulaU = Chr(34) & shape.Parent.Name & Chr(34)

Open in new window

0
 
Lucas TarettiAuthor Commented:
Thanks for ur response.

My code just add a prop row named "Setor" on shape data. The property is created with no value, it stays blank.

I add this line in my code but returns me an error "object required".
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
NorieVBA ExpertCommented:
Lucas

I'm a bit rusty with Visio VBA but assumed the Parent property of the shape object would be its 'container'.

Do you still get the same error with this?
MsgBox shape.Parent.Name

Open in new window

0
 
Lucas TarettiAuthor Commented:
Same error Norie.

I think you didnt understand my problem, I'll try to explain it better.

My code insert a prop "Setor" in all shapes on the page, but all those shapes are in two different containers "SETOR 1" and "SETOR 2", so I want to fill the prop "Setor" with the container's name.
Shapes in container "SETOR 1" fill the prop "Setor" with "SETOR 1"
Shapes in container "SETOR 2" fill the prop "Setor" with "SETOR 2"

I changed the container's shape name in developer tab to become more easy to refer their names on the code.
0
 
NorieVBA ExpertCommented:
Lucas

I think I understand the problem but perhaps misinterpreted the code on first look and/or misunderstood when you want to do this.

Is this code setting the label for the property you've created?
    shape.CellsU("Prop.Setor.Label").FormulaU = Chr(34) & "Setor" & Chr(34)

Open in new window

0
 
Rgonzo1971Commented:
Hi,

pls try (if the container is a shape)
shape.CellsU("Prop.Setor.Label").FormulaU = Chr(34) & shape.ContainingShape.Name & Chr(34)

Open in new window

Regards
0
 
Lucas TarettiAuthor Commented:
This part of the code sets the name of the prop on shape data.

If I change the "Setor" to "Shape" for example, the prop will be named Shape instead of Setor.

The first part of the code, search the "LockWidth" field in all shapes and if this field exists, then the prop will be created.

The second part is the "machine" that really creats the prop on shape data, I call the second part in the first one.
0
 
Rgonzo1971Commented:
and
shape.CellsU("Prop.Setor").FormulaU = Chr(34) & shape.ContainingShape.Name & Chr(34)

Open in new window

0
 
Lucas TarettiAuthor Commented:
Rgonzo, thanks for ur response.

I changed the code and the prop fills with "ThePage". I need the prop fills with the container's name
0
 
Rgonzo1971Commented:
then try
shape.CellsU("Prop.Setor").FormulaU = Chr(34) & shape.ContainingPage.Name & Chr(34)

Open in new window

or
shape.CellsU("Prop.Setor").FormulaU = Chr(34) & shape.ContainingPage.NameU & Chr(34)

Open in new window

0
 
Lucas TarettiAuthor Commented:
It still isnt working.

If I put shape.ContaininPage.name, returns "ThePage" or "Page-1".
0
 
Lucas TarettiAuthor Commented:
Hi Scott,

Thanks for your response.
The shapesheet formula solution fits my need, Im going to use it!

Thanks for the help!
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.