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

时间:2016-01-20 09:29:50

标签: vba excel-vba powerpoint-vba excel

所以我试图从使用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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

myPresentation.Slides(1).Shapes(1).Delete

将代码置于

之后
Set mySlide = myPresentation.Slides.Item(1)

当我使用它时,它从我的powerpoint中删除了我的桌子,但是它只是幻灯片中的一个表格,你可能需要更改形状中的数字以使其适合你。我也不知道它将如何继续使用,你可能需要不断更改数字。

我使用了This link 了解如何从powerpoint中删除项目

ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete

来自网站的原始代码是否已链接并使用反复试验进行了调整

This link 更多地解释形状,希望它有所帮助。在基本概述中,它基本上说在powerpoint中你可以输入的大多数项称为形状

如果您希望我进一步解释,请发表评论,我会尝试这样做