Auto Press OK on Msgbox

Posted on 2006-04-26
Last Modified: 2013-12-25

I have an issue where I am controlling another application with the Function SendMessage.  The problem is when  I press one of the buttons on this other application it gives me an OK message box.  

How can I auto click this 'OK' button on an message box.


Question by:Lucree
    1 Comment
    LVL 17

    Accepted Solution

    1 Timer
    2 Buttons
    1 Label
    1 Textbox ( MultiLine = True )


    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long

    Private Sub Command1_Click()
    Timer1.Enabled = False: MsgBox "You clicked 'Yes'"
    End Sub

    Private Sub Command2_Click()
    Timer1.Enabled = False: MsgBox "You clicked 'No'"
    End Sub

    Private Sub Timer1_Timer()
        Timer1.Enabled = False
    End Sub

    Private Sub Form_Load()
    Timer1.Interval = 4000
    Command1.Caption = "Yes"
    Command2.Caption = "No"
    Command1.Move 720, 840, 1095, 375
    Command2.Move 2280, 840, 1095, 375
    Me.Width = 4245: Me.Height = 1920
    Label1.Move 720, 360, 2895, 615
    Text1.Move 720, 360, 2895, 615
    Text1.Visible = False

    Caption = "Registration"
    zz = zz & "This Program is not registered."
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & vbCrLf
    zz = zz & "Would you like to register it now?"

    Text1 = zz ' Text1 only needed to measure the label1 height.
    Label1 = zz
    End Sub

    Private Sub myHeight() 'calculate the height of Label1 (for long text, the label must be higher)
        RowsT1 = SendMessage(Text1.hWnd, &HBA, 0, 0) 'how many lines has text1 ?
        Label1.Height = 360 + (195 * (RowsT1 - 1)) 'calculate the height for label1
        Form1.Height = 1995 + (195 * (RowsT1 - 1))
        Command1.Top = Me.Height - 1080
        Command2.Top = Me.Height - 1080
    Me.Move Screen.Width / 2 - Width / 2, Screen.Height / 2 - Height / 2 'display msgbox to center screen
    End Sub


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now