VBA Powerpoint - 从形状获取参数并移交给另一个形状
VBA Powerpoint - get Parameters from Shape and hand over to another shape
我正在尝试构建一个 VBA-Powerpoint 宏,它返回激活形状(通过鼠标)的参数。
想法是用第二个宏将参数移交给另一个形状。这需要先激活(通过鼠标)。
步骤:
- 点击形状
- 激活宏选择测试 -> 反馈参数
- 点击第二个形状
- 激活第二个宏->交出参数
- 两个形状的大小和位置相同
我的问题是我不明白如何检查参数是否在我的 Dim 中以及如何将其移交。
我是 VBA 的新手,正在努力学习 :)
Dim width As Double
Dim height As Double
Dim x As Double
Dim y As Double
Sub selectTest()
With PowerPoint.ActiveWindow.Selection.ShapeRange
width = .width
height = .height
x = .Left
y = .Top
End With
End Sub
该代码包含全局变量的声明和两个过程 - getParams
将形状的参数(坐标和尺寸)保存到全局变量,setParams
将保存的参数分配给另一个形状。全局(在子函数或函数外部定义)变量在调用子函数或函数之间保留其值。
在每个过程的开始,我们检查是否有任何数字被 selected(这个表达式的结果必须是 False
):ActiveWindow.Selection.Type = ppSelectionNone
。 setParams
过程还检查参数是否以前保存过。为此,使用了变量 paramsFilled
,其默认值为 False
,但在保存参数后,它被分配了值 True
。
如果上述检查成功通过,则执行主要代码 - 将形状属性的值分配给变量,反之亦然,将保存在变量中的值分配给形状属性。
ActiveWindow.Selection.ShapeRange(1)
意味着我们 select 来自 selected 的第一个形状 (a shape range can contain as few as a single shape or as many as all the shapes on the document)。
如果检查不成功,则输出相应的消息。
Dim width As Double
Dim height As Double
Dim x As Double
Dim y As Double
Dim paramsFilled As Boolean 'if true indicates that the parameters have been filled
Sub getParams()
If ActiveWindow.Selection.Type = ppSelectionNone Then
MsgBox "Nothing selected", vbCritical
Else
With ActiveWindow.Selection.ShapeRange(1)
width = .width
height = .height
x = .Left
y = .Top
End With
End If
paramsFilled = True ' the parameters have been saved
End Sub
Sub setParams()
If Not paramsFilled Then 'check if the parameters are filled in
MsgBox "Parameters were not saved", vbCritical
Exit Sub
End If
If ActiveWindow.Selection.Type = ppSelectionNone Then
MsgBox "Nothing selected", vbCritical
Else
With ActiveWindow.Selection.ShapeRange(1)
.width = width
.height = height
.Left = x
.Top = y
End With
paramsFilled = False 'optionally you can reset stored parameters after apply or leave them for future use
End If
End Sub
我正在尝试构建一个 VBA-Powerpoint 宏,它返回激活形状(通过鼠标)的参数。
想法是用第二个宏将参数移交给另一个形状。这需要先激活(通过鼠标)。
步骤:
- 点击形状
- 激活宏选择测试 -> 反馈参数
- 点击第二个形状
- 激活第二个宏->交出参数
- 两个形状的大小和位置相同
我的问题是我不明白如何检查参数是否在我的 Dim 中以及如何将其移交。
我是 VBA 的新手,正在努力学习 :)
Dim width As Double
Dim height As Double
Dim x As Double
Dim y As Double
Sub selectTest()
With PowerPoint.ActiveWindow.Selection.ShapeRange
width = .width
height = .height
x = .Left
y = .Top
End With
End Sub
该代码包含全局变量的声明和两个过程 - getParams
将形状的参数(坐标和尺寸)保存到全局变量,setParams
将保存的参数分配给另一个形状。全局(在子函数或函数外部定义)变量在调用子函数或函数之间保留其值。
在每个过程的开始,我们检查是否有任何数字被 selected(这个表达式的结果必须是 False
):ActiveWindow.Selection.Type = ppSelectionNone
。 setParams
过程还检查参数是否以前保存过。为此,使用了变量 paramsFilled
,其默认值为 False
,但在保存参数后,它被分配了值 True
。
如果上述检查成功通过,则执行主要代码 - 将形状属性的值分配给变量,反之亦然,将保存在变量中的值分配给形状属性。
ActiveWindow.Selection.ShapeRange(1)
意味着我们 select 来自 selected 的第一个形状 (a shape range can contain as few as a single shape or as many as all the shapes on the document)。
如果检查不成功,则输出相应的消息。
Dim width As Double
Dim height As Double
Dim x As Double
Dim y As Double
Dim paramsFilled As Boolean 'if true indicates that the parameters have been filled
Sub getParams()
If ActiveWindow.Selection.Type = ppSelectionNone Then
MsgBox "Nothing selected", vbCritical
Else
With ActiveWindow.Selection.ShapeRange(1)
width = .width
height = .height
x = .Left
y = .Top
End With
End If
paramsFilled = True ' the parameters have been saved
End Sub
Sub setParams()
If Not paramsFilled Then 'check if the parameters are filled in
MsgBox "Parameters were not saved", vbCritical
Exit Sub
End If
If ActiveWindow.Selection.Type = ppSelectionNone Then
MsgBox "Nothing selected", vbCritical
Else
With ActiveWindow.Selection.ShapeRange(1)
.width = width
.height = height
.Left = x
.Top = y
End With
paramsFilled = False 'optionally you can reset stored parameters after apply or leave them for future use
End If
End Sub