Consulting

Results 1 to 7 of 7

Thread: Change Background Color of a Shape if Certain Criteria are Met

  1. #1

    Change Background Color of a Shape if Certain Criteria are Met

    I have a pre-made powerpoint slide that includes two shapes. The header of the slide (text at the top) is selected via a GUI created with VBA. What I would like is to change the background color of just one of the shapes to a certain color depending on which header has been selected. Is this at all possible?

    Here is the code I have that allows the user to select the header:

    Private Sub Combo7_DropButtonClick()
    
    
        If Combo7.ListCount = 0 Then
            With Combo7
                .AddItem "Generic"
                .AddItem "Advisory"
                .AddItem "Watch"
                .AddItem "Warning"
            End With
        End If
    
    
    End Sub
    
    Private Sub Headlines()
    
    
        'This generates the headline of the slide.
    
    
        If Combo7 = "Generic" Then
        ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
         FileName:="K:\Story\Headline Images\Generic.png", _
         LinkToFile:=msoFalse, _
         SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
         Width:=720, Height:=91).Select
         
        ElseIf Combo7 = "Advisory" Then
        ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
         FileName:="K:\Story\Headline Images\Advisory.png", _
         LinkToFile:=msoFalse, _
         SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
         Width:=720, Height:=91).Select
         
        ElseIf Combo7 = "Watch" Then
        ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
         FileName:="K:\Story\Headline Images\Watch.png", _
         LinkToFile:=msoFalse, _
         SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
         Width:=720, Height:=91).Select
         
        ElseIf Combo7 = "Warning" Then
        ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
         FileName:="K:\Story\Headline Images\Warning.png", _
         LinkToFile:=msoFalse, _
         SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
         Width:=720, Height:=91).Select
         
        End If
    End Sub
    So what I need is something along the lines of:

    If Combo7 = "Generic" Then
    
    'Here would be the code to change the background color of the shape that is already on the slide, if possible.
    Any help is greatly appreciated! Thanks!

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    You need to say how to identify the shape to be changed.

    Name?
    Position?

    Also you can probably remove the .Select (x4) it is almost certainly doing nothing at all except slowing the code.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Hi, John. I would love to be able to name the shapes that are default in the slide, but I am not sure how to do that? If the method I proposed it too difficult, then I can always just insert a new shape using vba. In this case, I would just need to figure out how to customize the shape (i.e. change the background color/transparency and round the corners).

    Also, if I remove the .Select, the code breaks and it says I have a syntax error.
    Last edited by hunter21188; 11-22-2015 at 02:22 AM.

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    If you have 2007 or later you can (re)name shapes in the selection pane

    HOME Tab > Select (on right) Selection Pane.
    In 2003 and earlier you must use code

    Sub nameme()
    ActiveWindow.Selection.ShapeRange(1).Name="Whatever"
    End Sub
    The code is breaking because of the brackets BTW.

    Options

    1. Remove the brackets
    2. Use either of theses methods

             Call ActiveWindow.Selection.SlideRange.Shapes.AddPicture( FileName:="K:\Story\Headline Images\Generic.png", _
            LinkToFile:=msoFalse, _
            SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
            Width:=720, Height:=91)
            
          Dim NewShp As Shape
           Set newshape = ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
     FileName:="K:\Story\Headline Images\Generic.png", _
            LinkToFile:=msoFalse, _
            SaveWithDocument:=msoTrue, Left:=0, Top:=0, _
            Width:=720, Height:=91)
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    I have the 2010 version. That is great to know! Thanks! I think I may be able to figure out my initial problem now that I know that. I'll give it a try.

    Thanks for the info on the .Select! I went ahead and made those changes.

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    A good rule is always try to avoid select in PPT code. Sometimes you have to but not often.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  7. #7
    Good to know. Thanks.

    Just wanted to answer my original question. Once you told me I could name my shapes, it was simple. The following code accomplishes the task:

    Private Sub CommandButton1_Click()
    
        If Combo7 = "Generic" Then
        ActiveWindow.Selection.SlideRange.Shapes("TimingShape").Fill.ForeColor.RGB = RGB(255, 102, 204)
        End If
    
    
    End Sub
    Thanks again for all your help, John!
    Last edited by hunter21188; 11-22-2015 at 06:59 AM.

Tags for this Thread

Posting Permissions

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