Results 1 to 6 of 6

Thread: How to simulate a command button click in another workbook (resolved)

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7

    How to simulate a command button click in another workbook (resolved)

    Harlow,

    I have 2 workbooks and I want to simulate a click on one of the buttons in workbook A via some vba code in workbook B. How can I do that?

    Thanks in advance....
    Last edited by shaolang; Aug 2nd, 2004 at 02:39 AM.

  2. #2
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    workbook1.modulname.Button_onClick at a guess.
    ?
    'What's this bit for anyway?
    For Jono

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    That can't work, b'cos the buttons are drawn on a worksheet, not a userform. I can't modify the visibility of the button1_click() subroutine to public, which is kinda huge restrain.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    You need to use the Shapes collection for buttons.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    But how exactly do you do that? I could get hold of the command button via the Shapes collection, but how do I make the shape "click"? Calling
    Code:
    worksheets(1).Shapes("commandbutton1").click
    gives me a "method not found error".

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7

    How to simulate a command button click in another workbook (resolved)

    After a few days of searching, I've finally found the answer to the problem, and thought it might be useful to share in the forum.

    There isn't a convenient way to simulate a click on a button that resides in a different worksheet where the code is (the only way is to use the Win32api), but since clicking the button is the same as pressing spacebar on the an activated button, I could just use the SendKeys() function. The code might look like this:
    Code:
    ' code in worksheet B
    dim obj as OLEObject    ' will reference to a button in workbook A
    set obj = Workbooks("A").ActiveSheet.Shapes(1).OLEFormat.Object
    obj.Activate    ' must activate the button so SendKeys() will send the spacebar to the intended button
    SendKeys " "   ' sends a spacebar, to simulate a click

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