Solved

Close specific IE window

Posted on 2006-06-12
11
443 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
ID: 16884495
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
ID: 16884521
SendMessage(WB.HWND, wm_Destroy, 0, 0);

that threw up an exception
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16890231
for k := 0 to ShellWindow.Count do  <-- I suppose it should be "to ShellWindow.Count - 1"
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Author Comment

by:wildzero
ID: 16890716
true, i changed it but still fails...
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16894210
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
 
LVL 26

Accepted Solution

by:
EddieShipman earned 250 total points
ID: 16896094
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
ID: 16897144
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
ID: 16910263
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
ID: 16911249
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
ID: 16914846
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
ID: 16925267
Thanks :D
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Using idhttp to login to instagram 2 90
Dynamically Created Query 3 55
update joined tables 2 46
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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