在PowerPoint中更改形状对象(表格)内的字体大小。

huangapple go评论116阅读模式
英文:

Changing font size within a Shape object (Table) in PowerPoint

问题

我正在尝试更改从Excel粘贴到PowerPoint的形状对象的字体大小。这是一个看起来像这样的表格:

在PowerPoint中更改形状对象(表格)内的字体大小。

我使用了在此主题中提供的方法,它有效,但问题是它运行得非常慢:

https://stackoverflow.com/questions/36481106/change-the-font-size-within-a-pasted-shape-in-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:

在PowerPoint中更改形状对象(表格)内的字体大小。

I used the way presented in this thread, and it works but the problem is that it works extremely slow:

https://stackoverflow.com/questions/36481106/change-the-font-size-within-a-pasted-shape-in-powerpoint

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

huangapple
  • 本文由 发表于 2023年3月9日 17:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定