How to reset picture back from artistic effect in Excel VBA

Viewed 135

I am trying to figure out something. I have a picture in my Excel file. Whenever I click on it I want it to become grayscale so I use the following vba code:

Sheets("Dashboard-3").Shapes("ReportsIcon").Fill.PictureEffects.Insert msoEffectPencilGrayscale

And it works perfectly fine. But then I want it to get back to its original color and formatting when I click again (Which is the Reset Picture button in Excel). unfortunately recording Macro doesnt get that. I use msoEffectNone, msoEffectTypeNone, but doesnt work. Also searched a lot but seems no one has tried to reset the artistic effects back. Does anyone have a clue what should I use?

1 Answers

You don't need to set a different effect, you have to remove it with Delete from the list of PictureEffects that are assigned to the picture. The following Sub shows how to toggle it (if it is present, it is removed, else applied)

Sub toggleEfect(sh As Shape, efecttype As Long)
    Dim fe As PictureEffect, found As Boolean
    For Each fe In sh.Fill.PictureEffects
        If fe.Type = efecttype Then
            fe.Delete
            found = True
        End If
    Next
    If Not found Then sh.Fill.PictureEffects.Insert efecttype
End Sub

You can call it with

toggleEfect Sheets("Dashboard-3").Shapes("ReportsIcon"), msoEffectPencilGrayscale
Related