Closing IE windows from VBA

S

Sonnich Jensen

Hi

accroding to my post from yesterday, I found this....

It should close all IE windows, and is close to what I tried.

The problem is SendMessage HWnd, WM_CLOSE, 0&, 0&
It gives a ding (like for a confirmation or warning), I guess that
something blocks it, so it is not allowed to close.

I tried other messages, WM_DESTROY and WM_QUIT, same result... or
actually none, they do not even give a ding.

Why can I not close that window?
I should mention, that I create it myself using shellexecute and wait
7 secs. So the window is open and ready.


WBR
Sonnich



Public Declare Function FindWindow Lib "user32" Alias
"FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias
"SendMessageA" ( _
ByVal HWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Const WM_CLOSE As Long = &H10
Public Const IE_WINDOW_CLASS = "IEFrame"

Dim HWnd As Long
HWnd = FindWindow(IE_WINDOW_CLASS, vbNullString)
Do Until HWnd = 0
SendMessage HWnd, WM_CLOSE, 0&, 0&
HWnd = FindWindow(IE_WINDOW_CLASS, vbNullString)
Loop
 
S

Sonnich Jensen

I got on with this, and found:

The problem with the code before, is that WM_CLOSE does not work for
IE. Dont know why, just is so.
Found this, which works in Delphi, so I portaged it.

My problem is that "f = Process32First(hSnap, proc)" is 0, while in
Delphi: "if Process32First(processSnapshot, processEntry) then"
works. To me it should be the same. Large parts of the VB code is
found here, as the declarations (easier that way). Still, something
goes wrong... what?

The delphi code is
processSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
processEntry.dwSize := SizeOf(processEntry);
if Process32First(processSnapshot, processEntry) then
etc... btw this works...


Which translates into:

Dim f As Long, sname As String
Dim hSnap As Long, proc As PROCESSENTRY32
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnap <> hNull Then
proc.dwSize = Len(proc)

f = Process32First(hSnap, proc)
Do While f ' <- alway false
sname = StrZToStr(proc.szExeFile)
If sname = "iexplore.exe" Then

ProcessHandle = OpenProcess(PROCESS_TERMINATE, 0,
proc.th32ProcessID)
Call TerminateProcess(ProcessHandle, 0)

End If
f = Process32Next(hSnap, proc)
Loop
'CloseHandle hSnap
End If


Remember to add:
Public Declare Function Process32First Lib "kernel32" ( _
ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" ( _
ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "Kernel32.dll" ()
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Public Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long

Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long ' This process
th32DefaultHeapID As Long
th32ModuleID As Long ' Associated exe
cntThreads As Long
th32ParentProcessID As Long ' This process's parent process
pcPriClassBase As Long ' Base priority of process threads
dwFlags As Long
szExeFile As String * 260 ' MAX_PATH
End Type

Function StrZToStr(s As String) As String
StrZToStr = Left$(s, Len(s) - 1)
End Function
 
S

Steve Yandl

Sonnich,

If none of the PCs you want to run the code on are Win98 or earlier, you can
use WMI to kill all open instances of Internet Explorer. This isn't a
terribly delicate way to close windows and may annoy users who have
instances of IE already running but it does work.

_____________________________

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'IEXPLORE.EXE'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
_____________________________

Is there a reason you're using shellexecute to open IE rather than create an
instance of the object "InternetExplorer.Application" which would give you
much greater control over the IE window?

Steve
 
S

Sonnich Jensen

Sonnich,

If none of the PCs you want to run the code on are Win98 or earlier, you can
use WMI to kill all open instances of Internet Explorer. This isn't a
terribly delicate way to close windows and may annoy users who have
instances of IE already running but it does work.

_____________________________

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'IEXPLORE.EXE'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next


It does not like the line "objProcess.Terminate()" by some reason.

I am still bulling around the the code above , wondering why f =
Process32First(hSnap, proc) is always false...

wBR
Sonnich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top