【问题标题】:Macro to copy paste multiple excel ranges in PPT在PPT中复制粘贴多个excel范围的宏
【发布时间】:2020-04-10 03:51:20
【问题描述】:

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

现在我想对多张幻灯片重复此操作,但不是一次又一次地复制粘贴此宏,是否有任何更短的代码,我只需更改范围、目标幻灯片、定位并创建完整的集合。

这是运行良好的现有代码:

'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

【问题讨论】:

    标签: excel vba powerpoint


    【解决方案1】:

    您可以使用下面的其他程序来完成。因此,您只需为幻灯片的每个副本复制一行。

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 2018-09-27
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多