Simple Visual Basic Program

Posted on 2003-03-03
Medium Priority
Last Modified: 2013-11-25
how would i go about building a program that would detect a new window, delay about 5 seconds, then close it.

I would like for this program to exist in the background and be able to run for an extended amount of time.

Adam Miller
Question by:dullertap
LVL 28

Accepted Solution

vinnyd79 earned 300 total points
ID: 8061060
Is it a specific window you are trying to close? If you know the windows caption it can be done using the FindWindow API:

' add a timer and set the interval to 1000
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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
Private Const WM_CLOSE = &H10

Private Sub Timer1_Timer()
' where "Untitled - Notepad" is the window caption
CloseProgram ("Untitled - Notepad")
End Sub

Private Sub CloseProgram(ByVal Caption As String)
Dim Handle
Handle = FindWindow(vbNullString, Caption)
If Handle = 0 Then Exit Sub
' wait 5 secs
Sleep 5000
SendMessage Handle, WM_CLOSE, 0&, 0&
End Sub
LVL 101

Expert Comment

ID: 8065855
Why are you trying to write this?


Expert Comment

ID: 8940391
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
LVL 49

Expert Comment

ID: 9024078
Moderator, my recommended disposition is:

    Accept vinnyd79's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

609 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