因此,我试图从使用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发布于 2016-01-20 10:38:16
myPresentation.Slides(1).Shapes(1).Delete在后面将代码放置在上面
Set mySlide = myPresentation.Slides.Item(1)当我使用它时,它从我的powerpoint中删除了我的表,但是它只是幻灯片中的一个表,您可能需要更改形状的数字才能使它对您有效。我也不知道它将如何公平地继续使用,你可能需要不断地改变号码。
我使用这个链接了解如何从powerpoint中删除项。
ActivePresentation.Slides(2).Shapes(5).Table.Rows(3).Delete是来自网站链接的原始代码,并且是使用试用和错误改编的。
这个链接解释了更多的形状,希望它能有所帮助。在一个基本的概述中,它简单地说,在powerpoint中,您可以输入的大多数项目都称为形状。
如果你想让我进一步解释,请留下评论,我会尽力这样做的
发布于 2016-01-20 10:29:49
尝试调用此函数删除指定幻灯片中的所有表:
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 mySlidehttps://stackoverflow.com/questions/34896191
复制相似问题