如何使用 VBa 在 powerpoint 中删除 table?

How do I delete a table in powerpoint using VBa?

所以我试图从使用 VBa 打开的 powerpoint 中删除 table,但我似乎无法让它工作。我已经尝试了一些东西,但它们从来没有任何效果,或者通常只是给我一个错误。

到目前为止我已经得到以下内容,它打开一个特定的 powerpoint 并将特定的 table 复制到第一张幻灯片。我真的很希望能够删除已经存在的 table 并用新的替换它。

我该怎么做?代码如下:

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
myPresentation.Slides(1).Shapes(1).Delete

将代码放在上面

Set mySlide = myPresentation.Slides.Item(1)

当我使用它时,它从我的 powerpoint 中删除了我的 table,但它只是幻灯片中的一个 table,您可能需要更改形状中的数字才能使它为您工作.我也不知道继续用会怎么样,可能需要不断换号。

我用过This link 了解如何从 powerpoint

中删除项目
ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete

是否链接了该站点的原始代码并通过反复试验对其进行了调整

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

如果你想让我进一步解释任何事情,请发表评论,我会尽力做到的