将表从Excel复制到PowerPoint VBA

时间:2016-06-30 12:43:13

标签: vba excel-vba powerpoint shape excel

我正在尝试将包含Excel表格形状的表格复制并粘贴到PowerPoint幻灯片中,使用VBA保持其源格式[Snapshot1]。 我想在粘贴后直接写在幻灯片上的故事。除了形状没有粘贴到表[Snapshot2]中之外,一切似乎都能正常工作。

Sub CreatePP()
    Dim ppapp As PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSlide As PowerPoint.Slide
    Dim ppTextBox As PowerPoint.Shape
    Dim iLastRowReport As Integer
    Dim sh As Object
    Dim templatePath As String

        On Error Resume Next
        Set ppapp = GetObject(, "PowerPoint.Application")
        On Error GoTo 0

    'Let's create a new PowerPoint
        If ppapp Is Nothing Then
            Set ppapp = New PowerPoint.Application
        End If
    'Make a presentation in PowerPoint
        If ppapp.Presentations.Count = 0 Then
           Set ppPres = ppapp.Presentations.Add
           ppPres.ApplyTemplate "C:\Users\luunt1\AppData\Roaming\Microsoft\Templates\Document Themes\themevpb.thmx"
        End If

    'Show the PowerPoint
        ppapp.Visible = True

         For Each sh In ThisWorkbook.Sheets
         If sh.Name Like "E_KRI" Then
            ppapp.ActivePresentation.Slides.Add ppapp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
            ppapp.ActiveWindow.View.GotoSlide ppapp.ActivePresentation.Slides.Count
            Set ppSlide = ppapp.ActivePresentation.Slides(ppapp.ActivePresentation.Slides.Count)
            ppSlide.Select


            iLastRowReport = Range("B" & Rows.Count).End(xlUp).Row
            Range("A1:J" & iLastRowReport).Copy
            DoEvents
            ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")
            Wait 3
            With ppapp.ActiveWindow.Selection.ShapeRange
              .Width = 700
              .Left = 10
              .Top = 75
              .ZOrder msoSendToBack
            End With
            Selection.Font.Size = 12
          'On Error GoTo NoFileSelected
            AppActivate ("Microsoft PowerPoint")
            Set ppSlide = Nothing
            Set ppapp = Nothing
    End If
    Next   
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

1 个答案:

答案 0 :(得分:0)

而不是选择表格和粘贴的范围,它可以解决您的解决方案而不是粘贴表格对象本身,所以:

ActiveSheet.ListObjects(1).Copy  'Assuming it is the only table on the sheet.  Adjust this code as needed for your specific case