Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Closing IE windows from VBA

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Closing IE windows from VBA

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 284
Default Closing IE windows from VBA

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



"Sonnich Jensen" wrote in message
ups.com...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Closing IE windows from VBA

On Oct 2, 8:00 pm, "Steve Yandl" wrote:
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
closing file with multiple windows open gvanhoy3 Excel Worksheet Functions 1 November 15th 06 06:31 PM
Warning the user about closing Windows:1. Walden2 Excel Programming 1 November 15th 06 06:33 AM
Closing Windows Sameer Excel Discussion (Misc queries) 3 October 26th 06 10:48 AM
Prevent open excel windows from automatically closing. Prevent Automatic Closing Excel Discussion (Misc queries) 1 April 10th 06 10:27 PM
closing active windows without saving changes Tony Excel Programming 2 March 26th 05 10:24 PM


All times are GMT +1. The time now is 01:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"