[英]Copying the Data from Excel to PowerPoint table using VBA
Let me explain you what I'm trying to do, then I'll explain you where I'm stuck 让我向您解释我要做什么,然后再向您解释我遇到的困难
--> Copy data from Excel table to PowerPoint table (they both have same header), for each row containing data ->对于包含数据的每一行,将数据从Excel表复制到PowerPoint表(它们都有相同的标题)
--> If table in PowerPoint crosses the certain height it should start pasting data on new slide ->如果PowerPoint中的表格超过一定高度,则应开始将数据粘贴到新幻灯片上
So, this how I'm approaching: 所以,这就是我正在接近的方式:
--> First, considering one slide in ppt as master slide, where table has headers and one blank row below it - I'll copy this slide and paste it when I'll move to next slide ->首先,将ppt中的一张幻灯片作为主幻灯片,其中表格具有标题,并在其下面有一个空白行-我将复制此幻灯片并将其粘贴到下一张幻灯片中
--> copy one row each from Excel and paste it in PowerPoint table ->从Excel复制每行并将其粘贴到PowerPoint表中
--> if the table in PowerPoint cross the certain height, then paste the new master slide (which I have in copy) ->如果PowerPoint中的表格跨过特定高度,则粘贴新的主幻灯片(我已将其复制)
--> this loop should continue for each row in Excel which has data. ->此循环应在Excel中包含数据的每一行继续进行。
I'm approaching to it bit by bit; 我正在一点一点地接近它。 so far I have created the loop for inserting the row in .ppt table and pasting the slide if it crosses the table height limit. 到目前为止,我已经创建了一个循环,用于将行插入.ppt表中,并在幻灯片超过表高度限制时粘贴幻灯片。 But I'm stuck here - the loop given below insert rows only for master slide and paste the next slide but doesn't insert in other slides. 但是我被困在这里-下面给出的循环仅针对主幻灯片插入行,并粘贴下一张幻灯片,但不插入其他幻灯片。
Public Sub Excel_cpy_pst()
Dim oTbl As Table
Dim mShp As ShapeRange
Dim shp As Object
Dim sldCount As Slide
Set sldCount = ActivePresentation.Slides(ActivePresentation.Slides.Count)
ActivePresentation.Slides(6).Copy 'Copying and using it as a master slide
Set mShp = ActivePresentation.Slides(ActivePresentation.Slides.Count).Shapes.Range(Array("MyShape"))
Set oTbl = mShp.Table
For Each shp In ActivePresentation.Slides
If mShp.Height <= 6.34 * 72 Then
With oTbl
.Rows.Add (-1) 'adding a row at the bottom of a table
End With
ElseIf ActivePresentation.Slides.Count <= 8 Then 'to stop it from infinite loop, putting a constatnt
With ActivePresentation.Slides
ActivePresentation.Slides.Paste (ActivePresentation.Slides.Count + 1)
End With
End If
Next
End Sub
If you start by duplicating your masterslide you can assign the new slide to a variable that can be referenced. 如果从复制主幻灯片开始,则可以将新幻灯片分配给可以引用的变量。
Dim aSLide as Powerpoint.SlideRange
Set aSlide = .Slides("Slide1").Duplicate
with aSlide
for each shp in aslide.shapes
Add this to your loop to reference the new copy of the original slide. 将此添加到循环中以引用原始幻灯片的新副本。
If your slide template (slide1) is constructed as a table, you can assign the text to each cell in the table then move to the next slide without checking the size. 如果幻灯片模板(slide1)构造为表格,则可以将文本分配给表格中的每个单元格,然后移至下一张幻灯片,而无需检查大小。 Just fill in the boxes as you go. 随您所愿,只需填写方框即可。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.