How to check if Com Object Already Exists

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Philbot44
Posts: 31
Joined: 25 Apr 2016, 08:21

How to check if Com Object Already Exists

03 Jul 2016, 02:11

Hi
Can anybody please help with this problem.

I am opening up a website using the code below but in some cases it may already be open and would like to know how to check 1st if open and if so use the one that is already open

When I run the code below it keeps opening new browser window?

Code: Select all

If !IsObject(ie)
	ie := ComObjCreate("InternetExplorer.Application")
	ie.Visible := True
	ie.Navigate("MyUrl")
IELoad(ie)

IELoad(ie)
{
	While !(ie.busy)
		Sleep, 100
	While (ie.busy)
		Sleep, 100
	While !(ie.document.Readystate = "Complete")
		Sleep, 100
}
ameyrick
Posts: 122
Joined: 20 Apr 2014, 18:12

Re: How to check if Com Object Already Exists

03 Jul 2016, 14:16

Code: Select all

title := "Google"
ie := IEGet(title) ;Retrieve pointer to existing IE window/tab

If !IsObject(ie)
	ie := ComObjCreate("InternetExplorer.Application")
	ie.Visible := True
	ie.Navigate("MyUrl")
IELoad(ie)
 
IELoad(ie)
{
	While !(ie.busy)
		Sleep, 100
	While (ie.busy)
		Sleep, 100
	While !(ie.document.Readystate = "Complete")
		Sleep, 100
}

IEGet(Name="")  ;Retrieve pointer to existing IE window/tab
{
IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
	Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
	: RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
For wb in ComObjCreate( "Shell.Application" ).Windows
	If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
		Return wb
	; Below added for when no tab name/page title provided ~ ameyrick
	If InStr( wb.FullName, "iexplore.exe" ) 
		Return wb
} ;written by Jethrow
Philbot44
Posts: 31
Joined: 25 Apr 2016, 08:21

Re: How to check if Com Object Already Exists

05 Jul 2016, 16:38

Thank you ameyrick that was really helpful and has done the trick
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

Re: How to check if Com Object Already Exists

12 May 2020, 11:22

I'm trying to learn from these codes. Would you mind explaining why there is both "While !(ie.busy)..." and "While (ie.busy)..."?
ameyrick wrote:
03 Jul 2016, 14:16

Code: Select all


IELoad(ie)
{
[b]	While !(ie.busy)
		Sleep, 100
	While (ie.busy)
		Sleep, 100[/b]
	While !(ie.document.Readystate = "Complete")
		Sleep, 100
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Hansielein, Lpanatt and 318 guests