PDA

View Full Version : Help with changing shape fill to custom colors



G00dbyegirl
01-27-2013, 09:20 AM
Hello

I'm hoping someone may be able to help me with what's probably an easy thing for a more experienced coder. I want to change the color (not text) of any selected shape (selected by the user) on PowerPoint 2010 to a custom color.

Here's my code so far, it's just returning the error (so I got that right!).

Many thanks

Sub changecol()
Dim oshp As Shape
On Error GoTo errhandler
oshp.Fill.ForeColor.RGB = RGB(215, 31, 133)
Exit Sub
errhandler:
MsgBox "Opps!"
End Sub

John Wilson
01-27-2013, 12:06 PM
Hi

You have declared an object variable (oshp) BUT you haven't set it to the first selected shape.

Sub changecol()
Dim oshp As Shape
On Error GoTo errhandler
set oshp=ActiveWindow.Selection.ShapeRange(1)
oshp.Fill.ForeColor.RGB = RGB(215, 31, 133)
Exit Sub
errhandler:
MsgBox "Opps!"
End Sub

Make sure you have a selected shape.

G00dbyegirl
01-27-2013, 01:32 PM
Hey John

Thanks for helping me. Here's my clumsy code (worked out before you posted a clean version) which I can now compare with yours to get some more learning in! My worked, but yours is cleaner.

Thanks again.

Sub changecol()
Dim oshp As Shape
ActiveWindow.Selection.ShapeRange.Select
With ActiveWindow.Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(215, 31, 133)
.Fill.Solid
End With
On Error GoTo errhandler
Exit Sub
errhandler:
MsgBox "Opps!"

End Sub

John Wilson
01-27-2013, 02:01 PM
Here's some tips then:

If at all possible (it usually is) AVOID using Select. In any case the ActiveWindow.Selection is already selected by definition.

You will not usually need to set the fill to solid and visible.

Your code will work if you delete the line
ActiveWindow.Selection.ShapeRange.Select

Your code will change all selected shapes mine just the first one.
On Error GoTo errhandler needs to be near the start - BEFORE the error might happen

MsgBox "oops-" & err.Description will tell you more.

G00dbyegirl
01-30-2013, 12:34 AM
Thanks John, I'm very grateful for the free advice and help. I started looking at VBA code just this past weekend to do some very specific color coding work to make a repetitive task less time consuming. I'm happy that I've got done what I wanted in Outlook, Word and PowerPoint, but now I want to learn how to do it right!

Thanks again for your time.

Johncoltrane
02-07-2013, 10:14 PM
John has given informative information. I will apply this code. Thanks John.

mckeown55
04-08-2013, 06:51 AM
this was very useful for me also - thanks