4

I want to know how to get TOTAL height of html document loaded into TWebBrowser component (Delphi)?

I have found something like this and it is not working:

webbrowser.oleobject.document.body.scrollheight

I placed it inside OnDocumentComplete event.

I need height because I am calculating PageSize property of ScrollBar (my custom scrollbar - build-in WebBrowser is disabled) which depends on web page height.

Thanks for any feedback, best regards

  • please define `"it is not working"`. – kobik Jan 28 '15 at 13:20
  • Take a look at my comment: http://stackoverflow.com/questions/28187441/total-height-of-html-document-loaded-into-twebbrowser?noredirect=1#comment44743633_28187583 The same I get with my version (from my question) of code – kuta_senator Jan 28 '15 at 13:25

1 Answers1

2

Something like this should work:

uses MSHTML;

var
  HtmlElement: IHTMLElement2;
  PageHeight: Integer;

begin
  with MyWebBrowser.ControlInterface do
  begin
    HtmlElement := (Document as IHTMLDocument3).documentElement as IHTMLElement2;
  end;

  PageHeight := HtmlElement.scrollHeight;
end;

This is the full height. The body element seems to give a bit smaller value (probably thanks to margins):

var
  BodyElement: IHTMLElement2;
  PageHeight: Integer;

begin
  with MyWebBrowser.ControlInterface do
  begin
    BodyElement := (Document as IHTMLDocument2).body as IHTMLElement2;
  end;

  PageHeight := BodyElement.scrollHeight;
end;
manlio
  • 18,345
  • 14
  • 76
  • 126
  • Strange thing is that is gaves me const value (int) every time I load a new page (I know it has different height because of content), now it gives me integer 1633572 (where browser height is something like 600px, and document is a little bit higher - sth like 650-700px). I have webbrowser placed on panel, and this panel is placed on the second higher panel2. Don't have any idea why it is not working... – kuta_senator Jan 28 '15 at 08:31
  • Hmmm, looks like second option it is working, but as you said height of body is a bit smaller then documentElement, but documentElement gives me wrong height value (const every time). – kuta_senator Jan 28 '15 at 13:28