Solved

Close specific IE window

Posted on 2006-06-12
11
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi...Split view - idea? 1 145
Delphi and Access based Enumeration 9 90
Tvertscrollbox like a whatsapp layout delete messages 1 50
Firemonkey Tcameracomponent resultion 1 46
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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