[英]VBA to Copy and Paste Tables from PowerPoint to Excel
I have PowerPoint file with multiple tables, and I am trying to write an excel vba code to open the file, loop through the slides and shapes, and then transfer the tables into excel.我有包含多个表格的 PowerPoint 文件,我正在尝试编写 excel vba 代码来打开文件,遍历幻灯片和形状,然后将表格传输到 ZBF57C906FA7D2BB66D07372E41585D9Z6。 For this purpose I just want to know how to loop through a slide and transfer any and all shapes/tables to excel.
为此,我只想知道如何循环播放幻灯片并将任何和所有形状/表格转移到 excel。
Here is my current code:这是我当前的代码:
Sub PP_Test()
Dim s As Slide
Dim sh As Shape
Dim wbk As Workbook
Dim wsh As Worksheet
Set objPPT = CreateObject("Powerpoint.application")
objPPT.Visible = True
Dim file As String
file = "C:\Example.pptx"
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPres = pptApp.Presentations.Open(file)
Set wbk = Workbooks("Test.xlsm")
'Loop through the slides and loop through the shapes to find all the Tables. Copy the table, and paste them in Excel
For Each s In ActivePresentation.Slides
For Each sh In s.Shapes
'Create a new sheet in Excel
Set wsh = wbk.Worksheets.Add(After:=wbk2.Worksheets(wbk2.Worksheets.Count))
' Copy/paste the shape/table
sh.Copy
wsh2.Paste
Next sh
Next s
End Sub
I currently get the following Run-Time Error on the line "For Each s In Active Presentation.Slides": Run-time error '429': ActiveX component can't create object我目前在“For Each s In Active Presentation.Slides”行中收到以下运行时错误:运行时错误“429”:ActiveX 组件无法创建 object
I have looked around for examples, but I can only find examples of how to transfer tables from excel to PowerPoint, but not the other way around.我已经四处寻找示例,但我只能找到如何将表格从 excel 传输到 PowerPoint 的示例,而反之则不行。
Lots of problems with your code:你的代码有很多问题:
Slide
would be invalid and the compiler would complain), so no need to declare objPPT
as Object and use CreateObject
.Slide
类型将无效并且编译器会抱怨),因此无需将objPPT
声明为 Object 并使用CreateObject
。 It's much better to declare pptApp as PowerPoint.Application
and simply use the new
-command.pptApp as PowerPoint.Application
并简单地使用new
-command。CreateObject("PowerPoint.Application")
)CreateObject("PowerPoint.Application")
)Option Explicit
.Option Explicit
。 You should.wbk
but later use wbk2
.wbk
但稍后使用wbk2
。 Same with wsh
vs. wsh2
.wsh
与wsh2
相同。 Let the compiler help you find those problems.ActivePresentation
(and this is where your error comes from). ActivePresentation
不了解(这就是您的错误的来源)。 You could use pptApp.ActivePresentation
, but this is not necessary as you have already a reference to the presentation in pptPres
.pptApp.ActivePresentation
,但这不是必需的,因为您已经在pptPres
中引用了演示文稿。 Assuming early binding , your code could look like假设早期绑定,您的代码可能看起来像
Sub PP_Test()
Const filename = "C:\Example.pptx"
Dim wbk As Workbook, wsh As Worksheet
Dim pptApp As PowerPoint.Application, pptPres As PowerPoint.Presentation
Set pptApp = new PowerPoint.Application
pptApp.Visible = True
Set pptPres = pptApp.Presentations.Open(filename)
Set wbk = Workbooks("Test.xlsm")
'Loop through the slides and loop through the shapes to find all the Tables. Copy the table, and paste them in Excel
Dim s As PowerPoint.Slide, sh As PowerPoint.Shape
For Each s In pptPres.Slides
For Each sh In s.Shapes
'Create a new sheet in Excel
Set wsh = wbk.Worksheets.Add(After:=wbk.Worksheets(wbk.Worksheets.Count))
' Copy/paste the shape/table
sh.Copy
wsh.Paste
Next sh
Next s
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.