首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用VBa删除powerpoint中的表?

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

Stack Overflow用户
提问于 2016-01-20 09:29:49
回答 2查看 2.5K关注 0票数 0

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

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

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

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-20 10:38:16

代码语言:javascript
复制
myPresentation.Slides(1).Shapes(1).Delete

在后面将代码放置在上面

代码语言:javascript
复制
Set mySlide = myPresentation.Slides.Item(1)

当我使用它时,它从我的powerpoint中删除了我的表,但是它只是幻灯片中的一个表,您可能需要更改形状的数字才能使它对您有效。我也不知道它将如何公平地继续使用,你可能需要不断地改变号码。

我使用这个链接了解如何从powerpoint中删除项。

代码语言:javascript
复制
ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete

是来自网站链接的原始代码,并且是使用试用和错误改编的。

这个链接解释了更多的形状,希望它能有所帮助。在一个基本的概述中,它简单地说,在powerpoint中,您可以输入的大多数项目都称为形状。

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

票数 1
EN

Stack Overflow用户

发布于 2016-01-20 10:29:49

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

代码语言:javascript
复制
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

打电话给:

代码语言:javascript
复制
DeleteTablesFromSlide mySlide
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34896191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档