Results 1 to 7 of 7

Thread: [RESOLVED] Clicking a shape via VBA

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Resolved [RESOLVED] Clicking a shape via VBA

    I have a worksheet that contains several shapes, and macros are assigned to each of them. Let's call one of those shapes shpGS and the macro it triggers GoodStuff. I use Application.Caller in GoodStuff to determine which macro to execute and when I manually click shpGS, GoodStuff is executed and it works great. However I'd like some way of clicking the shape programmatically. Calling GoodStuff doesn't work because Application.Caller does not get created unless the shape itself is clicked and so GoodStuff fails with an error.

    Is there any way to "click" shpGS programmatically?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: Clicking a shape via VBA

    Quote Originally Posted by MartinLiss View Post
    I have a worksheet that contains several shapes, and macros are assigned to each of them. Let's call one of those shapes shpGS and the macro it triggers GoodStuff. I use Application.Caller in GoodStuff to determine which macro to execute and when I manually click shpGS, GoodStuff is executed and it works great. However I'd like some way of clicking the shape programmatically. Calling GoodStuff doesn't work because Application.Caller does not get created unless the shape itself is clicked and so GoodStuff fails with an error.

    Is there any way to "click" shpGS programmatically?
    Seems like a bit of a Rube-Goldberg. Simulate a click so that an object is populated and a method is called and the value of the object is checked so that the appropriate macro is executed?

    Why not just directly execute the appropriate macro?

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Clicking a shape via VBA

    Why not just directly execute the appropriate macro?
    Because as I said, the macro uses Application.Caller to determine which shape is calling the macro and unless you manually click the shape (or hoping somehow simulate the manual click through VBA), the Application.Caller line errors out.

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: Clicking a shape via VBA

    Without seeing code, I stand by my suggestion. There's no reason to try to simulate a click of anything if you know in the end what code it is you want to execute. It may require moving some specific code into it's own routine if you have a bunch of code in a Click handler directly.

  5. #5

  6. #6
    Addicted Member
    Join Date
    May 2021
    Posts
    131

    Re: Clicking a shape via VBA

    If doing it programmatically, you could pass the shape name (or whatever) to the subroutine as a parameter. By making the parameter optional in the subroutine, you can still call it from clicking on shapes.
    Code:
    Option Explicit
    
    Sub AppCaller(Optional Param As Variant)
    
        On Error GoTo ErrHandler
    
        Dim ShapeSelection As String
        If IsMissing(Param) Then
            ShapeSelection = Application.Caller
        Else
            ShapeSelection = CStr(Param)
        End If
        
        Select Case ShapeSelection
            Case "shp1"
                DoSomething1
            Case "shp2"
                DoSomething2
            Case Else
                MsgBox ShapeSelection
        End Select
        
    ErrHandler:
        If Err.Number <> 0 Then MsgBox Err.Number & vbNewLine & Err.Description
        
    End Sub
    
    Sub DoSomething1()
        MsgBox "Do something one way"
    End Sub
    
    Sub DoSomething2()
        MsgBox "Do something another way"
    End Sub
    
    Sub Problem()
        AppCaller
    End Sub
    
    Sub OnePossibility()
        AppCaller "Pass the name of the shape as a parameter?"
    End Sub

  7. #7

Posting Permissions

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



Click Here to Expand Forum to Full Width