PDA

View Full Version : Find active IE tab



theta
03-06-2012, 07:18 AM
Hey all...coming back for some expert advice as I am a bit stuck. After using the KB and other posts I have constructed a simple macro to start an IE instance and navigate to a page, fire some elements and execute a javascript - all the way through to the production of a crystal report.

I now have a small problem - there is session control in place on the .aspx pages I was using. This is not an issue if doing it manually...log in to the sign in page, open a new IE tab and navigate directly to the Reports.aspx page and the session is valid.

I basically need a VBA macro to scroll through any open instances of InternetExplorer and find which one contains a valid session. This can be detected by seeing if the address is "fcss.xxxxxxx.net" - as this would indicate a valid session is in place.

So if I had 2 instances of IE open, each with 3 tabs - I would need it to scroll through all 3 tabs on instance 1, if no live session is found then move on to instance 2 and scroll through the tabs on there. If found, open a new tab and navigate to reports.aspx. If no live session is found, start a new instance of IE all together and my existing script will run...

Any help appreciated :)

theta
03-06-2012, 07:20 AM
Other option (prob more reliable than using the address method) is to check within each tab if there is an element called name="__VIEWSTATE" although I would like the option to try both methods and see what works... :)

theta
03-07-2012, 06:16 AM
Anyone.... :/

The logic is simple, but I do not have the IE object knowledge.

Scroll through each open tab of each IE instance until you find a page with the matching URL, or that contains the object _VIEWSTATE.

If found, open a new tab in this instance of IE.

If not found, open a new instance of IE and then 'do something'

mohanvijay
03-07-2012, 06:31 AM
Try this to loop through IE tabs



Dim IE_Tab As SHDocVw.InternetExplorer
Dim SH_Win As SHDocVw.ShellWindows
Dim T_Str As String
Set SH_Win = New SHDocVw.ShellWindows
For Each IE_Tab In SH_Win
T_Str = IE_Tab.LocationURL

If T_Str = "your criteria" Then
'do your stuff
Else
' do something
End If

Next

Set IE_Tab = Nothing
Set SH_Win = Nothing

theta
03-07-2012, 06:38 AM
Hi - thanks for the quick reply.

Can you explain the logic behind this please? I have googled SHDocVw and can see that is is a DLL (Shell Document View).

So SHDocVw.InternerExplorer treats each Tab as an object, and ShellWindows is recognised as being an IE instance (or will this look through all objects that are classed as ShellWindows?)

Any help appreciated...

(Also - is there a pure VBA approach with object - using late binding for my understanding - or is SHDocVw the only way?)

mohanvijay
03-07-2012, 07:01 AM
shellwindows is the collection of the all opened explorer windows

logic is get the all opened explorer windows and loop through them and get the IE object based on our criteria

theta
03-07-2012, 07:06 AM
Perfect - thanks for confirming.

How could I take it a step further. Instead of looking at the URL, could I look at the IE.Document and see if it contains the element name="_VIEWSTATE"

By combining this with the URL I could be sure the user is in fact logged in so I can run reports.

I then need to build a timer - say 10 seconds - that will be initialised every time I sent a request (e.g. .FireEvent("onchange") so that if nothing happens and the readystate is not reached, the macro will terminate. Do not want to be stuck in an infinite loop while I wait for a command that can never complete :/

mohanvijay
03-07-2012, 07:18 AM
Try this to get document object



Dim HTML_Doc As MSHTML.HTMLDocument

Set HTML_Doc = IE_Tab.Document

nhmabv
11-20-2012, 04:54 PM
It doesnt work----"User type not defined" on Dim HTML_Doc As MSHTML.HTMLDocument