Name a BuildFreeform shape Question

julhs

Active Member
Joined
Dec 3, 2018
Messages
407
Office Version
  1. 2010
Platform
  1. Windows
The code below constructs a Freeform shape, how can I give the shape a meaning full/given name so that it is not simply given “Freeform ??? what ever” when it gets converted to a shape.

VBA Code:
Sub DrawFreeformME()
Dim ws As Worksheet, fb As FreeFormBuilder
Set ws = ActiveSheet
' Create freeform
Set fb = ws.Shapes.BuildFreeform(msoEditingAuto, 380, 230)
' Add segments.
fb.AddNodes msoSegmentCurve, msoEditingCorner, _
380, 230, 400, 250, 450, 300
fb.AddNodes msoSegmentCurve, msoEditingAuto, 480, 200
fb.AddNodes msoSegmentLine, msoEditingAuto, 480, 400
fb.AddNodes msoSegmentLine, msoEditingAuto, 380, 230
' Render drawing.
fb.ConvertToShape
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Perhaps
VBA Code:
Sub DrawFreeformME()
Dim ws As Worksheet, fb As FreeformBuilder
Dim shp As Shape

Set ws = ActiveSheet
' Create freeform
Set fb = ws.Shapes.BuildFreeform(msoEditingAuto, 380, 230)
' Add segments.
fb.AddNodes msoSegmentCurve, msoEditingCorner, _
380, 230, 400, 250, 450, 300
fb.AddNodes msoSegmentCurve, msoEditingAuto, 480, 200
fb.AddNodes msoSegmentLine, msoEditingAuto, 480, 400
fb.AddNodes msoSegmentLine, msoEditingAuto, 380, 230
' Render drawing.
Set shp = fb.ConvertToShape
shp.Name = "Blue Shape3"

End Sub
 
Upvote 0
Solution
HI
After fb.ConvertToShape add this code
VBA Code:
With ActiveSheet
  .Shapes(.Shapes.Count).Name = "YourName"
End With
Hi,
Mario
 
Upvote 0
Thank you Micron, you’re a life saver!
I’d been struggling with this for ages and it was doing my nut in!!
I can now set about positioning the Freeform shape on the sheet in the same sort of way that you do when you use a predefined mso shape, ie
Excel Formula:
AddShape(msoShape?????, 250, 350, 100, 200)
‘250, 350 being its position & 100, 200 being its size
 
Upvote 0
The other suggestion would likely work to address your posted concern, but the advantage of creating an object variable for this would be that you could do other things with or to it in that code without having to continually reference the Shapes collection.
Glad I could help. If you have a solution, perhaps mark this as solved so that others looking to help can bypass this one.
 
Upvote 1
the advantage of creating an object variable for this would be that you could do other things with or to it in that code without having to continually reference the Shapes collection.
Did think using object variable was going to be easy to reference further down the line, but defining the Freeform shape as an object was what I was struggling with!!
 
Upvote 0
HI

@Micron
I have to agree that your suggestion is more streamlined than mine. Compliments.
I disagree, however, with the point of continually referring to the Shapes Collection; at that point you refer to the image created by its name, the one you gave it.
Do you agree?

HI,
Mario
 
Upvote 0
If I understand your suggestion, then IMO this or anything similar is still a reference to the shapes collection
ActiveSheet.Shapes(Shapes.Count).somePropertyHere = somePropertyValueHere

AFAIK, (Shapes.Count) is the reference to the shape with the highest index number; index is a property of the Shapes collection. I say AFAIK because my knowledge of Access vba is far better than Excel vba but I do find that many of the basic principles apply to both. So if you have an example of how to refer to it by name only (not by index (which can be a number or its name) and not by using an object variable), can you share it?
Thanks.
 
Upvote 0
HI
As you can see from the last line of code, I select the created shape by its name.
Here's what I mean.

VBA Code:
Sub Macro3()
Dim ws As Worksheet, fb As FreeformBuilder
Set ws = ActiveSheet
' Create freeform
Set fb = ws.Shapes.BuildFreeform(msoEditingAuto, 380, 230)
' Add segments.
fb.AddNodes msoSegmentCurve, msoEditingCorner, _
380, 230, 400, 250, 450, 300
fb.AddNodes msoSegmentCurve, msoEditingAuto, 480, 200
fb.AddNodes msoSegmentLine, msoEditingAuto, 480, 400
fb.AddNodes msoSegmentLine, msoEditingAuto, 380, 230
' Render drawing.
fb.ConvertToShape
'change name
With ActiveSheet
  .Shapes(.Shapes.Count).Name = "YourName"
End With

ActiveSheet.Shapes("YourName").Select   ' <---xxxxxxxxxxx '
End Sub

HI,
Mario
 
Upvote 0
OK, Shapes("YourName") is still a reference to the shapes collection that belongs to the active sheet by using the shape name instead of a number for the index property value. Not that it's wrong, just that I consider creating an object variable and storing the shape in it is just marginally more efficient if repeated references to it will be used.

So shp.name, or shp.Select or whatever, eliminates referring to the active sheet and drilling down to the shapes collection because the actual shape is in the variable. Same as if you used a With block:
VBA Code:
With shp
    .Select
    .etc
End With
vs
VBA Code:
With ActiveSheet.Shapes("myShape")
    .Select
    .etc
End With
For subsequent references to a shape, I don't see how you can escape starting at the sheet level, referring to its shape collection, then to a shape via the index property (by name or number) without storing the shape in a variable. Granted, putting the shape in an object variable requires that chain of references, but it is only done once.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,103
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top