how can i press a command button through vba code

  • 12 years ago

     

    I want to simulate a mouse click over a command button through vba Word code . by using  two  API  windows : SendMessage and  SetCusorPos  I can solve this problem.  I  need some explanations  to use these API . Please Help

     

  • 12 years ago

     You can invoke the Button1_Click() event by calling the sub, but it has to be in the same module (ThisDocument object, for example):

    Private Sub CommandButton1_Click()
        MsgBox "Hey!"
    End Sub

    Public Sub test()
        Call CommandButton1_Click
    End Sub

     

    So in test() I just called the procedure and it's the same idea as pressing the button with the mouse. The only difference is the computer knows that the mouse didn't click the button. Hope this helps.

     

  • 12 years ago

     Thanks malick for your help

    I think I wasn't able to explain what I am seeking . I have a userform wich contain some controls including one command Button. My problem is  how can I use  the APIs SendMessage  and Mouse_event to visually  press a command Button. According to some tutorials these APIs can work together but  I don't know  how.

  • 12 years ago

    I see what you mean. I was worried that's what you meant =)

    This is increasingly proving me that this task is pretty difficult (especially since I never used those APIs).
    There's a really nice program at a website called All API:

    http://allapi.mentalis.org/apilist/apilist.php

    You can also find all the API calls on that site as well (be careful when using VBA and the examples found at that site, some things are different in VB6 versus VBA).

    Give me a little time and I'm sure I can get this working!
     

  • 12 years ago

     I'm going to bed now, and I won't be working on this for another 14 hours or so...but here's what I think we need to do:

    1. Get the handle of the CommandButton using the FindWindow and / or FindWindowEx APIs
    2. Use SendMessage to set the BM_SETSTATE of the button to TRUE for "highlighted" (pressed down, like a left click) and I think we also need to set it back to FALSE after a set time (we may need a timer API as well for that)


    Check out the info I found for BM_SETSTATE with SendMessage as msdn:
    http://msdn.microsoft.com/en-us/library/bb761823(VS.85).aspx

    I hope this can get you started. If you have never used APIs, then this is going to be a crazy ride for you. A real "crash course".

    Good luck and I'll check back soon. 

  • 12 years ago

     'Button1.PerformClick( )' method works same as someone click on the button in VB.Net

  • 12 years ago

    Hey shahidnoor86,

    heny is writing the code in VBA, not VB.NET (even though this is posted in the VB.NET forum) so .NET code won't work for them. Also, I mentioned doing the same thing by calling the _Click event of the button, but I believe heny is looking to "visually" see the button pressed using APIs.
     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson