Take the 2-minute tour ×

I have a this code:

    for i:=0 to Memo1.Lines.Count-1 do
           while WebBrowser1.Busy do Application.ProcessMessages;

but WebBrowser1 run several times in spite of the expectations of the procedure. How do I start WebBrowser1 not in the background or what is the solution?

share|improve this question
Could you elaborate what WebBrowser1 run several times ? And what procedure ? Did you mean some event or where is the code you've posted placed ? Please take the time to update your question since it's unaswerable at this time. –  TLama Aug 9 '12 at 18:20
I have a some link on loaded page in WebBrowser and need click each in FOR cycle and wait until complete processing 1st link. In Memo1.Lines i have some link. subcatlist thiы is procedure like this code.Оn the site several categories of investments, I collect all attachments –  Stas Aug 9 '12 at 18:51

1 Answer 1

up vote 1 down vote accepted

You need to implement 3 events of TWebBrowser, BeforeNavigate2, DocumentComplete and NavigateComplete2

    TForm1 = class(TForm)
      CurDispatch: IDispatch; 
      FDocLoaded: Boolean;    

    procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
      const URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
      var Cancel: WordBool);
      CurDispatch := nil;
      FDocLoaded := False;

    procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;
      const URL: OleVariant);
      if (pDisp = CurDispatch) then  
        FDocLoaded := True;
        CurDispatch := nil;

    procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch;
      const URL: OleVariant);
      if CurDispatch = nil then
        CurDispatch := pDisp;

And now you can use FDocLoaded variable to know if the page is loaded into the WebBrowser

    repeat Application.ProcessMessage until FDocLoaded;


share|improve this answer
Application.ProcessMessage or Application.ProcessMessages? –  Stas Aug 9 '12 at 19:33
Thank you, after delete my old procedure all good ) –  Stas Aug 9 '12 at 21:33
@TLama this solutions function with a page with frames? –  cadetill Aug 10 '12 at 4:57
That solution was for whole pages (but taking back my comment)... –  TLama Aug 10 '12 at 7:22

Your Answer


