Log in

View Full Version : Change Background colour of slides using VBA



jess
03-15-2010, 03:40 AM
Hi
I have a line of code which is attached to VBA command button in a slide presentation in Powerpoint 2007. There are two buttons which should either change the background colour of the whole presentation to either white or yellow.

When the button is selected (In presentation slide show mode) nothing happens and no errors appear. It seems to ignore the line of code for those button.
The line of code for the white button is:
ActivePresentation.SlideMaster.Background.Fill.BackColor.RGB = 255255255
The line of code for the yellow button is:
ActivePresentation.SlideMaster.Background.Fill.BackColor.RGB = 2552550

I have tried everything, savng the presentation as a presentation show file and breaking down the line of code but i can not find what the problem is. :banghead:

Could anyone help me?
Thank you


Jess

John Wilson
03-16-2010, 08:17 AM
You need to use ForeColor not BackColor

Paul_Hossler
03-16-2010, 06:26 PM
Jess -- I assume that you just left off the RGB( ) in your post in your colors


Option Explicit
Sub Yellow()
ActivePresentation.SlideMaster.Background.Fill.ForeColor.RGB = RGB(255, 255, 0)
End Sub

Sub White()
ActivePresentation.SlideMaster.Background.Fill.ForeColor.RGB = RGB(255, 255, 255)
End Sub


John -- 3 questions

1. Why is that ForeColor?
2. What would be the BackColor?
3. WHY oh WHY is Powerpoint so confusing?

Paul

John Wilson
03-17-2010, 12:11 AM
It IS crazy!

Think of Forecolor as Color1 or Main Color (why didn't MS think of this) and BackColor as Color2 or secondarycolor.

You would use BackColor eg if you had a pattern fill with a red main color and a blue backcolor or a two color gradient color1 > color2.

Basically nearly all "normal" uses = Forecolor

You are correct about the missing RGB, I assumed they had worked out the value but didn't look closely enough eg Yellow is 65535. I strongly recommend using RGB(255,255,0) though!

jess
03-17-2010, 10:11 AM
Hi

Thank you soo much! It is working perfectly thank you.

Thanks


Jess

RandomGerman
09-20-2016, 02:17 AM
Hi there,

I found this wonderful little piece of code on the internet, written by Shyam Pillai, and it works fantastic ... but ... using Ctrl-Z one has to click twice. After the first click it sets the Background Color to Accent 1 Color of the document and I have no idea why. (Happens in PPT2010 and newer, have not tested in 2007). Someone able to help me?


Sub GreenSlide()
With ActiveWindow.Selection.SlideRange
.FollowMasterBackground = msoFalse
.Background.Fill.Solid
.Background.Fill.ForeColor.RGB = RGB(0, 255, 0)
End With
End Sub

John Wilson
09-20-2016, 03:05 AM
If the background is already solid I believe .BackGround.Fill.Solid applies the default Accent 1 so you need to undo two things

Try


Sub GreenSlide() With ActiveWindow.Selection.SlideRange
.FollowMasterBackground = msoFalse
If Not .Background.Fill.Type = msoFillSolid Then .Background.Fill.Solid
.Background.Fill.ForeColor.RGB = RGB(0, 255, 0)
End With
End Sub

RandomGerman
09-20-2016, 04:24 AM
No difference :( One still needs to click Ctrl+Z two times.

John Wilson
09-20-2016, 07:18 AM
I swear it worked at first! Now two clicks (three in one case in 2016)

Just buggy I think

RandomGerman
09-20-2016, 09:12 AM
You are right, it's even worse - needs three clicks now in 2010, too. Weird.