PDA

View Full Version : Merge two shape using vba



cswarup401
12-21-2021, 09:37 PM
Sir,


I am a newbie in PowerPoint vba.It may be super simple question for other but I can not configure it.
I want to merge two shape by subtract using vba.
I find a solution from searching web but how to rename this new generated shape/make a shape object.

I want to rename it "MergeShape"


Any solution will be appreciated.




Sub mergeShape()
Dim shp1 As Shape
Dim shp2 As Shape
Dim shp3 As Shape
Set shp1 = ActivePresentation.Slides(1).Shapes("Rectangle1")
Set shp2 = ActivePresentation.Slides(1).Shapes("Pentagon1")
Call ActiveWindow.Selection.SlideRange(1).Shapes.Range(Array(shp1.ZOrderPosition , shp2.ZOrderPosition)).MergeShapes(msoMergeSubtract, shp1)
End Sub

John Wilson
01-08-2022, 11:24 AM
MergeShapes returns VOID so iot is difficult to refer to the new shape. However in most cases it will have the zorder position one less than the first shape so this MIGHT work


Sub mergeShape()
Dim shp1 As Shape
Dim shp2 As Shape
Dim shp3 As Shape
Dim osld As Slide
Dim lngPos As Long
Set osld = ActivePresentation.Slides(1)
Set shp1 = osld.Shapes("Rectangle1")
lngPos = shp1.ZOrderPosition
Set shp2 = osld.Shapes("Pentagon1")
Call osld.Shapes.Range(Array(shp1.ZOrderPosition, shp2.ZOrderPosition)).MergeShapes(msoMergeSubtract, shp1)
osld.Shapes(lngPos - 1).Name = "merged"
End Sub