Excel更新PowerPoint演示文稿

时间:2018-11-22 14:08:02

标签: excel vba excel-vba powerpoint powerpoint-vba

我有一个演示文稿,我必须每周更新。我更新的信息是我从Excel数据透视表生成的一堆图像(从Excel复制并直接粘贴在PowerPoint上)。 今天,我可以这样做:

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

Set PPTPrez = 
objPPT.Presentations.Open("\\network_folder\presentation.pptm")   
Set pSlide = PPTPrez.Slides(2)
If pSlide.Shapes.Count <> 0 Then
ActiveWorkbook.Sheets("Pivot1").Range("A8:Z18").CopyPicture
pSlide.Shapes.Paste  
EndIf

它工作完美无瑕...但是我需要一点点控制和精确度... 我需要在幻灯片上选择当前图像,将其删除并将新图像粘贴到同一位置...有些幻灯片具有3张图像或更多图像... 我不知道如何正确地告诉VBA什么图像是什么,并选择具有该图像正确信息的数据透视表...我什至不知道这是否可能... 但是我尝试过的另一个解决方案是如何在幻灯片上指定图像的位置和尺寸...我可以在更新之前删除所有图像...在这种情况下,如何指定尺寸和位置?

谢谢!!!

Ps .:对不起,我的英语不好

1 个答案:

答案 0 :(得分:0)

此示例(基于您的代码)可能会为您指明正确的方向。您需要知道PowerPoint形状的名称(可以通过VBA或功能区的Home-Select-Selection窗格获得。

Option Explicit

Public Sub UpdateShapes()

    Dim vPowerPoint As PowerPoint.Application
    Dim vPresentation As Presentation
    Dim vSlide As Slide

    Dim vShapeName As String
    Dim vShape, vNewShape

    Set vPowerPoint = New PowerPoint.Application
    vPowerPoint.Visible = True

    ' Open the powerpoint presentation
    Set vPresentation = vPowerPoint.Presentations.Open("\\network_folder\presentation.pptm")

    ' Set slide to be worked on
    Set vSlide = vPresentation.Slides(2)

    ' Set shape to (for this example) "Picture 3"
    vShapeName = "Picture 3"
    Set vShape = vSlide.Shapes(vShapeName)

    ' Copy and paste new shape (picture) of range specified
    ThisWorkbook.Sheets("Sheet1").Range("A6:B9").CopyPicture
    Set vNewShape = vSlide.Shapes.Paste

    ' Align size and position of new shape to that of old shape
    With vNewShape
        .Width = vShape.Width
        .Height = vShape.Height
        .Left = vShape.Left
        .Top = vShape.Top
    End With

    ' Delete original shape, rename new shape to original so code works next replace cycle
    vSlide.Shapes(vShapeName).Delete
    vNewShape.Name = vShapeName

End Sub