Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to control another applications message box from VB6

Posted on 2004-04-30
10
Medium Priority
?
849 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

886 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