PDA

View Full Version : [SOLVED:] Change Background Color of a Shape if Certain Criteria are Met



hunter21188
11-21-2015, 07:40 AM
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!

John Wilson
11-21-2015, 01:04 PM
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.

hunter21188
11-22-2015, 12:51 AM
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.

John Wilson
11-22-2015, 04:07 AM
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)

hunter21188
11-22-2015, 05:27 AM
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.

John Wilson
11-22-2015, 05:38 AM
A good rule is always try to avoid select in PPT code. Sometimes you have to but not often.

hunter21188
11-22-2015, 06:36 AM
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!