VBA PPT:如何添加图片并将其发送到文本后面或将其添加为背景图片?

VBA PPT: How to add an image and send it behind the text or add it as a background image?

所以我们正在尝试创建一些基于excel数据的ppt报告。我希望第一张幻灯片有背景图像或只是设置在文本后面的图像。这是我目前的代码:

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

问题

使用当前代码,它确实生成了在文本后面发送图像的 ppt,但我收到以下错误消息:Methods 'close' of object '_Presentation' failed。此外,演示文稿在保存到桌面后不会关闭。

我知道这与我添加图像的方式有关,因为如果我用下面的代码替换那行代码,它就可以正常工作(只有图像是 而不是 后面的文字):

    PowPntSlide.Shapes.AddPicture Filename:=imagePath, _
        LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, _
        Left:=0, _
        Top:=0, _
        Width:=960, _
        Height:=540

我尝试了什么

我又尝试了一件同样无效的事情。这是哪个:

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

但是那个代码只给了我一个 syntax error

你们很亲近。三项调整:

  1. Excel 有一个 Shape 对象。要使用 PowerPoint Shape 对象而不是 Excel Shape 对象,您需要将其声明为 PowerPoint.Shape.
  2. 当您从使用 AddPicture 调用转换为使用 Set image = 将其分配给对象时,您还需要在参数周围添加 ( )
  3. 你也想要.zorder msoSendToBack。根据 the documentationmsoBringInFrontOfTextmsoSendBehindText 常量应仅在 Microsoft Office Word 中使用。

这段代码对我有用:

    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

另一种方法是将图像创建为真正的背景图像,更像是幻灯片母版。这有时更可取,因为您不能直接 select、删除、移动等图像。该代码将替换以上所有内容:

    PowPntSlide.FollowMasterBackground = False  ' you must add this line
    With PowPntSlide.Background.Fill
        .UserPicture imagePath
        .Visible = True
    End With

背景代码示例来自:https://answers.microsoft.com/en-us/msoffice/forum/all/powerpoint-vba-select-image-and-turn-it-into/7ed490e5-5527-494b-b276-e8ace6e0bf0d?auth=1