Solved

Change background image to greyscale programmatically using VBA in PowerPoint 2010?

Posted on 2014-02-24
5
1,797 Views
Last Modified: 2014-02-26
For a PowerPoint 2010 add-in for our corporate template, I've got a macro button that lets them select an image from the corporate library and insert that as the slide background.  I want to take that one step further and, if they select a specific layout, convert that background image to grayscale.  This is easy to do manually (right-click, format background, picture color, recolor, greyscale), but I need to do it with VBA.  Here is what I've tried:
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select Background Image"
fd.InitialFileName = contentlibraryfolder & "\Background Images"

With fd
    If .Show = -1 Then
        pickedpix = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With

Set fd = Nothing

currentslide.FollowMasterBackground = False

currentslide.Background.Fill.UserPicture (pickedpix)

If InStr(currentslide.CustomLayout.Name, "B&W") Then
    currentslide.Background.Fill.PictureEffects.Insert(msoEffectSaturation).EffectParameters(1).Value = 0
End If

Open in new window


This works great up to "currentslide.Background.Fill.UserPicture (pickedpix)."  But that last line fails to turn the picture to grayscale.  But it doesn't give me an error either.  I know I'm very close because this same pictureeffects method will turn a regular picture on the foreground of the slide to grayscale, but it's not changing the background.

I'm presenting this in 9 hours at a corporate meeting and would love to include this feature if someone can point me in the right direction.

Thanks!
0
Comment
Question by:Bryce Bassett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 23

Assisted Solution

by:JSRWilson
JSRWilson earned 150 total points
ID: 39889035
You cannot apply picture effects to a background image. You would need to modify an imaga and apply as a background.
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 350 total points
ID: 39889050
Hi,

pls try
Sub macro()
'Set currentslide = ActivePresentation.Slides(1)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select Background Image"
fd.InitialFileName = contentlibraryfolder & "\Background Images"

With fd
    If .Show = -1 Then
        pickedpix = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With

Set fd = Nothing

currentslide.FollowMasterBackground = False

currentslide.Background.Fill.UserPicture (pickedpix)

If InStr(currentslide.CustomLayout.Name, "B&W") = 0 Then
    Set oPic = currentslide.Shapes.AddPicture(FileName:=pickedpix, _
        LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)
    oPic.PictureFormat.ColorType = msoPictureBlackAndWhite
    strPicBWName = Left(pickedpix, InStrRev(pickedpix, ".") - 1) & "_bw.jpg"
    oPic.Export strPicBWName, ppShapeFormatJPG
    currentslide.Background.Fill.UserPicture (strPicBWName)
    Kill strPicBWName
    oPic.Delete
Else
    currentslide.Background.Fill.UserPicture (pickedpix)
End If

End Sub

Open in new window

REgards
0
 
LVL 23

Expert Comment

by:JSRWilson
ID: 39889055
Or maybe just have two master layout one gray and one color. They can easily be applied in code.
0
 

Author Closing Comment

by:Bryce Bassett
ID: 39890426
A few modifications needed, but mostly there.
0
 

Author Comment

by:Bryce Bassett
ID: 39890441
Thanks, Both.  Clever solution to import the image, modify it, export it, pull it in as the background, the nuke the temporary files!  

I did have to make a couple modifications.  ColorType = msoPictureBlackAndWhite gave me a literally 2 toned image, so I want back to the grayscale effect from my original post.  

Also (and you could not have known this), the contentlibraryfolder from which the picture is imported is a read-only network location to which I cannot write files, so I had to parse out the filename and save the temporary file to a local folder.  Here's what I ended up with:
If InStr(currentslide.CustomLayout.Name, "B&W") > 0 Then
    oPicFileName = Right(pickedpix, Len(pickedpix) - InStrRev(pickedpix, "\"))
    strPicBWName = Environ("appdata") & "\" & Left(oPicFileName, InStrRev(oPicFileName, ".") - 1) & "_bw.jpg"
    Set oPic = currentslide.Shapes.AddPicture(FileName:=pickedpix, _
        LinkToFile:=msoTrue, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)
    oPic.Fill.PictureEffects.Insert(msoEffectSaturation).EffectParameters(1).Value = 0
    oPic.Export strPicBWName, ppShapeFormatJPG
    currentslide.Background.Fill.UserPicture (strPicBWName)
    Kill strPicBWName
    oPic.Delete
Else
    currentslide.Background.Fill.UserPicture (pickedpix)
End If

Open in new window

Thanks for helping me solve this!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question