Reputation: 287
I am trying to build a VBA-Powerpoint macro which gives the parameters of an activated shape (by mouse) back.
The idea is to hand over the parameters with a second macro to another shape. This need to be activated before (by mouse).
Steps:
My problem is that i dont understand how to check if the paramters are in my Dim and how to hand it over.
I am new to VBA and trying to learn :)
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
Upvotes: 0
Views: 691
Reputation: 7627
The code contains a declaration of global variables and two procedures - getParams
to save parameters of a shape (coordinates and dimensions) to global variables and setParams
to assign saved parameters to another shape. Global (defined outside a sub or function) variables retain their values between calls to a sub or function.
At the beginning of each procedure we check if any figure is selected (the result of this expression must be False
): ActiveWindow.Selection.Type = ppSelectionNone
. The setParams
procedure also checks if the parameters were previously saved. To do this, the variable paramsFilled
is used, the default value of which is False
, but after saving the parameters it is assigned the value True
.
If the above checks are passed successfully, the main code is executed - either assigning values of the shape properties to the variables or vice versa assigning the values saved in the variables to the shape properties.
ActiveWindow.Selection.ShapeRange(1)
means that we select the first shape from the selected ones (a shape range can contain as few as a single shape or as many as all the shapes on the document).
If the checks are unsuccessful, the corresponding messages are output.
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
Upvotes: 1