Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Nov 2005
    Location
    London, Gtr London, United Kingdom
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Powerpoint VBA If statement (UK/PPT/2000)

    Hi everyone.

    I'm sure this is extremely simple but i've gone crossed eyed over this and can't get it to do what i want.

    What i'm trying to do is have some code that changes a box's fill and outline depending on what it already is. ie, with this example below, If i have a box with an existing fill, and I run the macro, I wan't the box then then have no fill, but a blue Outline. But if the box already has no fill.... give it a blue fill!!! Basically, i'm creating a button that, click once.... blue fill, click again and blue outline, white fill. confusing?

    Function WM1()
    On Error Resume Next
    With ActiveWindow.Selection.ShapeRange
    If Fill.ForeColor.Solid Then
    With ActiveWindow.Selection.ShapeRange
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    .Fill.Visible = msoFalse
    End With
    Else: Fill.Visible = msoFalse
    With ActiveWindow.Selection.ShapeRange
    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(25, 61, 133)
    .Line.Visible = msoFalse
    End With
    End If
    End With

    With ActiveWindow.Selection.TextRange.Font
    .Color.RGB = RGB(Red:=255, Green:=255, Blue:=255)
    End With

    End Function

  2. #2
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    Try this:

    Function WM1()
    On Error Resume Next
    With ActiveWindow.Selection.ShapeRange
    If ActiveWindow.Selection.ShapeRange.Fill.Visible = msoTrue Then 'ForeColor.Solid Then
    With ActiveWindow.Selection.ShapeRange
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    .Fill.Visible = msoFalse
    End With
    Else
    Fill.Visible = msoFalse
    With ActiveWindow.Selection.ShapeRange
    .Fill.Visible = msoTrue
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(25, 61, 133)
    .Line.Visible = msoFalse
    End With
    End If
    End With

    With ActiveWindow.Selection.TextRange.Font
    .Color.RGB = RGB(Red:=255, Green:=255, Blue:=255)
    End With

    End Function
    Regards,
    Rudi

  3. #3
    Star Lounger
    Join Date
    Nov 2005
    Location
    London, Gtr London, United Kingdom
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    That's brilliant Rudi.

    Many thanks for that.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    You should use .Fill instead of Fill.
    You cannot use If Fill.ForeColor.Solid Then, for Solid is not a property, it is a method.
    The code can be simplified to

    With ActiveWindow.Selection.ShapeRange
    If .Fill.Visible Then
    .Fill.Visible = False
    .Line.Visible = True
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = vbWhite
    Else
    .Line.Visible = False
    .Fill.Visible = True
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(25, 61, 133)
    End If
    End With

    (Not sure it's a good idea to set the font color to white)

  5. #5
    Star Lounger
    Join Date
    Nov 2005
    Location
    London, Gtr London, United Kingdom
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    Thanks for this Hans. I was just mulling over how to shorten it.

    the white font should be there. I've moved that into the part with solid fill.

    Many thanks again

  6. #6
    Star Lounger
    Join Date
    Nov 2005
    Location
    London, Gtr London, United Kingdom
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    the final question on this!

    the final thing I needed was if nothing was selected, it would draw a blue rectangle.

    I have a feeling that this isn't the right way to do it, using a ErrorHandler, so any suggestions as to a neater way, I'd be eternally grateful.

    Function WM1()

    On Error GoTo ErrorHandler

    With ActiveWindow.Selection.ShapeRange
    If .Fill.Visible Then
    .Fill.Visible = False
    .Line.Visible = True
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = vbWhite
    With ActiveWindow.Selection.TextRange.Font
    .Color.RGB = vbBlack
    End With
    Else
    .Line.Visible = False
    .Fill.Visible = True
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(25, 61, 133)
    With ActiveWindow.Selection.TextRange.Font
    .Color.RGB = vbWhite
    End With
    End If
    End With
    Exit Function
    ErrorHandler:
    ActiveWindow.Selection.SlideRange.Shapes.AddShape( msoShapeRectangle, 59.62, 359.38, 198.38, 87#).Select
    With ActiveWindow.Selection.ShapeRange
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    End With
    End Function

  7. #7
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    This will work! In essence you are handling an error!

    PS: You must just think of the consequences. Run it under different scenarios and make sure that it works correctly under different conditions. If possible its best to refer to a specific object, not just at a selection, which could be anything!!!
    My VBA for powerpoint is not so good, but I'd consider that!!

    Cheers
    Regards,
    Rudi

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    You could test what type the current selection is:

    If ActiveWindow.Selection.Type = ppSelectionShapes Then
    ' your code here
    End If

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    As Hans says, this is the way to do it:
    <pre>Option Explicit

    Sub Wreck()
    Dim oSel As PowerPoint.Selection
    Set oSel = ActiveWindow.Selection
    Select Case oSel.Type
    Case ppSelectionNone
    With oSel.SlideRange.Shapes. _
    AddShape(msoShapeRectangle, 59.62, 359.38, 198.38, 87#)
    .Line.Visible = msoTrue
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = RGB(255, 255, 255)
    End With
    Case ppSelectionSlides
    MsgBox "In Slide sorter view"
    Case ppSelectionShapes
    With oSel.ShapeRange
    If .Fill.Visible Then
    .Fill.Visible = False
    .Line.Visible = True
    .Line.ForeColor.RGB = RGB(25, 61, 133)
    .Line.BackColor.RGB = vbWhite
    oSel.TextRange.Font.Color.RGB = vbBlack
    Else
    .Line.Visible = False
    .Fill.Visible = True
    .Fill.Solid
    .Fill.ForeColor.RGB = RGB(25, 61, 133)
    oSel.TextRange.Font.Color.RGB = vbWhite
    End If
    End With
    Case ppSelectionText
    MsgBox "Text selected in a shape or placeholder"
    End Select
    End Sub
    </pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    Star Lounger
    Join Date
    Nov 2005
    Location
    London, Gtr London, United Kingdom
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    Sam / Hans / Rudi

    this is spot on. many thanks

    As you can probably tell, i'm fairly inexperienced at VBA, and i've not seen the use of "case" before, and it seems to simplify things greatly, so I shall look into it

  11. #11
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Powerpoint VBA If statement (UK/PPT/2000)

    The Case statement in VB is very nice. It can be use as a substitute for a bunch of OR's as in <post:=127,502>post 127,502</post:> and Select Case True can simplify a complicated ElseIf as in <post:=415,241>post 415,241</post:>. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

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