Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:23:39+00:00 2026-05-27T09:23:39+00:00

procedure TForm1.Button1Click(Sender: TObject); var vaIn, vaOut: OleVariant; begin WebBrowser1.Navigate(‘http://www.google.com’); while WebBrowser1.ReadyState < READYSTATE_COMPLETE do

  • 0
procedure TForm1.Button1Click(Sender: TObject);
var 
  vaIn, vaOut: OleVariant;
begin
  WebBrowser1.Navigate('http://www.google.com');
  while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
    Application.ProcessMessages;
  WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);

  // HOWTO: WAIT until print <strike>job</strike> dialog is done or canceled

  // UPDATE (1):
  WebBrowser1.Enabled := False;
  WebBrowser1.OnCommandStateChange := WebBrowser1CommandStateChange;
end;

procedure TForm1.WebBrowser1CommandStateChange(Sender: TObject; Command: Integer; Enable: WordBool);
begin
  Memo1.Lines.Add(Format('%d : %d : %d', [WebBrowser1.QueryStatusWB(OLECMDID_PRINT), Command, Ord(Enable)]));
  // TODO: after LAST event when the print dialog closes:
  // WebBrowser1.OnCommandStateChange := nil;
end;

Same goes for Preview:
WebBrowser1.ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT, vaIn, vaOut);

I need to wait (or trigger an event) until the Print / Print Preview dialogs are done, and user has selected either print or cancel.

UPDATE (1)

Based on this question I tested the OnCommandStateChange.
It is fired after print or cancel in the Print dialog. but it can be fired 1 or 2 times before the dialog opens.

UPDATE (2)
Found a workaround that might do the trick (it’s a basic idea):

procedure TForm1.WaitPrintDialog;
var
  t1, t2: DWORD;
  w, wpd: HWND;
begin
  t1 := GetTickCount();
  t2 := t1;
  wpd := 0;
  while ((wpd = 0) and (t2 - t1 <= 5000)) do // 5 sec timeout
  begin
    w := FindWindowEx(0, 0, 'Internet Explorer_TridentDlgFrame', nil);
    if (w <> 0) and (GetWindow(w, GW_OWNER) = Self.Handle) then
    begin
      wpd := w;
    end;
    Application.ProcessMessages;
    t2 := GetTickCount();
  end;
  if wpd <> 0 then // found and no timeout
    while IsWindow(wpd) and (not Application.Terminated) do
    begin
      Application.HandleMessage; // Application.ProcessMessages;
    end;
end;

usage:

WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);
WaitPrintDialog;
ShowMessage('Print Done!');

works both for OLECMDID_PRINT and OLECMDID_PRINTPREVIEW
please tell me what you think…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:23:40+00:00Added an answer on May 27, 2026 at 9:23 am

    When I’ve been looking for a solution I’ve found the PRINT_WAITFORCOMPLETION flag few days ago but can’t get it to work. And the trick was quite easy (see note nr. 4). I’ve been wrong with passing the third parameter of ExecWB method for the OLECMDID_PRINT command as variant type VT_I4 but it is overloaded and for PRINT_WAITFORCOMPLETION must be converted to the exact type VT_I2, what is in Delphi represented as a smallint.

    Here is how to make the print dialog modal (also answer to this by accident 🙂

    procedure TForm1.Button1Click(Sender: TObject);
    var
      vaIn: OleVariant;
      vaOut: OleVariant;
    const
      PRINT_WAITFORCOMPLETION = $02;
    begin
      WebBrowser1.Navigate('http://www.google.com');
      while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
        Application.ProcessMessages;
    
      vaIn := OleVariant(VarAsType(PRINT_WAITFORCOMPLETION, varSmallint));
      WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, vaIn, vaOut);
    
      ShowMessage('Print dialog has been closed ...');
    end;
    

    Unfortunately you can’t get any feedback if user sent the document to the printer queue or cancelled the dialog. The IDM_PRINT has no output value, which would return this. Another thing is that even if user accepts the printing dialog it doesn’t mean that the document will be physically printed. For this you would have to, as Remy said, monitor the printer queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code: var WinHttpReq: OleVariant; procedure TForm1.Button1Click(Sender: TObject); begin WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1'); WinHttpReq.Open('GET', 'http://stackoverflow.com',
code sample procedure TForm1.Button1Click(Sender: TObject); var r: Trect; s: String; begin R := Rect(0,0,
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin
I have some simple code: procedure TForm1.Button1Click(Sender:TObject); var x: RawByteString; begin x := UTF8Encode('testing
What does (Sender:TObject) mean?? As in: procedure TForm1.Button1Click(Sender:TObject); var s: Integer; begin ..... .....
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self
what is wrong there? procedure TForm1.VCLHelpClick(Sender: TObject); var Ctrl : TWinControl; begin Ctrl :=
How do I change desktop wallpaper? I tried this procedure TForm1.Button1Click(Sender: TObject); var PicPath:
i would like to create a procedure on this. procedure TForm1.Button1Click(Sender: TObject); var a:
Ok, I have Idhttp created dynamically like the following procedure TForm1.Button1Click(Sender: TObject); Var Resp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.