Solved

Close specific IE window

Posted on 2006-06-12
11
431 Views
Last Modified: 2010-04-05
Hi there

I am using this code to close a IE window by address

Procedure QuitBrowser( sUrl : string );
var
 ShellWindow: IShellWindows;
 WB: IWebbrowser2;
 spDisp: IDispatch;
 IDoc1: IHTMLDocument2;
 k: Integer;
begin
 ShellWindow := CoShellWindows.Create;
 // get the running instance of Internet Explorer
 for k := 0 to ShellWindow.Count do
 begin
   spDisp := ShellWindow.Item(k);
   if spDisp = nil then Continue;
   // QueryInterface determines if an interface can be used with an object
   spDisp.QueryInterface(iWebBrowser2, WB);
   if WB <> nil then
   begin

     WB.Document.QueryInterface(IHTMLDocument2, iDoc1);

     if iDoc1 <> nil then
     begin
       WB := ShellWindow.Item(k) as IWebbrowser2;
       If sUrl = WB.LocationURL then
     //  WB.Quit;  {method 1}
       PostMessage (WB.HWND, WM_CLOSE, 0, 0);

       end;
     end;
   end;
end;


As you can see I have tried WB.Quit and PostMessage (WB.HWND, WM_CLOSE, 0, 0);

Both of these seem to work AOK on my Window Xp home computer, however, I get an error
Access is denied when I run this on my windows 2000 computer.... I got no idea!

Any suggestions?
0
Comment
Question by:wildzero
  • 7
  • 3
11 Comments
 
LVL 10

Author Comment

by:wildzero
Comment Utility
Also tried

    SendMessage(WB.HWND,WM_DESTROY,0,0);
    SendMessage(WB.HWND,WM_NCDESTROY,0,0);

but no lock there either
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
SendMessage(WB.HWND, wm_Destroy, 0, 0);

that threw up an exception
0
 
LVL 11

Expert Comment

by:ZhaawZ
Comment Utility
for k := 0 to ShellWindow.Count do  <-- I suppose it should be "to ShellWindow.Count - 1"
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
true, i changed it but still fails...
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
WB.Quit worked fine for me but I am using XP, too.
When my buddy comes online later I'll get him to try on Win2K.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Accepted Solution

by:
EddieShipman earned 250 total points
Comment Utility
Her just tried it and it worked for him, too.

did you try it this way:

var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  k: Integer;
  sURL: String;
begin
  ShellWindow := CoShellWindows.Create;
  for k := 0 to ShellWindow.Count-1 do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then
      Continue;
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        // MUST strip the protocol (http://) and the
        // path off the URL, first
        if iDoc1.domain = sURL then
          WB.Quit;  {method 1}
      end;
    end; {if WB}
  end; {for}
end;
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
Cool I will give that a go
Maybe my Windows2000 has had something to stop IE windows being closed by another application.
Ah well
will keep you informed.
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
Hmm tired that and still got an exception error.
Herm
Works on on my Windows XP machine but not my 2000 Pro machine.
:-/
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
Could be permissions issue, otherwise I have no idea.
Did you try running it under a user with Admin rights?
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
The user I run it under is the only user on the computer ;-)
The windows 2000 install is well comming up to 5 years old so I bet the system is a little messed up (still runs rock solid though, after 3 hardware upgrades) - to bad windows xp isn't as stable hehe.

Might see if I can find some other people out there running 2000 and see if they can test it for me.

Thanks!
0
 
LVL 10

Author Comment

by:wildzero
Comment Utility
Thanks :D
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now