[Tutor] closing a internet explorer com object

John Chandler oldmantaggie at gmail.com
Tue Jun 24 22:25:19 CEST 2008


Below is a bit of code that should work, you might want to change ieregex
because right now it will close anything that has "Microsoft Internet
Explorer" in the title bar. Have fun.

import win32con
import win32gui
import win32process
import re

def getHwnds():
    def callback(hwnd, hwnds):
        if win32gui.IsWindowVisible(hwnd) and
win32gui.IsWindowEnabled(hwnd):
            _, found_pid = win32process.GetWindowThreadProcessId(hwnd)
            hwnds.append(hwnd)
        return True

    hwnds = []
    win32gui.EnumWindows(callback, hwnds)
    return hwnds

ieregex = re.compile(".*Microsoft Internet Explorer.*")

for hwnd in getHwnds():
    name = win32gui.GetWindowText(hwnd)
    if ieregex.match(name):
        print hwnd
        win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)



On Tue, Jun 24, 2008 at 1:33 PM, Jeff Peery <jeffpeery at yahoo.com> wrote:

> hello,
> I'm using internet explorer to print out html documents and I'm not sure
> how to close it once it is created. How do I do this? below is the simple
> bit of code I use to print documents.
>
> thanks!
> Jeff
>
>         ie = win32com.client.Dispatch("InternetExplorer.Application")
>         ie.Visible = 0
>         ie.Navigate(doc_name)
>         if ie.Busy: sleep(1)
>         # print the current IE document without prompting
>         # the user for the printerdialog
>         ie.ExecWB(win32com.client.constants.OLECMDID_PRINT,
>           win32com.client.constants.OLECMDEXECOPT_DONTPROMPTUSER)
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
-John Chandler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080624/551e5ffc/attachment-0001.htm>


More information about the Tutor mailing list