Consulting

Results 1 to 3 of 3

Thread: Conditions: "And" or "Or" or something else

  1. #1
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location

    Conditions: "And" or "Or" or something else

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    A ShapeRange can only have one type

    It cannot be This and That

    When you number the shapes you could check each shape type
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    Quote Originally Posted by John Wilson View Post
    A ShapeRange can only have one type

    It cannot be This and That
    This explains a lot ... :-)

    Quote Originally Posted by John Wilson View Post
    When you number the shapes you could check each shape type
    Great idea, thank you! It even made the whole code shorter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •