通过循环重构涉及 With 关键字的 VBA 脚本以减少代码长度
Refactoring via Loop a VBA script involving With keyword To reduce Code Length
在一个循环中,我在 PowerPoint 幻灯片中有一个 N x 1 table,当我创建它们时,我对它们进行了格式化。我在下面展示了一个 N=2 的例子。请问如何使用循环重构它以减少代码的长度?
With pptTable.Table.Cell(1, 1).Shape
.TextFrame.TextRange.Font.Name = "Calibri"
.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight
.TextFrame.TextRange.Font.Size = 54
.TextFrame2.VerticalAnchor = msoAnchorMiddle
End With
With pptTable.Table.Cell(2, 1).Shape
.TextFrame.TextRange.Font.Name = "Calibri"
.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight
.TextFrame.TextRange.Font.Size = 54
.TextFrame2.VerticalAnchor = msoAnchorMiddle
End With
您需要学习如何将 Activity 拆分成更小的块,以便它们可以封装在子程序或函数中。
Sub Test()
ReformatShape ppttable, 1, 1
ReformatShape ppttable, 2, 1
ReformatShape ppttable, 3, 1, ipsize:=48
ReformatShape ppttable, 4, 1, ipVerticalAnchor:=msoAnchorBottomBaseLine
' etc
End Sub
Public Sub ReformatShape _
( _
ByVal ipTable As PowerPoint.Table, _
ByVal ipX As Long, _
ByVal ipY As Long, _
Optional ByVal ipName As String = "Calibri", _
Optional ByVal ipAlignment As PpParagraphAlignment = ppAlignRight, _
Optional ByVal ipsize As Long = 54, _
Optional ByVal ipVerticalAnchor As MsoVerticalAnchor = msoAnchorMiddle _
)
With iptable.Table.Cell(ipX,ipY).Shape
.TextFrame.TextRange.Font.Name = ipName
.TextFrame.TextRange.ParagraphFormat.Alignment = ipAlignment
.TextFrame.TextRange.Font.Size = ipsize
.TextFrame2.VerticalAnchor = ipVerticalAnchor
End With
End Sub
在一个循环中,我在 PowerPoint 幻灯片中有一个 N x 1 table,当我创建它们时,我对它们进行了格式化。我在下面展示了一个 N=2 的例子。请问如何使用循环重构它以减少代码的长度?
With pptTable.Table.Cell(1, 1).Shape
.TextFrame.TextRange.Font.Name = "Calibri"
.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight
.TextFrame.TextRange.Font.Size = 54
.TextFrame2.VerticalAnchor = msoAnchorMiddle
End With
With pptTable.Table.Cell(2, 1).Shape
.TextFrame.TextRange.Font.Name = "Calibri"
.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight
.TextFrame.TextRange.Font.Size = 54
.TextFrame2.VerticalAnchor = msoAnchorMiddle
End With
您需要学习如何将 Activity 拆分成更小的块,以便它们可以封装在子程序或函数中。
Sub Test()
ReformatShape ppttable, 1, 1
ReformatShape ppttable, 2, 1
ReformatShape ppttable, 3, 1, ipsize:=48
ReformatShape ppttable, 4, 1, ipVerticalAnchor:=msoAnchorBottomBaseLine
' etc
End Sub
Public Sub ReformatShape _
( _
ByVal ipTable As PowerPoint.Table, _
ByVal ipX As Long, _
ByVal ipY As Long, _
Optional ByVal ipName As String = "Calibri", _
Optional ByVal ipAlignment As PpParagraphAlignment = ppAlignRight, _
Optional ByVal ipsize As Long = 54, _
Optional ByVal ipVerticalAnchor As MsoVerticalAnchor = msoAnchorMiddle _
)
With iptable.Table.Cell(ipX,ipY).Shape
.TextFrame.TextRange.Font.Name = ipName
.TextFrame.TextRange.ParagraphFormat.Alignment = ipAlignment
.TextFrame.TextRange.Font.Size = ipsize
.TextFrame2.VerticalAnchor = ipVerticalAnchor
End With
End Sub