Solved

Move the document from opened IE to a WebBrowser

Posted on 2004-10-19
17
367 Views
Last Modified: 2010-04-05
How to read into my WebBrowser a document already opened into an IE sesion ? I don't wish just to read the address from IE and to navigate to in WB. I intend to get instantly the document from IE ( cache ?? ) and displayed to my WB.

Codes please!
0
Comment
Question by:ginsonic
  • 9
  • 8
17 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12358438
This will get the page into your webbbrowser but any relative links will be
obliterated because the document.location is now "about:blank":

procedure TForm1.Button1Click
  ( ASender:                   TObject
  );
var
  ShellWin : TShellWindows;
  i : integer;
  IE : IWebBrowser2;
  AStream:TMemoryStream;
begin
  WebBrowser1.Navigate('about:blank');
  ShellWin := TShellWindows.Create(nil);
  try
    for i := 0 to ShellWin.Count-1 do
      begin
        try
          ShellWin.Item(i).QueryInterface(IID_IWebBrowser2, IE);
          AStream:=TMemoryStream.Create;
          try
            (IE.Document as IPersistStreamInit).Save(TStreamAdapter.Create(AStream), False);
             AStream.Seek(0,soFromBeginning);
          finally
            (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(AStream));
            AStream.Free;
          end;
        except
        end;
      end;
  finally
    ShellWin.Free;
  end;
end;

What you really need is to save as MHT and load . See my tip here:
http://www.delphipages.com/tips/thread.cfm?ID=209

Does a full save as in IE and then loads the MHT in the AViewer:

procedure SaveWholePage
 ( AURL:               String
 ; AFileName:       TFileName
 ; AView:              Boolean
 ; AViewer:           TWebBrowser
 );

usage, in above context:

procedure TForm1.Button1Click
  ( ASender:                   TObject
  );
var
  ShellWin : TShellWindows;
  i : integer;
  IE : IWebBrowser2;
begin
  ShellWin := TShellWindows.Create(nil);
  try
    for i := 0 to ShellWin.Count-1 do
      begin
        try
          ShellWin.Item(i).QueryInterface(IID_IWebBrowser2, IE);
          SaveWholePage(IE.LocationURL, 'c:\temp\mysite.mht', True, WebBrowser1);
        except
        end;
      end;
  finally
    ShellWin.Free;
  end;
end;
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12382300
I get next error for code 1:
[Error] Unit1.pas(43): Undeclared identifier: 'IPersistStreamInit'
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12382358
For code 2 don't have ADODB_TLB, CDO_TLB on my system
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12382597
IPersisStreamInit is in ActiveX.pas, place ActiveX in your uses.

For ADO_TLB and CDO_TLB, you must import their type libraries.
If you don't have The DLL for CDO to import if it isn't in the list is:
C:\Program Files\Common Files\Microsoft Shared\CDO\CDOEX.DLL
If you don't have Outlook/Exchange installed, you're out of luck.

ADO (if you don't have it in the list: C:\Program Files\Common Files\System\ado\msadox.dll)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12659822
ginsonic, you gonna clean this one up? If so, could you wait until after 12/1 to do it?
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12662100
MHT isn't what I need. I have already some code for this. The problem is that lose the images and I don't wish to spend time and space to save to a file and after that to load it. I try to find a way to get from memory the page. The page is already on cache and memory.

Hope to find a way to get instantly the page.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12665931
My first example does that. It loads the current page in IE into a TWebBrowser. Did you try it?
Beware that loading any cached pages will retrieve images, scripts, stylesheets, etc from the URL
NOT from cache.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12666007
Like I said in the other post, though, if the site has relative urls for images, such as 'src="images/m.gif"' then
it won't work. They must be fully qualified urls. That being said, it will also load the images, scripts, etc.
from the web instead of from the cache.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:ginsonic
ID: 12672020
Not usefuly :(. My real reason for this topic is that I need to get a thumbnail for my opened IE session. Because I can't find a way I thinking to move the document to a WEBBrowser and to snap the IE document into a jpeg. Something alike WebBrowser1,Document.Assign(IE.Document);  ?!?!?!?

P.S. Can't get a snap of desktop because I'm interested to capture when the IE is hidden or minimized, too.
P.S.S. Still don't understand why I can snap On TWEBBrowser and from an IE session can't :P
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12696346
No need to do that. Search this forum for Explorer and IViewObject and you'll find examples on how to take a thumbnail snapshot of IE's window.

Well, forgive me, you already posted an Assisted answer to this question that should answer YOUR own question:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20698124.html

All you have to do is to replace this webBrowser1.Document with the Document of the IE instance you want to shoot.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12696350
I showed you how to do that in example 1 above.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12703437
I try already to do that but look like WebBrowser IViewObject isn't the same with IE document IViewObject. If you can give me a code that will  snapshot the IE session I will offer this question and a new 500 points for snap topic.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12850600
Any comment?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12851633
been too busy, will revisit sometime today.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12853392
OK, still can wait :) Thanks for support!
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 12854701
OK, here's what I came up with...

After looking a little more closely, I found a post on the Borland NG's that stated:
"According to the Microsoft documentation, this only works if you are using in-process
automation."

Now, that could be found nowhere in the docs but I couldn't find ANYTHING to make it work
across process bourndaries so...that being said. I decided to use a "hidden" TWebBrowser to
do the work for me. I copied some code from a CodeCentral Post by zam juhara:
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=21429 and
tested it on several sites. Worked like a charm...Now you are wondering, why did I have to
NAVIGATE to the site using the TWebBrowser. Well, it has to do with realative .vs absolute
URL's in the web site's HTML source. If you persist the website being viewd in IE to a Stream
and then assign it to the WebBrowser, all realtive links get a document location of about:blank
and any images with a realtive URL will not show up.

I know this isn't the perfect solution but at least it works...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, MSHTML, ActiveX, ShDocVW, ExtCtrls, StdCtrls, COMObj,
  OleCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
    TargetBitmap:TBitmap;
    ImgTop: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  ShellWin : TShellWindows;
  i : integer;
  IE : IWebBrowser2;
begin
  WebBrowser1.Navigate('about:blank');
  ShellWin := TShellWindows.Create(nil);
  try
    ShellWin.Item(0).QueryInterface(IID_IWebBrowser2, IE);
    try
    finally
      WebBrowser1.Navigate(IE.LocationURL);
    end;
  finally
    ShellWin.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ImgTop := Image1.Top;
  TargetBitmap:=TBitmap.Create;
  //set web browser full screen size. Weird browser huh? that's why we set it to invisible
  //we want to capture entire html page
  WebBrowser1.ClientWidth:=Screen.Width;
  WebBrowser1.ClientHeight:=Screen.Height;
  //we hide it. I don't know why set its Visible property to false did not hide it
  WebBrowser1.Left:=-WebBrowser1.Width;
  WebBrowser1.Top:=-WebBrowser1.Height;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  viewObject:IViewObject;
  arect:TRect;
  navComplete:boolean;
begin
  navComplete:=true;
//we check that document is really loaded
//by checking navComplete because OnDocumentComplete
//may occur even if browser did not succesfully navigate to site
  if (navComplete) and (WebBrowser1.Document<>nil) then
  begin
//query interface for IViewObject
    WebBrowser1.Document.QueryInterface(IViewObject,viewObject);
    if (ViewObject<>nil) then
    begin
//hide scroll bar of browser
      Webbrowser1.OleObject.Document.Body.Style.OverflowX:='hidden';
      Webbrowser1.OleObject.Document.Body.Style.OverflowY:='hidden';
//set size of webbrowser as big as actual html size because
//we want to capture ALL html
      if WebBrowser1.ClientWidth<Webbrowser1.OleObject.Document.Body.ScrollWidth then
        WebBrowser1.ClientWidth:=Webbrowser1.OleObject.Document.Body.ScrollWidth;
      if WebBrowser1.ClientHeight<Webbrowser1.OleObject.Document.Body.ScrollHeight then
//4 is a little bit hack value! i'don't know why. the height is always cut few pixel at the bottom
//can anybody tell me why?
      WebBrowser1.ClientHeight:=Webbrowser1.OleObject.Document.Body.ScrollHeight+4;

//note:if your html height or width is too big this may cause
//exception due to not enough resource or memory for bitmap
      TargetBitmap.Width:=WebBrowser1.ClientWidth;
      TargetBitmap.Height:=WebBrowser1.ClientHeight;
      arect:=Rect(0,
                  0,
                  WebBrowser1.ClientWidth,
                  WebBrowser1.ClientHeight);
//the actual draw
      ViewObject.Draw(DVASPECT_CONTENT,
                      0,
                      nil,
                      nil,
                      0,
                      TargetBitmap.Canvas.Handle,
                      @arect,
                      nil,
                      nil,
                      0);
      Image1.Top := ImgTop;
      Image1.Left := 0;
      Image1.Height := WebBrowser1.Height;
      Image1.Width  := WebBrowser1.Width;
      Image1.Picture.Bitmap.Assign(TargetBitmap);
      Image1.Refresh;
      ViewObject._Release;
      ViewObject:=nil;
    end;
  end;
end;

end.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 12858979
:( Don't understand whay can be done from IE. Isn't TWebBrowser a wrapper for IE?

Thanks for suggestion. I will accept your comment but isn't what I need. To navigate with IE and WebBrowser to same url mean supplimentar time. Is possible that IE to find the page but WB to block. Or viceversa...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now