Consulting

Results 1 to 10 of 10

Thread: Change Background colour of slides using VBA

  1. #1
    VBAX Regular
    Joined
    Nov 2009
    Posts
    7
    Location

    Change Background colour of slides using VBA

    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.

    Could anyone help me?
    Thank you


    Jess

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    You need to use ForeColor not BackColor
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Jess -- I assume that you just left off the RGB( ) in your post in your colors

    [VBA]
    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
    [/VBA]

    John -- 3 questions

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

    Paul

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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!
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Regular
    Joined
    Nov 2009
    Posts
    7
    Location
    Hi

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

    Thanks


    Jess

  6. #6
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    No difference One still needs to click Ctrl+Z two times.

  9. #9
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I swear it worked at first! Now two clicks (three in one case in 2016)

    Just buggy I think
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  10. #10
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    You are right, it's even worse - needs three clicks now in 2010, too. Weird.

Posting Permissions

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