+ Reply to Thread
Results 1 to 7 of 7

checking if shapes exist and grouping them

Hybrid View

  1. #1
    Registered User
    Join Date
    06-16-2008
    Posts
    32

    checking if shapes exist and grouping them

    I've a small problem.

    I'm creating a powerpoint presentation based on a data in excel.

    The presentation consists of dozens of rounded rectangles with text in them, and depending on data, they may get a star, circle or other kind of things on top of them (small pictures).

    I would somehow be able to group these shapes...

    So far I'm creating the shapes(4 different shapes for now):

    the 'main' rounded rectangle is named 'boxframe' & indexnr

    star is named star & indernr (the indexnr is always same as in the box, so if there's a box with index 12, and it has data in it which qualifies a star on it, the star will be named star12.

    and few more objects, similarly named.

    I've counted how many boxforms there is on the sheet and made a loop through all of them, but then there's the problem. I've no idea how to check if the star @ that indexnr exists... if I knew I could just:
    for i = 1 to boxframecount
    if existsshape.name("star"&i) then
    groupedshapes = groupedshapes+1
    checking rest of the shapes.
    end if
    Dim shapesarray(1 to groupedshapes) as variant
    Shapes.Range(shapesarray).Group
    Note that the code in the tags is not real, it would give gazillion errors, but it's approx how i'm planning. Just, I've no idea how to check if a shape exists.

    Thanks.

    ps. this is not strictly an excel problem, but vba :P

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    You need to write a function to test for shape.

    Public Function ShapeExists(OnSheet As Object, Name As String) As Boolean
            
        On Error GoTo ErrShapeExists
        If Not OnSheet.Shapes(Name) Is Nothing Then
            ShapeExists = True
        End If
    ErrShapeExists:
        Exit Function
        
    End Function
    
    Sub Test()
    
        Dim lngIndex As Long
        Dim strName As String
        
        For lngIndex = 1 To 3
            strName = "Rectangle " & lngIndex
            If Not ShapeExists(ActiveSheet, strName) Then
                MsgBox "Does not exist " & strName
            End If
        Next
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-16-2008
    Posts
    32
    Couldn't get it work :I

    here's how I tried it now:

        On Error GoTo ERR_DOESNTEXIST
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("star" & i).Name
        grouped(2) = "star" & i
        groupedamount = groupedamount + 1
    ERR_DOESNTEXIST:
    
        On Error GoTo ERR_DOESNTEXIST2
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("top" & i).Name
        grouped(3) = "top" & i
        groupedamount = groupedamount + 1
    ERR_DOESNTEXIST2:
        
        On Error GoTo ERR_DOESNTEXIST3
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("picture" & i).Name
        grouped(4) = "picture" & i
        groupedamount = groupedamount + 1
    ERR_DOESNTEXIST3:
    so... basically it should just jump over the adding to array and such if an error appears... It just doesn't work :P

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    This works for me.

    The code will group those rectangle and stars that match.

    Sub X()
    ' group Box and Star shapes with same index
    
        Dim lngIndex As Long
        Dim shpBox As Shape
        Dim shpStar As Shape
        Dim strGroupNames() As String
        Dim lngGroupCount As Long
        
        
        For lngIndex = 1 To 4
            If ShapeExists(ActivePresentation.Slides(1), "Box" & lngIndex) Then
                lngGroupCount = 1
                ReDim strGroupNames(1 To lngGroupCount)
                strGroupNames(lngGroupCount) = "Box" & lngIndex
                
                If ShapeExists(ActivePresentation.Slides(1), "Star" & lngIndex) Then
                
                    lngGroupCount = lngGroupCount + 1
                    ReDim Preserve strGroupNames(1 To lngGroupCount)
                    strGroupNames(lngGroupCount) = "Star" & lngIndex
                
                    With ActivePresentation.Slides(1).Shapes.Range(strGroupNames).Group
                        .Name = "Group" & lngIndex
                    End With
                    
                End If
            End If
        Next
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-16-2008
    Posts
    32
    Nevermind, solved :P

    here's the code

        On Error GoTo ERR_DOESNTEXIST
        exists = True
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("star" & i).Name
        If exists = True Then
            grouped(2) = "star" & i
            groupedamount = groupedamount + 1
            exists = False
        End If
    ERR_DOESNTEXIST:
        If exists = True Then
            exists = False
            Resume Next
        End If
        
        On Error GoTo ERR_DOESNTEXIST2
        exists = True
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("top" & i).Name
        If exists = True Then
        grouped(3) = "top" & i
        groupedamount = groupedamount + 1
        exists = False
        End If
    ERR_DOESNTEXIST2:
        If exists = True Then
            exists = False
            Resume Next
        End If
        On Error GoTo ERR_DOESNTEXIST3
            exists = True
        temp = opa.ActivePresentation.Slides(activeslide).Shapes("picture" & i).Name
        If exists = True Then
        grouped(4) = "picture" & i
        groupedamount = groupedamount + 1
        exists = False
        End If
    ERR_DOESNTEXIST3:
        If exists = True Then
            exists = False
            Resume Next
        End If

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    If all items are required in order to group them then you can condense you code to

        On Error GoTo ERR_DOESNTEXIST
        grouped(2) = opa.ActivePresentation.Slides(activeslide).Shapes("star" & i).Name
        grouped(3) = opa.ActivePresentation.Slides(activeslide).Shapes("top" & i).Name
        grouped(4) = opa.ActivePresentation.Slides(activeslide).Shapes("picture" & i).Name
        ' group items
    
    ' code to group items
        
    ERR_DOESNTEXIST:
        Exit Sub

  7. #7
    Registered User
    Join Date
    06-16-2008
    Posts
    32
    Quote Originally Posted by Andy Pope View Post
    If all items are required in order to group them then you can condense you code to

        On Error GoTo ERR_DOESNTEXIST
        grouped(2) = opa.ActivePresentation.Slides(activeslide).Shapes("star" & i).Name
        grouped(3) = opa.ActivePresentation.Slides(activeslide).Shapes("top" & i).Name
        grouped(4) = opa.ActivePresentation.Slides(activeslide).Shapes("picture" & i).Name
        ' group items
    
    ' code to group items
        
    ERR_DOESNTEXIST:
        Exit Sub
    Thank you, though not all are required. Just need to have 2 elements for grouping.

    The meaning of this grouping is that if user has to modify the presentation by hand a bit, it's easier to move the groups of objects which belong together, than all objects separately.

    So if there's no extra objects at all, I skip the grouping all together ( There is always the 'boxframe' That's where all are 'attached')

    I'm sure there's million better ways to code this than mine and I'm always writing quite bloated code. But no-one else is reading this but me

    Thanks anyway.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1