Discussion:
TWebBrowser load from string
(too old to reply)
Didier Cabalé
2003-12-01 17:11:54 UTC
Permalink
Hello,

is there a way to use the TWebBrowser for viewing html strings coming from
s: string?
Thank's for your help
Didier
eshipman
2003-12-01 17:51:07 UTC
Permalink
In article <***@newsgroups.borland.com>, ***@free.fr
says...
Post by Didier Cabalé
Hello,
is there a way to use the TWebBrowser for viewing html strings coming from
s: string?
Thank's for your help
Didier
http://members.shaw.ca/iedelphi/webbrowser.htm#advanced4
Didier Cabalé
2003-12-02 19:18:08 UTC
Permalink
1. Thank's eshipman
2. i just put a TWebBrowser on my Form, then i run (on a event handler):
var
HTMLDoc: IHTMLDocument2;
begin
HTMLDoc := WebBrowser1.Document as IHTMLDocument2;
end;

* then HTMLDoc = nil *

Does anybody know why?
Thank's for your help
Didier
Post by eshipman
says...
Post by Didier Cabalé
Hello,
is there a way to use the TWebBrowser for viewing html strings coming from
s: string?
Thank's for your help
Didier
http://members.shaw.ca/iedelphi/webbrowser.htm#advanced4
eshipman
2003-12-02 23:43:09 UTC
Permalink
In article <3fcce5dc$***@newsgroups.borland.com>, ***@free.fr
says...
Post by Didier Cabalé
1. Thank's eshipman
var
HTMLDoc: IHTMLDocument2;
begin
HTMLDoc := WebBrowser1.Document as IHTMLDocument2;
end;
* then HTMLDoc = nil *
Does anybody know why?
Did you navigate to a page before checking? Usually, I navigate
to about:blanka nd it works.
Didier Cabalé
2003-12-03 08:52:55 UTC
Permalink
Post by eshipman
Did you navigate to a page before checking? Usually, I navigate
to about:blanka nd it works.
You are right.
Thus it seems one need to navigate to 'about:blank' before this
'LoadFromString' like procedure, but without including the 'navigate' in the
'LoadFromString'. If i include the 'navigate' in the 'LoadFromString'
procedure, the 'LoadFromString' fails.
Thus is there a way to initialize the document first in the 'LoadFromString'
procedure?

Thank's for your help
Didier
eshipman
2003-12-03 15:02:38 UTC
Permalink
In article <***@newsgroups.borland.com>, ***@free.fr
says...
Post by Didier Cabalé
Post by eshipman
Did you navigate to a page before checking? Usually, I navigate
to about:blanka nd it works.
You are right.
Thus it seems one need to navigate to 'about:blank' before this
'LoadFromString' like procedure, but without including the 'navigate' in the
'LoadFromString'. If i include the 'navigate' in the 'LoadFromString'
procedure, the 'LoadFromString' fails.
Thus is there a way to initialize the document first in the 'LoadFromString'
procedure?
Do it in FormShow, instead, the WebBorwse will look the same to your
user.
Didier Cabalé
2003-12-03 16:58:19 UTC
Permalink
But the problem still remains: i have to put the 'navigate' and the
'LoadFromString' procedures on 2 different event handlers. And the problem
is serious if i put the 'LoadFromString' in the TForm.OnCreate. Do you
agree? If yes, is there a way to initialize the document in the same
procedure?
Thank's
Didier
Post by eshipman
says...
Post by Didier Cabalé
Post by eshipman
Did you navigate to a page before checking? Usually, I navigate
to about:blanka nd it works.
You are right.
Thus it seems one need to navigate to 'about:blank' before this
'LoadFromString' like procedure, but without including the 'navigate' in the
'LoadFromString'. If i include the 'navigate' in the 'LoadFromString'
procedure, the 'LoadFromString' fails.
Thus is there a way to initialize the document first in the
'LoadFromString'
Post by eshipman
Post by Didier Cabalé
procedure?
Do it in FormShow, instead, the WebBorwse will look the same to your
user.
eshipman
2003-12-03 17:40:59 UTC
Permalink
In article <3fce1699$***@newsgroups.borland.com>, ***@free.fr
says...
Post by Didier Cabalé
But the problem still remains: i have to put the 'navigate' and the
'LoadFromString' procedures on 2 different event handlers. And the problem
is serious if i put the 'LoadFromString' in the TForm.OnCreate. Do you
agree? If yes, is there a way to initialize the document in the same
procedure?
Thank's
Didier
Post by eshipman
says...
Post by Didier Cabalé
Post by eshipman
Did you navigate to a page before checking? Usually, I navigate
to about:blanka nd it works.
You are right.
Thus it seems one need to navigate to 'about:blank' before this
'LoadFromString' like procedure, but without including the 'navigate' in
the
Post by eshipman
Post by Didier Cabalé
'LoadFromString'. If i include the 'navigate' in the 'LoadFromString'
procedure, the 'LoadFromString' fails.
Thus is there a way to initialize the document first in the
'LoadFromString'
Post by eshipman
Post by Didier Cabalé
procedure?
Do it in FormShow, instead, the WebBorwse will look the same to your
user.
I just realize why it is happening. The WebBrowser's document isn't yet
loaded when you Navigate and then call
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;

What you should do is place the LoadString in the OnDocumentComplete
event of the WebBrowser.

This is the only way I know of doing it in one procedure:

procedure TForm1.Button1Click(Sender: TObject);
var
v: Variant;
HTMLDocument: IHTMLDocument2;
HTMLString: String;
begin
HTMLString := '<html><body><font color="red">String Loaded<br>into' +
'TWebBrowser</font></body></html>';
Webbrowser1.Navigate('about::blank');
// Wait for 2 seconds for the Document to get loaded.
// May need to increase/decrease this value for optimal
// results
sleep(2000);
// Now it should be ready.
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
v := VarArrayCreate([0, 0], varVariant);
v[0] := HTMLString; // Here's your HTML string
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;
Didier Cabalé
2003-12-03 18:03:18 UTC
Permalink
Hi eshipman,
Post by eshipman
What you should do is place the LoadString in the OnDocumentComplete
event of the WebBrowser.<<

That works actually.
But the following one does not.
It helps me anyway to understand that the TWebBrowser.document is not so
easy to initialize.
Thank's for your assistance!
Didier

procedure TForm1.Button1Click(Sender: TObject);
var
v: Variant;
HTMLDocument: IHTMLDocument2;
HTMLString: String;
begin
HTMLString := '<html><body><font color="red">String Loaded<br>into' +
'TWebBrowser</font></body></html>';
Webbrowser1.Navigate('about::blank');
// Wait for 2 seconds for the Document to get loaded.
// May need to increase/decrease this value for optimal
// results
sleep(2000);
// Now it should be ready.
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
v := VarArrayCreate([0, 0], varVariant);
v[0] := HTMLString; // Here's your HTML string
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;
eshipman
2003-12-03 18:23:42 UTC
Permalink
In article <***@newsgroups.borland.com>, ***@free.fr
says...
Post by Didier Cabalé
Hi eshipman,
Post by eshipman
What you should do is place the LoadString in the OnDocumentComplete
event of the WebBrowser.<<
That works actually.
But the following one does not.
It helps me anyway to understand that the TWebBrowser.document is not so
easy to initialize.
Thank's for your assistance!
Didier
It worked for me that's why I posted the code.

Loading...