'添加控件 Text1 Text2 Command1 Option Explicit Option Compare Text 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 Const WM_CLOSE = &H10 '关闭窗体常量 Dim MsgForm As Long Private Sub Form_Load() Text1.Text = "Microsoft Internet Explorer" '一般IE的JS脚本消息框都是这个标题 Text2.Text = "2000" '每两秒执行一次 End Sub Private Sub Command1_Click() If Text1.Text = "" Then MsgBox "请输入窗体名", 0, "提示" If Timer1.Enabled = False Then Command1.Caption = "开启" Text1.Enabled = False Text2.Enabled = False Timer1.Interval = Val(Text2.Text) Timer1.Enabled = True Else Command1.Caption = "停止" Text1.Enabled = True Text2.Enabled = True Timer1.Interval = Val(Text2.Text) Timer1.Enabled = False End If End Sub Private Sub Timer1_Timer() MsgForm = FindWindow(vbNullString, Text1.Text) '查找窗体标题 If MsgForm > 0 Then Call SendMessage(MsgForm, WM_CLOSE, 0, ByVal 0&) '关闭此窗体 End If End Sub