question on automating IE using mshtml and frame

H

holder

Folks, I have a simple web page with frames. I am trying to automate
internet explorer to display the page and allow access to its frames.

Here is the sample code :

using mshtml;
static void Main(string[] args)

{

SHDocVw.InternetExplorer explorer = null;

explorer = new InternetExplorer();


object nada = null;

explorer.Navigate("file://" + "c:\\temp\\xx" + "/frame.htm", ref nada, ref
nada, ref nada, ref nada);

explorer.Visible = true;

IWebBrowser2 browser;

browser = (IWebBrowser2)explorer;


IHTMLDocument2 doc = (IHTMLDocument2)browser.Document;

FramesCollection frames = (FramesCollection) doc.frames; // <<<======
exception

}

The browser launches and shows the page. Here is the web page:

<html>

<head>

<title>Title of page</title>

</head>

<frameset id="top" rows="25%,15%,15%,15%,15%,15%">

<frame id="a" >

<frame id="b" >

<frame id="c" >

<frame id="d" >

<frame id="e" >

<frame id="f" >

</frameset>

</html>

The frames field of the doc variable when accessed is throwing an invalid
cast exception.

Obviously, I have done something wrong. Can someone advise?

thanks,

bob
 
C

Chris Fulstow

Hi Bob,

You can get a reference to your frame window like this:

int frameIndex = 0;
indexObj = i; // item() param must be a reference to an object
mshtml.IHTMLWindow2 frame = (mshtml.IHTMLWindow2)doc.frames.item(ref
indexObj);

If you need to find a frame by its name, loop through frames and check
the frame.name property until it matches what you're looking for.
Unfortunately the frames collection isn't enumerable, so you'll have to
use a "for" loop and frames.length.

HTH,

Chris
 
H

holder

Hi Chris. Thanks for the reply. I found my problem. The application is a
console mode app and I "forgot" to mark the main thread with STAThread
attribute. Once I did that and wait for the doc load event the frames were
accessible with no exception.
Sorry for my late night flub.
bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top