So we are trying to create some ppt report based on excell data. I'd like for the first slide to have a background image or just an image that is set behind the text. This is my code currently:
Sub generatePptWithCharts()
Dim coverImagePath As String
imagePath = "C:\Users\user\Documents\vitalias-ppt-cover.png"
'Powerpoint objects.
Dim PowPntApp As PowerPoint.Application
Dim PowPntPrsnt As PowerPoint.Presentation
Dim PowPntSlide As PowerPoint.Slide
'Excel objects.
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim ChartObj As ChartObject
'Initialize the Excel objects.
Set wbBook = ThisWorkbook
Set PowPntApp = New PowerPoint.Application
PowPntApp.Visible = True
PowPntApp.Activate
Set PowPntPrsnt = PowPntApp.Presentations.Add
Set PowPntSlide = PowPntPrsnt.Slides.Add(1, ppLayoutTitle)
PowPntSlide.Shapes(1).TextFrame.TextRange = "Employee Information"
PowPntSlide.Shapes(2).TextFrame.TextRange = "by Presenter"
//This does not work properly
PowPntSlide.Shapes.AddPicture(imagePath, msoFalse, msoTrue, 0, 0, 960, 540).ZOrder msoSendToBack
PowPntPrsnt.SaveAs Environ("UserProfile") & "\Desktop\EmployeeInformation " & Format(Now, "yyyy-mm-dd hh-mm-ss") & ".pptx"
PowPntPrsnt.Close
PowPntApp.Quit
End Sub
The problem
With the current code, it does generate the ppt with a image send behind the text, but I am getting the following error message: Methods 'close' of object '_Presentation' failed
. Also, the presentation does not close after saving it to the desktop.
I know it has to do with the way I am adding the image, because if i replace that line of code with the following code, it works properly (only the image is not behind the text):
PowPntSlide.Shapes.AddPicture Filename:=imagePath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=960, _
Height:=540
What have I tried
I have tried one more thing that also does not work. Which is this:
Dim image As Shape
Set image = PowPntSlide.Shapes.AddPicture Filename:=imagePath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=960, _
Height:=540
image.ZOrder msoSendBehindText
But that code just gives me a syntax error
.
CodePudding user response:
You are very close. Three adjustments:
- Excel has a Shape object. To use the PowerPoint Shape object instead of the Excel Shape object, you need to declare it as
PowerPoint.Shape
. - When you shift from using the
AddPicture
call to assigning it to an object withSet image =
, you also need to add( )
around the parameters. - You also want
.zorder msoSendToBack
. Per the documentation: ThemsoBringInFrontOfText
andmsoSendBehindText
constants should be used only in Microsoft Office Word.
This code works for me:
Dim image As PowerPoint.Shape
Set image = PowPntSlide.Shapes.AddPicture(Filename:=imagePath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=960, _
Height:=540)
image.ZOrder msoSendToBack
Another alternative is to create the image as a true background image, more like a slide master. This is sometimes preferable because you can't select, delete, move, etc. the image directly. That code would replace all the above with:
PowPntSlide.FollowMasterBackground = False ' you must add this line
With PowPntSlide.Background.Fill
.UserPicture imagePath
.Visible = True
End With
Background code sample from: https://answers.microsoft.com/en-us/msoffice/forum/all/powerpoint-vba-select-image-and-turn-it-into/7ed490e5-5527-494b-b276-e8ace6e0bf0d?auth=1