Consulting

Results 1 to 7 of 7

Thread: Which shape triggered the macro?

  1. #1
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    4
    Location

    Which shape triggered the macro?

    I am trying to simulate a combination lock as part of an escape room power point. On one particular slide there are 4 text boxes. The user can click any text-box to increment the number in that particular text box. The VBA code is the same for all four text boxes so I'd like to just write it once (for easy maintenance) because the behavior is exactly the same for each "Dial". BUT it looks like macros can not use a parameter list so I can't give the macro an index (or tag) as to which of the text-boxes the user clicked (the sub name doesn't show up in the macro list if there is a anything in the brackets of the sub). I guess I could just copy and paste the code four times, change the macro name and set the action to a specific copy - but surely there is a better way?

    Thank you in advance - this is a real challenge for me.

    The code below:
    I've named the text boxes 0, 1, 2, 3. There is also a Reset button, to reset the lock to: 0 0 0 0. And a star that turns green if the correct combination is in the text boxes.
    And all the commented out code are futile attempts at discovering the index or name of the specific text box that triggered the macro.

    LOCK1 is a workaround: If I happen to insert a new slide in front of this "combo lock" slide, the combo lock slide gets a new slide number then the macros are useless. I would like to use the macro for any slide, but one problem at a time I suppose.


    Const LOCK1 As Integer = 1 ' slide index for combination lock
    Dim D(4) As Integer 'combination dials
    
    
    
    
    Sub SpinDial()
        Dim shpDial As Shape
        Dim i As Integer
        Dim Test As Boolean
        
        MsgBox "Spin dial"
        
        'set shpDial = ActiveWindow.Selection.ShapeRange(1)
        'Set shpDial = ActivePresentation.Slides(LOCK1).Shapes.Range.Id
        'shpDial = ActivePresentation.Slides(LOCK1).Shapes
        
        i = CInt(shpDial.Name)
        MsgBox "shape name: " & i
        
        'D(i) = (D(i) + 1) Mod 10
        'MsgBox "D(" & CStr(i) & ") = " & CInt(D(i))
        
        'ActivePresentation.Slides(LOCK1).Shapes(shpDial.Name).TextFrame.TextRange.Text = D(i)
        'MsgBox "check combo..."
        
        'CheckCombo
        'MsgBox "returned from check combo"
    
    
    End Sub
    
    
    Sub Reset()
        Dim i As Integer
        For i = 0 To 3
            D(i) = 0
            ActivePresentation.Slides(LOCK1).Shapes(CStr(i)).TextFrame.TextRange.Text = D(i)
        Next i
        ActivePresentation.Slides(LOCK1).Shapes("Star").Fill.ForeColor.RGB = vbRed
    
    
    End Sub
    
    
    Function CheckCombo() As Boolean
        
        If D(0) = 8 And D(1) = 3 And D(2) = 6 And D(3) = 2 Then
            ActivePresentation.Slides(LOCK1).Shapes("Star").Fill.ForeColor.RGB = vbGreen
        Else
            ActivePresentation.Slides(LOCK1).Shapes("Star").Fill.ForeColor.RGB = vbRed
        End If

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    You can pass the clicked shape to the macro like this:

    Sub SpinDial(SpinShape As Shape) ' no spaces
        Dim i As Integer
        Dim Test As Boolean
        MsgBox "Triggered by: " _
        & SpinShape.Name & " Slide: " & SpinShape.Parent.SlideIndex
        End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    4
    Location

    Thanks John

    That works great! John, I see you name all over this forum - thank you for answering my question.

    What are the restrictions for the parameter list - can it only handle shape objects or can I hand in constants, or my own variables?

    Thanks again.

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    In this case (passing what was clicked in a show ) you can only pass shapes. If you call the macro in other ways (e.g. from another macro) you can pass any variable
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    4
    Location
    Ok thanks. Your awesome!

    Can you recommend any good books on this level/style of VBA programming for MS Powerpoint? or URLs? I have visited your site - has lots of good stuff.

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    There are very few books specifically for PowerPoint. David Marcovitz has one "Powerful PowerPoint for Educators" but it is fairly basic. The best way (I think) is to look at sites like ours, Steve Rindsberg's PPTFAQ and Shyam Pillai's site. They all have good examples you can take apart to see how they work.

    VBA Developers Handbook by Getz and Gilbert is nearly 20 years old and not just for PowerPoint. It's out of print but if you can find a cheap second hand copy on Amazon it it sometimes useful. Don't pay a lot though most that come up are expensive but there is sometimes a cheap copy some here right now.

    https://www.amazon.com/VBA-Developer.../dp/0782129781
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  7. #7
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    4
    Location

    Solved

    Thanks again John. I will check into what you have suggested. I appreciate the help.

    I can't find the 'Solved' button, so I placed it in the Title.

Tags for this Thread

Posting Permissions

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