mshtml - access denied for Writeln()

  • Thread starter Thread starter sam
  • Start date Start date
S

sam

Hi

Just encountered a strange problem and wonder if it is a
bug ? I have the following piece of code....

try
{
IHTMLDocument2 MyDoc = (IHTMLDocument2
MyWebBrowser.Document;
IHTMLWindow2 MyWin = (IHTMLWindow2 ) MyDoc.open
("","_TEST_","","");

MyWin.document.writeln("line #1");
MyWin.document.writeln("line #2");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}

This opens the new WebBrowser, and writes the "line #1"
into it. But while writing the second line it throws an
exception "access denied"
Seems like I am allowed to access the "document" property
of the "IHTMLWindow2" only once.
If I try to access it again, I get this exception.

I found a work around.....
Basically I extract the MyWin.document into a variable of
IHTMLDocument2 and then invoke the writeln().
try
{
IHTMLDocument2 MyDoc = (IHTMLDocument2)
IWeb2.Document;
IHTMLWindow2 MyWin = (IHTMLWindow2 ) MyDoc.open
("","_TEST_","","");
IHTMLDocument2 MyDoc2 = (IHTMLDocument2)
MyWin.document;

MyDoc2.writeln("line #1");
MyDoc2.writeln("line #2");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}


This works, but it is not foolproof. if I try to access
MyWin.document again at some other time... eg when I
invoke the function again, I still get the exception.

Anybody any clues?

TIA
sam
 
Hi

Just encountered a strange problem and wonder if it is a
bug ? I have the following piece of code....
[...]

This works, but it is not foolproof. if I try to access
MyWin.document again at some other time... eg when I
invoke the function again, I still get the exception.

Anybody any clues?

TIA
sam

Hi Sam,

You have to Wait for the document to load, to be able to write to it.
There's a OnLoadFinished Event on the IHTMLDocument. You should connect to
the event and only start your writing when this event ahs been fired.

Greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
 
im not sure without opening and testing it but its possible its because your
not setting the designMode property...

MyWin.designMode = "On";
 
Hi Peter,
You have to Wait for the document to load, to be able to
write to it.

I dont think it is a load problem because if I do the
following then it works like a charm...
MyDoc2 = (IHTMLDocument2)MyWin.document;
MyDoc2.writeln("line #1");
MyDoc2.writeln("line #2");

The problem is if I EVER again try to access
MyWin.document, it gives me the "access denied" exception.

TIA
sam
 
Back
Top