Solved

How to control another applications message box from VB6

Posted on 2004-04-30
10
821 Views
Last Modified: 2013-12-25
I have a program that runs the ImportExportFavorites command of ShellUIHelper.  Natually this causes IE to ask "are you sure you wanna import your favs?" and "operation complete (click okay)".  Ideally i'd like to run ShellUIHelper with an option that tells it not to show these messages, but i didnt see that as an option.  So now the only thing i can think of is sendkeys "y" and "<enter>".  Problem is that since this is an import command, the time varies.  I could just loop for a while, or set a timer and send them- but that is sloppy.  I'm trying to find a way to search for the message box, but i have know idea what its name is.  It has no taskbar real estate and the name "Import Favorites" (which is shown ontop of the box) didnt work with appactivate.  My plan is to have this program run late at night when no one is using the computer; however i cant be sure that someone wont send an IM or something.  Please help.... running out of ideas.

e
0
Comment
Question by:irkgreen
  • 5
  • 2
10 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10960091
Have you already tried using FindWindow API to find the handle of the Window?
0
 

Author Comment

by:irkgreen
ID: 10960125
No... how can i do that?  Will it give me a unique name for that window?  Will i hafta run findwindow while the message is open?
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 250 total points
ID: 10960498
It will return the handle for the window,then you could use FindWindowEx to get the handle of the button that needs to be clicked.It would have to be running in the background.You could use a Timer with the interval set to 1000 and use something like this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Const BM_CLICK = &HF5

Private Sub Timer1_Timer()
Dim lWin As Long, Control As Long
' get handle to the window by finding caption
lWin = FindWindow(vbNullString, "The Windows Caption Goes Here")
If lWin <> 0 Then
    ' get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Ok")
    ' click the button
    SendMessage Control, BM_CLICK, 0, 0
End If

End Sub
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:irkgreen
ID: 10960841
I tried that and i think i am close.  I think "import favorites" is not the actual caption name though.  My code:

Again:
lWin = FindWindow(vbNullString, "Import Favorites")
If lWin <> 0 Then
    'get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Yes")
    'click the button
    SendMessage Control, BM_CLICK, 0, 0
Else
GoSub Again:
End If

"Import Favorites" is the tilte of the message box (yes/no) but perhaps not the caption.  Sicnce it doesnt have a taskbar slot i dont think finwindow is finding it. or perhaps it has a different name that i dont know about.  Any ideas???
0
 

Author Comment

by:irkgreen
ID: 10961854
i changed the lwin line to:

lWin = FindWindow("#32770", Null)

lWin = FindWindow("#32770", "Import Favorites")

I used a small app to determine the classname of the message box window and found it (and most messageboxes) to be named #32770.  A null for the caption is supposed to accept anything... but neither worked :(

not sure what to do.  I think that it is possible that the message box is actually part of my application and not an extension of Internet Explorer (which i assumed it was).  So i might rephrase my question to "How to control an application's message box from VB6"?
0
 

Author Comment

by:irkgreen
ID: 10962040
ok this actually made it into the IF satement:

Again:

lWin = FindWindow("#32770", vbNullString)
If lWin <> 0 Then
    MsgBox ""
    'get handle to Ok button
    Control = FindWindowEx(lWin, 0, "Button", "Okay")
    'click the button
    SendMessage Control, BM_CLICK, 0, 0
Else
GoSub Again:
End If

it seems the vbnullstring is the proper variable to use; however i have to click yes to import favorites and okay to dismiss the successful import before lwin<>0.  So it seems that perhaps the program is on hold until the user clicks an option.

Could there be a way to start a new sub that triggers when a message box appears?
0
 

Author Comment

by:irkgreen
ID: 10989492
I've solved my problem, sorta.  I realized that the message box was putting a stop on the thread and since VB6 only supports a single thread I had to go another route. Since i dont have VB.NET i had to compile a second exe to enumarate all of the windows searching for the message box and send a key to dismiss it.  The original exe simply called on the 2nd to do this.  Works fine.

I am not sure how or if i can close this thread.
0
 

Expert Comment

by:satishpreddy
ID: 11807338
general function, that will display message boxes with minimun width and height. If message exceeds minimum width and height, then it should resize and adjust to text.
 
And, for errors, show exlamation, for successful messages show question mark.
 
So, on a high level, the message box function shall look like,
 
displaymessage(tMessage as string, tMessageType, tMessageIcon)
 
Include parameters as required.
 
Thanks and regards

Sai Gopal
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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