复制PPT中粘贴多个Excel范围的宏

时间:2018-10-25 08:57:44

标签: excel vba excel-vba powerpoint

我终于能够创建此宏,该宏可从excel中的特定范围复制数据并将其粘贴到现有的PPT中。

现在,我想对多张幻灯片重复此操作,但是没有一遍又一遍地复制粘贴此宏,而是有任何更短的代码在这里,我只需更改范围,目标幻灯片,位置即可创建完整的集。 / p>

这是可以正常工作的现有代码:

'Macro1
Sub excelrangetopowerpoint_month()

    Dim rng As Range
    Dim powerpointapp As Object
    Dim mypresentation As Object
    Dim destinationPPT As String
    Dim myshape As Object
    Dim myslide As Object

    Set rng = Worksheets("objectives").Range("m1")

    On Error Resume Next

    Set powerpointapp = CreateObject("powerpoint.application")
    destinationPPT = ("C:\Users\OLX-Admin\Dropbox (Corporate Finance)\Naspers Monthly Reporting\Prep for call\From teams\FY2019\OLX Group Monthly Report_Sep'18_Macro.pptx")
    powerpointapp.Presentations.Open (destinationPPT)

    On Error GoTo 0

    Application.ScreenUpdating = False

    Set mypresentation = powerpointapp.ActivePresentation
    Set myslide = mypresentation.Slides(1)

    rng.Copy

    myslide.Shapes.PasteSpecial DataType:=2 '2 = enhanced metafile
    Set myshape = myslide.Shapes(myslide.Shapes.Count)

    myshape.Left = 278
    myshape.Top = 175

    powerpointapp.Visible = True
    powerpointapp.Activate

    Application.CutCopyMode = False

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用下面的其他步骤来做到这一点。因此,您只需为每张幻灯片复制一行即可。

还要注意,您的错误处理是静默的。这是一个坏主意,因为如果发生错误,您只需忽略它,就不会注意到。另外,以下代码将无法正常工作。我也改变了。

Sub excelrangetopowerpoint_month()
    Dim powerpointapp As Object
    Set powerpointapp = CreateObject("powerpoint.application")

    Dim destinationPPT As String
    destinationPPT = ("C:\Users\OLX-Admin\Dropbox (Corporate Finance)\Naspers Monthly Reporting\Prep for call\From teams\FY2019\OLX Group Monthly Report_Sep'18_Macro.pptx")

    On Error GoTo ERR_PPOPEN
    Dim mypresentation As Object
    Set mypresentation = powerpointapp.Presentations.Open(destinationPPT)
    On Error GoTo 0

    Application.ScreenUpdating = False

    PasteToSlide mypresentation.Slides(1), Worksheets("objectives").Range("m1")
    'duplicate this line for all slides/ranges
    'PasteToSlide mypresentation.Slides(2), Worksheets("objectives").Range("m2")

    powerpointapp.Visible = True
    powerpointapp.Activate

    Application.CutCopyMode = False

ERR_PPOPEN:
    Application.ScreenUpdating = True 'don't forget to turn it on!
    If Err.Number <> 0 Then
        MsgBox "Failed to open " & destinationPPT, vbCritical
    End If
End Sub


Private Sub PasteToSlide(mySlide As Object, rng As Range)
    rng.Copy
    mySlide.Shapes.PasteSpecial DataType:=2 '2 = enhanced metafile

    Dim myShape As Object
    Set myShape = mySlide.Shapes(mySlide.Shapes.Count)

    myShape.Left = 278
    myShape.Top = 175
End Sub