Search code examples
vbapowerpoint

How to select shape and run macro to fill color?


I am trying to select a shape and fill this shape with a color in a PowerPoint presentation like that (I put my code on the green button):

Sub ChangeInRed()


   If ActiveWindow.Selection.Type = ppSelectionNone Then
        MsgBox "Please selecte a zone !"
   Else
        For Each shp In ActiveWindow.Selection.ShapeRange
          shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
        Next shp
   End If


End Sub

If I run my code from macros-code, my selected shape changes to red; if I don't have a shape selected, my code displays: "Please select a zone".

All good, but in the edit mode, my button is not active to run this piece of code and I tried that in PRESENTATION mode.

When I try to select a shape in the presentation mode, my slideshow gets to the next page but I don't need that.

I want to select the shape and press the button to change the color to red, or I want to click on the shape and change the color.

If I try the code from the top in macro-code, my shape changes to red, but not in presentation.

My question is: how to select a shape in the presentation mode and change color?


Solution

  • If you want the shape to change colour during a slide show, copy the macro below to your PowerPoint .pptm (or .ppsm) file and then with the shape selected, go to the PowerPoint ribbon and click Insert / Action and in the Mouse Click tab, click Run Macro and make sure the ChangeShapeColour macro is selected before clicking OK.

    enter image description here

    Option Explicit
    
    Sub ChangeShapeColour(ByRef oShp As Shape)
      oShp.Fill.ForeColor.RGB = RGB(255, 0, 0)
    End Sub