PDA

View Full Version : [SOLVED:] Reformatting a grouped shape and a similar combination that is not grouped (VBA)



juanbolas
02-04-2022, 07:55 PM
Hello everyone,
I've been trying to work this out but am stuck.
My main problem is that I can't select a shape after ungrouping - referencing the 2 o 3 ungrouped items.

I have a series of flow chart shapes that have a reference number on the top left of the main. shape. They number in the hundreds now. Because I was lazy, some of the shapes have the reference number and the main shape grouped together, others don't, Additionally some shapes have an additional grouping with a text shape to increase text area (the decision shape for example). The main shape and textbox are (or should be the same size).

Due to the holes I have in the code, I haven't been able to test what I've written so far.
Thanks in advance for your help!


Sub FormatFlowChartShape()

Dim oSlide As slide
Dim oShape As Shape
Dim oNewShape As Shape
Dim AreaTolerance As Integer
Dim ShapeNumberHeight As Integer
Dim ShapeNumberWidth As Integer

''AreaTolerance = 0.2 '' shape may be +/-20%

ShapeNumberHeight = 19.85
ShapeNumberWidth = 19.85
ShapeMainHeight = 48.76
ShapeMain Width = 119.65


Set oSlide = Application.ActiveWindow.View.slide
Set oShape = ActiveWindow.Selection.ShapeRange(1)

' store shape position of posibly grouped shape
With oShape
ShapeLeft = .Left
ShapeTop = .Top
ShapeWidth = .Width
ShapeHeight = .Height
End With
' ungroup shapes if necessary
If oShape.GroupItems.Count > 0 Then
If oShape.Type = msoGroup Then
oShape.Ungroup
' in some cases there are two grouping levels
' because a textbox is used for more text space
oShape.Ungroup
Debug.Print "Ungrouped"
End If

' select first shape
'which is the shapes step number in the flowchart
' it is usually located slightly offset to the
' left and top of the main flowchart shape
' having trouble selecting shape that was in a group
' i was thinking of find shape within the area of the ungrouped shape
' and selecting it. Does this make sense?

' select it -- HELP!!!
' if it doesn't exist, create a new shape
If Not ShapeExists Then
Set oNewShape = oSlide.Shapes.AddShape( _
Type:=msoRoundedRectangle, _
Left:=24, _
Top:=NewSlideTop, _
Width:=ShapeNumberWidth, _
Height:=ShapeNumberHeight)
End If

' format shape stuff goes here
' format

'Now main second shape

' select it -- HELP!!!

'reposition number Shape to main shape

oShape.Left = .Left - ShapeNumberLeft / 2
oShape.Top = .Top - ShapeNumberTop / 2
oShape.Width = ShapeMainWidth
oShape.Height = ShapeMainHeight

End With

' because a textbox is used for more text space

' format shape stuff goes here
' format

' if text box exists process it
' Size
' Format

' Set z-order
' Send geometric shape to back
' if text box Bring text box yo front
' Bring number to front

End Sub

John Wilson
02-05-2022, 03:32 AM
First I notice you have posted this question on at least one other site. It is always good to mention this simply because you may get an answer in one place leaving people working on your problem in other sites. In my expeperience posters rarely go back and say "Thanks I got an answer on this site." (also a good idea BTW)

The best way to do this is to loop through the groupitems before ungrouping and change the name of the groupitem shape to something unigue. Once ungrouped the name is retained and you can then select (setting Replace flag to False) or change shapes with that name.

juanbolas
02-05-2022, 05:37 AM
Thanks for the head's up John. I will make sure to cross-post and credit for the solution.

With regard to your proposed solution, do you mean that while looping I would have to identify the type of shape/size then name it?

Thanks in advance.

John Wilson
02-05-2022, 07:11 AM
I meant something like


If oShape.Type = msoGroup Then
For L = 1 To oShape.GroupItems.Count
oShape.GroupItems(L).Name = "Group_Item" & CStr(L)
Next
oShape.Ungroup

You could also just try


oShape.Ungroup.Select False



In most cases the group items will be selected

juanbolas
02-05-2022, 12:02 PM
Thanks a million!

juanbolas
02-05-2022, 02:38 PM
John, I hit the wall with a couple of shapes that have a subgroup. Any idea on how I can proceed? Loop through the sub group? If I ungroup I lose the selection.

The question probably should be: how do I ungroup a group with a nested group while keep cells selected?

Thanks in advance,