Powerpoint VBA - 如何修改代码以正确循环?
Powerpoint VBA - How to modify code to loop properly?
我有以下循环,用于在幻灯片 2 到 4 上创建一个三角形。
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i
代码有效,但因为我正在调用 ActiveWindow
,循环在同一张幻灯片(我 运行 宏的来源)而不是 3 张幻灯片上创建三角形 3 次.
我知道这是问题所在,但我不知道如何修改那部分代码来修复它。需要什么?
快速解决方案
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = ActivePresentation.Slides(i).Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i
您需要使用迭代演示幻灯片:
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = .Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i
我有以下循环,用于在幻灯片 2 到 4 上创建一个三角形。
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i
代码有效,但因为我正在调用 ActiveWindow
,循环在同一张幻灯片(我 运行 宏的来源)而不是 3 张幻灯片上创建三角形 3 次.
我知道这是问题所在,但我不知道如何修改那部分代码来修复它。需要什么?
快速解决方案
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = ActivePresentation.Slides(i).Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i
您需要使用迭代演示幻灯片:
For i = 2 To 4
With ActivePresentation.Slides(i)
Dim tri As Shape
Set tri = .Shapes.AddShape(msoShapeRightTriangle, 886, 0, 74, 74)
End With
Next i