【发布时间】:2023-03-14 10:35:01
【问题描述】:
我想使用 VBA 将表格从 excel 粘贴到 power point。但是,由于我有动态范围,因此我想创建 15 行的幻灯片,以便更好地可视化。例如,它将第 1 行到第 15 行粘贴到第 1 号幻灯片中,然后粘贴到第 1 行,将第 16 行到第 29 行粘贴到第 2 号幻灯片中,依此类推。这里第 1 行是表的标题。我附上了我只能创建一张幻灯片的代码。如果有人可以帮助我,我将不胜感激。
Sub SortingandSlidecreation()
Dim pptName As String
Dim ppt As PowerPoint.Application
Dim myPres As PowerPoint.Presentation
Dim slds As PowerPoint.Slides
Dim sld As PowerPoint.slide
Dim pptextbox As PowerPoint.Shape
Dim oLayout As CustomLayout
Dim wb As Workbook
Dim ws As Worksheet
Dim y As Workbook, LastRow&
Dim r As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("SortedTable")
'This will open a PowerPoint template (I didn't attach the function)
pptName = openDialog()
Set ppt = CreateObject("PowerPoint.Application")
Set myPres = ppt.Presentations.Open(pptName)
Set slds = myPres.Slides
' creating slides at the end of the template
Set sld = slds.Add(myPres.Slides.Count + 1, ppLayoutBlank)
'Here data is selected for pasting
Set r = ThisWorkbook.Worksheets("SortedTable").Range("A1:L" & LastRow)
r.Copy
sld.Shapes.PasteSpecial DataType:=0
sld.Shapes(1).Top = 100
sld.Shapes(1).Left = 100
'Here title of the table is added
Set pptextbox = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 22, 60, 700, 60)
With pptextbox.TextFrame
.TextRange.Text = "Summary of Current Projects"
.TextRange.Font.Bold = msoTrue
.TextRange.Font.Name = "Arial(Headings)"
.TextRange.Font.Size = 20
.TextRange.Font.Color.RGB = RGB(0, 51, 102)
End With
End Sub
【问题讨论】:
-
所有幻灯片的标题是否相同,您需要帮助的只是创建几张幻灯片?也是数据 A 到 L 的列吗?
-
@AAA 标题将与我将粘贴同一张表相同。正如我所提到的,第一行是 A 到 L 列的标题。因此,它将被粘贴到每张幻灯片中而没有任何更改。最终目标是如果表格包含超过 15 行,则将表格放入多张幻灯片中。
-
你试过下面的答案吗?
-
@AAA 现在可以完美运行了。我还有一个问题。如何在 PPT 中定义/固定表格的大小?大小一直在变化,字体变得非常小。
-
您需要在 Powerpoint 中编辑粘贴的内容吗?如果不是,为什么不粘贴为位图(`DataType:=1)?或者只是使用普通粘贴和源格式
标签: excel vba powerpoint