PDA

View Full Version : [SOLVED:] Conditions: "And" or "Or" or something else



RandomGerman
09-05-2017, 11:58 AM
Hi all,

the macro, I'm currently working on, should number different objects in order of selection by the user. It is working quite well with one little exception and I must have made a mistake with the conditions, creating this exception, but I don't see it. Maybe I didn't understand the correct usage of "or" and "and" in if-conditions, but it is impossible to search for the keywords "and" and "or", even in this forum.

The user can select one or more objects on a slide and then activate the macro to number them.

The conditions - what I would like to have (and what I have in brackets):
One or more shapes are selected - Marco should work (and it does)
One or more text boxes are selected - Macro should work (and it does)
One or more other types (e.g., groups, pictures) are selected, no matter how many and if mixed with shapes and/or text boxes or not - MsgBox should appear (and it does)
So far so good, but:
One or more shapes AND one or more text boxes are selected - Macro should work (and it does not. MsgBox appears instead)

After all I'm quite sure, which line of my code is wrong. I commented in the code.




Sub NumberSelectedShapes2()

Dim shRange As ShapeRange
Dim totNum As Long
Dim n As Long
Dim x As Long
Dim shp As Shape

If ActiveWindow.Selection.Type = ppSelectionText Then
GoTo NumberTheShapes
ElseIf ActiveWindow.Selection.Type <> ppSelectionShapes Then
MsgBox "Please select a shape"
Exit Sub
End If

Set shRange = ActiveWindow.Selection.ShapeRange

If shRange.Type = msoAutoShape Then
GoTo NumberTheShapes
ElseIf shRange.Type = msoTextBox Then
GoTo NumberTheShapes
ElseIf shRange.Type = msoAutoShape And shRange.Type = msoTextBox Then 'This one must be the problem line
GoTo NumberTheShapes
Else
MsgBox "Please select shapes or text boxes only. It is not possible to number groups, pictures, lines, or connectors"
Exit Sub
End If

NumberTheShapes:
'Define the number to start with
x = 1
'Count the selected shapes
totNum = ActiveWindow.Selection.ShapeRange.Count
'Number the shapes
For n = 1 To totNum
With ActiveWindow.Selection.ShapeRange(n)
.TextFrame2.TextRange.InsertBefore (x & ". ") '(insert with dot and space and before text)
x = x + 1 'go on counting up
End With
Next

End Sub


I tried with positive options, too. But the result was the same.


If shRange.Type = msoAutoShape Then
GoTo NumberTheShapes
ElseIf shRange.Type = msoTextBox Then
GoTo NumberTheShapes
ElseIf shRange.Type = msoAutoShape And shRange.Type = msoTextBox Then
GoTo NumberTheShapes
Else
MsgBox "Please select shapes or text boxes only. It is not possible to number groups, pictures, lines, or connectors"
Exit Sub
End If


I hope, someone can identify my mistake and help me correct it.

Thanks so much in advance
RG

John Wilson
09-06-2017, 12:30 AM
A ShapeRange can only have one type

It cannot be This and That

When you number the shapes you could check each shape type

RandomGerman
09-06-2017, 02:30 AM
A ShapeRange can only have one type

It cannot be This and That

This explains a lot ... :-)


When you number the shapes you could check each shape type

Great idea, thank you! It even made the whole code shorter.