1. Home
  2. Computing & Technology
  3. Delphi Programming

Using TWebBrowser to preview (and print) Microsoft Word documents

By , About.com Guide

If you decided to use (Microsoft) Word as a "printing engine" for your applications, you will probably want to have some kind of print and print preview functionality.
Here's how to use the TWebBrowser control to preview and print Microsoft Word documents.

Drop a TWebBroswer (name: WebBrowser1) on a Form and assign the next code for the NavigateComplete2 event handler:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TWordPreviewForm.FormCreate(Sender: TObject) ;
begin
   //open a Word document in WebBrowser
   WebBrowser1.Navigate('c:\SomeFolder\SomeDocument.doc') ;
end;

procedure TWordPreviewForm.WebBrowser1NavigateComplete2(Sender: TObject;
   const pDisp: IDispatch; var URL: OleVariant) ;
begin
     with (WebBrowser1.Document AS _Document) do
     begin
       ActiveWindow.View.ShowAll := False;
       ActiveWindow.View.TableGridlines := False;
       ActiveWindow.DisplayRulers := False;

       ActiveWindow.View.type_ := wdPageView;
     end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
The code in the above handler sets the "view type" to "Print layout", hides all non-printing characters (bookmarks, tab characters, etc.), hides table grid lines, and hides rulers.

Dont's miss: How to print a Word document contained inside a WebBrowser, and other WebBrowser tips!

Delphi tips navigator:
» Retrieving all image links from an HTML document
« Creating a roll up form (with animation)

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TWebBrowser
  6. Using TWebBrowser to preview (and print) Microsoft Word documents

©2009 About.com, a part of The New York Times Company.

All rights reserved.