如何在演示文稿中的幻灯片之间循环,将新的Excel范围粘贴到每张幻灯片的表格中

时间:2019-05-15 09:45:58

标签: excel vba powerpoint powerpoint-vba

我正在尝试使用vba将excel中较大范围的每20行粘贴到powerpoint中,将每20行粘贴到单独幻灯片中的单独表中。我已经为此苦苦挣扎了一段时间,因此任何帮助将不胜感激。

我已经尝试遍历excel范围,我认为这是可行的,但是我没有设法将范围粘贴到单独的幻灯片中-目前,它们多次粘贴到同一张幻灯片中的同一张表中。

代码1:

在excel范围内循环,但将其粘贴到一张幻灯片中的一个特定表中,而不是将每20行粘贴到单独的幻灯片中的单独表中:

Private Sub pptpasting()
Dim r As Range
Dim powerpointapp As PowerPoint.Application
Dim mypresentation As Object

Set r = ThisWorkbook.Worksheets("...").Range("C1:D847")
Set powerpointapp = GetObject(class:="PowerPoint.Application")
Set mypresentation = powerpointapp.Presentations("....ppxt")

powerpointapp.Visible = True
powerpointapp.Activate

If powerpointapp Is Nothing Then
MsgBox "PowerPoint Presentation is not open, aborting."
Exit Sub
End If

'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0

'Make the presentation the active presentation
mypresentation.Windows(1).Activate

'copy range in excel to paste into table on powerpoint

 Dim z As Integer
 'here define the range to paste
  For z = 1 To 150 Step 20
  Range(r(z, 1), r(z + 19, 2)).Copy

' find the table on a specific slide
    With powerpointapp.ActivePresentation.Slides(3).Shapes(2).Table
    .Cell(1, 1).Select
    'paste into the table
    powerpointapp.CommandBars.ExecuteMso ("Paste")

    End With
Next z
End Sub

代码2:

在这里,我试图遍历演示文稿中的幻灯片,但失败了,并显示错误代码:Shape(未知成员)无效的请求。要选择形状,其视图必须处于活动状态

Private Sub pptpasting()
Dim r As Range
Dim powerpointapp As PowerPoint.Application
Dim mypresentation As Object

Set r = ThisWorkbook.Worksheets("...").Range("C1:D847")
Set powerpointapp = GetObject(class:="PowerPoint.Application")
Set mypresentation = powerpointapp.Presentations("....ppxt")

powerpointapp.Visible = True
powerpointapp.Activate

If powerpointapp Is Nothing Then
MsgBox "PowerPoint Presentation is not open, aborting."
Exit Sub
End If

   'Handle if the PowerPoint Application is not found
    If Err.Number = 429 Then
    MsgBox "PowerPoint could not be found, aborting."
    Exit Sub
    End If
    On Error GoTo 0

'Make the presentation the active presentation
mypresentation.Windows(1).Activate

'copy range in excel to paste into table on powerpoint
 Dim i As Integer
 Dim z As Integer

 'here define the range
For z = 1 To 150 Step 20
    Range(r(z, 1), r(z + 19, 2)).Copy

    'here loop through the slidse in the presentation, pasting into each slide
    For i = 3 To powerpointapp.ActivePresentation.Slides.Count
        With powerpointapp.ActivePresentation.Slides(i).Shapes(2).Table
        'Paste the range into the table
        .Cell(1, 1).Select
        powerpointapp.CommandBars.ExecuteMso ("Paste")
        End With
     Next i
Next z

End Sub

如上所述,我期望或正在尝试将每20行粘贴到一张单独的幻灯片中的单独表中,但是我尝试过的两种代码都无法正常工作-1)第一个代码粘贴通过excel范围循环的代码放入同一张幻灯片的同一张表中,并且2)第二个代码有错误。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我发现为PowerPoint表创建标签会很有帮助,将标签名称设置为TABLENAME,将标签值设置为Excel表的名称。然后,您可以循环搜索有问题的特定标签并更新该表,然后移至下一个。

我还建议您将Excel数据放入Excel中的表中,然后在vba中引用这些数据。