View Full Version : set color saturation of image with vba

03-12-2013, 04:53 PM

I am trying to set the color saturation of a jpg image with VBA (excel 2010).
I have found an msoeffectsaturation property, but cant find how to use it.

I wish to toggle the saturation of the image from greyscale to full color as part of a click-on, click-off procedure.

has anyone done this?

03-12-2013, 05:28 PM
I found this on a french language blog. it seems to call the right things...

Sub Effets()
Dim s As Shape
Set myDocument = Worksheets(1)
Set s = ActiveWorkbook.Worksheets(1).Shapes(1)
With s.Fill.PictureEffects
.Insert(msoEffectSaturation).EffectParameters(1).Value = 0.5
Dim brightnessContrast As PictureEffect
Set brightnessContrast = .Insert(msoEffectBrightnessContrast)
brightnessContrast.EffectParameters(1).Value = 0.1 ' Luminosité
brightnessContrast.EffectParameters(2).Value = -0.3 ' Contraste
' Remove all Picture effects.
While .Count > 0
.Delete (1)
End With
End Sub
my implementation of a single line of code
myPicture.Fill.PictureEffects.Insert(msoEffectSaturation).EffectParameters( 1).Value = 0 doesn't work though

03-13-2013, 06:29 AM
That line works for me. Does it not do anything at all when you try it or does it error? If it does nothing, are you sure you are referring to the correct shape?

03-17-2013, 01:14 PM
I am fairly sure I have the right shape - other code did work. I was trying to use this on a picture I was turning into a button. the code ran - but did nothing. Alternative explanation is that I was also playing with 3d formats and perspective - and the effects might be mutually exclusive in some way.

anyway the code is not at fault, so anyone who wants to have a play, go your hardest...

There is little documentation on this so, it is either a new/obscure function or not the best way to do this. Thanks for your comment

John Wilson
05-22-2013, 03:24 AM
Couldn't you toggle with

Sub toggle()
Dim oshp As Shape
Set oshp = ActiveWorkbook.Worksheets(1).Shapes(1)
With oshp.PictureFormat
Select Case .ColorType
Case Is = msoPictureGrayscale
.ColorType = msoPictureAutomatic
Case Is = msoPictureAutomatic
.ColorType = msoPictureGrayscale
End Select
End With
End Sub

OR to use saturation

With s.Fill.PictureEffects
.Delete (1)
.Insert(msoEffectSaturation).EffectParameters(1).Value = 0.5 'whatever
End With

05-23-2013, 05:30 PM
It may be that the saturation setting needs to be deleted, then reset - and that inserting the saturation value as I have attempted is not the same as editing the value.

I do like the 'understandablenes by ignoramuses' atribute of the greyscale toggle code however, and shall give this option a shot when I play next.