[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-02-24
5
Medium Priority
?
1,960 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
  • 2
  • 2
5 Comments
 
LVL 23

Assisted Solution

by:JSRWilson
JSRWilson earned 450 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 54

Accepted Solution

by:
Rgonzo1971 earned 1050 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

872 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