3

We are trying to create a ppt report based on Excel data.

I'd like the first slide to have a background image or just an image that is set behind the text.

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

The code generates the ppt with an image sent 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

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

That code gives me a syntax error.

1 Answer 1

3

You are very close. Three adjustments:

  1. 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.
  2. When you shift from using the AddPicture call to assigning it to an object with Set image =, you also need to add ( ) around the parameters.
  3. You also want .zorder msoSendToBack. Per the documentation: The msoBringInFrontOfText and msoSendBehindText 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.