英文:
Changing font size within a Shape object (Table) in PowerPoint
问题
我正在尝试更改从Excel粘贴到PowerPoint的形状对象的字体大小。这是一个看起来像这样的表格:
我使用了在此主题中提供的方法,它有效,但问题是它运行得非常慢:
有没有办法加快这个过程?是否有一种方法可以模仿在形状内部执行Ctrl + A的行为,而不是逐个遍历表格中的每个单元格来立即更改字体?
英文:
I'm trying to change a font size for the Shape object I'm pasting to PowerPoint from Excel. This is a table that looks like this:
I used the way presented in this thread, and it works but the problem is that it works extremely slow:
Any idea how to speed this up? Is there a method that could replicate a behavior of Ctrl + A within a shape and change the font instantly instead of going through each and every cell within a table?
答案1
得分: 1
以下是您要翻译的内容:
原始代码
Sub changefontsizetablesoriginal()
Dim oShp As Shape
Dim oTbl As Table
Dim l As Long
Dim j As Long
Dim tT As Long
tT = Timer
Set oShp = ActivePresentation.Slides(3).Shapes(1)
Set oTbl = oShp.Table
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
oTbl.Cell(i, j).Shape.TextFrame.TextRange.Font.Size = 12
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
编辑后的代码(基于单元格数组,据说更快)
Sub changeFontSizeInTable()
Dim oSld As PowerPoint.slide
Dim oShp As PowerPoint.Shape
Dim oTbl As PowerPoint.Table
Dim i As Long
Dim j As Long
Dim cellArr() As PowerPoint.Shape
Dim tT As Long
tT = Timer
Set oSld = ActivePresentation.Slides(1)
Set oShp = oSld.Shapes(1)
Set oTbl = oShp.Table
ReDim cellArr(oTbl.Rows.Count, oTbl.Columns.Count)
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
Set cellArr(i, j) = oTbl.Cell(i, j).Shape
Next j
Next i
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
cellArr(i, j).TextFrame.TextRange.Font.Size = 16
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
Sub changeFontSizeInTable2()
Dim oSld As PowerPoint.slide
Dim oShp As PowerPoint.Shape
Dim oTbl As PowerPoint.Table
Dim i As Long
Dim j As Long
Dim cellArr() As PowerPoint.Shape
Dim tT As Long
tT = Timer
Set oSld = ActivePresentation.Slides(2)
Set oShp = oSld.Shapes(1)
Set oTbl = oShp.Table
ReDim cellArr(oTbl.Rows.Count, oTbl.Columns.Count)
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
Set cellArr(i, j) = oTbl.Cell(i, j).Shape
cellArr(i, j).TextFrame.TextRange.Font.Size = 12
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
希望这些翻译对您有帮助。如有任何其他问题,请随时提出。
英文:
Setting up an array of cells seemed to improve the speed of the macro you are referring to. I tested it with a table with 120 rows and 3 columns and it took one second against the 11 of the one you linked. however, the more I test it, the more various the runtime.
I made two versions, in the first the font size is applied at the end, in the second during the building of the arrays. It took the same amount of time, so it's up to you which one you prefer. I changed the slide index for all three for testing purposes, so you may have to edit that.
Just for reference, I am also posting the code you mentioned.
Original code
Sub changefontsizetablesoriginal()
Dim oShp As Shape
Dim oTbl As Table
Dim l As Long
Dim j As Long
Dim tT As Long
tT = Timer
Set oShp = ActivePresentation.Slides(3).Shapes(1)
Set oTbl = oShp.Table
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
oTbl.Cell(i, j).Shape.TextFrame.TextRange.Font.Size = 12
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
Edited code (supposedly faster, based on array of cells)
Sub changeFontSizeInTable()
Dim oSld As PowerPoint.slide
Dim oShp As PowerPoint.Shape
Dim oTbl As PowerPoint.Table
Dim i As Long
Dim j As Long
Dim cellArr() As PowerPoint.Shape
Dim tT As Long
tT = Timer
Set oSld = ActivePresentation.Slides(1)
Set oShp = oSld.Shapes(1)
Set oTbl = oShp.Table
ReDim cellArr(oTbl.Rows.Count, oTbl.Columns.Count)
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
Set cellArr(i, j) = oTbl.Cell(i, j).Shape
Next j
Next i
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
cellArr(i, j).TextFrame.TextRange.Font.Size = 16
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
Sub changeFontSizeInTable2()
Dim oSld As PowerPoint.slide
Dim oShp As PowerPoint.Shape
Dim oTbl As PowerPoint.Table
Dim i As Long
Dim j As Long
Dim cellArr() As PowerPoint.Shape
Dim tT As Long
tT = Timer
Set oSld = ActivePresentation.Slides(2)
Set oShp = oSld.Shapes(1)
Set oTbl = oShp.Table
ReDim cellArr(oTbl.Rows.Count, oTbl.Columns.Count)
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
Set cellArr(i, j) = oTbl.Cell(i, j).Shape
cellArr(i, j).TextFrame.TextRange.Font.Size = 12
Next j
Next i
Debug.Print Format(tT, "mm.ss.ssss")
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论