如何使用VBa删除powerpoint中的表格?

如何使用VBa删除powerpoint中的表格?,vba,excel,powerpoint,Vba,Excel,Powerpoint,因此,我试图从使用VBa打开的powerpoint中删除一个表,但似乎无法使其正常工作。我试过一些方法,但它们从来没有任何效果,或者通常只是给我一个错误 到目前为止,我已经得到了以下信息,它打开了一个特定的powerpoint,并在特定的表格中复制到第一张幻灯片。我真的希望能够删除已经存在的表,并用新表替换它 我该怎么做呢?代码如下: Sub ExcelRangeToPowerPoint() 'PURPOSE: Copy/Paste An Excel Range Into a New Powe

因此,我试图从使用VBa打开的powerpoint中删除一个表,但似乎无法使其正常工作。我试过一些方法,但它们从来没有任何效果,或者通常只是给我一个错误

到目前为止,我已经得到了以下信息,它打开了一个特定的powerpoint,并在特定的表格中复制到第一张幻灯片。我真的希望能够删除已经存在的表,并用新表替换它

我该怎么做呢?代码如下:

Sub ExcelRangeToPowerPoint()

'PURPOSE: Copy/Paste An Excel Range Into a New PowerPoint Presentation

Dim rng As Excel.Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Dim myShapeRange As PowerPoint.Shape

'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("Table1[#ALL]")

'Create an Instance of PowerPoint
On Error Resume Next

'Is PowerPoint already opened?
  Set PowerPointApp = GetObject(class:="PowerPoint.Application")

'Clear the error between errors
  Err.Clear

'If PowerPoint is not already open then open PowerPoint
  If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")

'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 PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate

'Create a New Presentation
Set myPresentation =         PowerPointApp.Presentations.Open("Y:\Projects\VBa\vbatest2.pptx")

'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Item(1)

'Delete current table in presentation
'ActivePresentation.Slides(1).Shapes(1).Delete

'Copy Excel Range
rng.Copy

'Paste to PowerPoint and position
 mySlide.Shapes.PasteSpecial DataType:=ppPasteSourceFormatting
 Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)

'Set position:
  myShapeRange.Left = 20
  myShapeRange.Top = 100
  myShapeRange.Height = 400
  myShapeRange.Width = 900

'Clear The Clipboard
Application.CutCopyMode = False

End Sub

尝试调用此函数删除指定幻灯片中的所有表格:

Option Explicit

' Deletes all tables from the specified slide (table shapes and tables within placeholders)
' Returns the number of tables deleted
' Written by Jamie Garroch of YOUpresent Ltd. (http://youpresent.co.uk)
Public Function DeleteTablesFromSlide(mySlide As PowerPoint.Slide) As Long
  Dim lCntr As Long
  Dim lTables As Long
  ' Count backwards when deleting items from a collection
  For lCntr = mySlide.Shapes.Count To 1 Step -1
    With mySlide.Shapes(lCntr)
      Select Case .Type
        Case msoTable: .Delete: lTables = lTables + 1 ' msoTable = 19
        Case msoPlaceholder ' msoPlaceholder = 19
          If .PlaceholderFormat.ContainedType = msoTable Then .Delete: lTables = lTables + 1
      End Select
    End With
  Next
  DeleteTablesFromSlide = lTables
End Function
致电:

DeleteTablesFromSlide mySlide

尝试调用此函数删除指定幻灯片中的所有表格:

Option Explicit

' Deletes all tables from the specified slide (table shapes and tables within placeholders)
' Returns the number of tables deleted
' Written by Jamie Garroch of YOUpresent Ltd. (http://youpresent.co.uk)
Public Function DeleteTablesFromSlide(mySlide As PowerPoint.Slide) As Long
  Dim lCntr As Long
  Dim lTables As Long
  ' Count backwards when deleting items from a collection
  For lCntr = mySlide.Shapes.Count To 1 Step -1
    With mySlide.Shapes(lCntr)
      Select Case .Type
        Case msoTable: .Delete: lTables = lTables + 1 ' msoTable = 19
        Case msoPlaceholder ' msoPlaceholder = 19
          If .PlaceholderFormat.ContainedType = msoTable Then .Delete: lTables = lTables + 1
      End Select
    End With
  Next
  DeleteTablesFromSlide = lTables
End Function
致电:

DeleteTablesFromSlide mySlide
将代码放在后面

Set mySlide = myPresentation.Slides.Item(1)
当我使用它时,它从我的powerpoint中删除了我的表格,但它只是幻灯片中的一张表格,您可能需要更改形状中的数字才能使其适用于您。我也不知道它将如何公平地继续使用,您可能需要不断更改号码

我曾经 了解如何从powerpoint中删除项目

ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete
网站上的原始代码是否链接并使用试错法进行了修改

再解释一下形状,希望能有所帮助。在基本概述中,它基本上说,在powerpoint中,您可以在其中输入的大多数项目称为形状

如果你想让我进一步解释,请留下评论,我会尽力这样做

将代码放在后面

Set mySlide = myPresentation.Slides.Item(1)
当我使用它时,它从我的powerpoint中删除了我的表格,但它只是幻灯片中的一张表格,您可能需要更改形状中的数字才能使其适用于您。我也不知道它将如何公平地继续使用,您可能需要不断更改号码

我曾经 了解如何从powerpoint中删除项目

ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete
网站上的原始代码是否链接并使用试错法进行了修改

再解释一下形状,希望能有所帮助。在基本概述中,它基本上说,在powerpoint中,您可以在其中输入的大多数项目称为形状


如果您想让我进一步解释,请留下评论,我将尝试这样做

这是可行的,请确保将表格的形状(编号)设置为正确的值。谢谢这是可行的,只是确保将表格的形状(编号)设置为正确的值。谢谢