Programming Delphi How to use the TWebBrowser to open a Microsoft Word document in delphi
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/ on line 149

If you want to preview a Microsoft word document in your programs, an easy way is to use the TWebBrowser component as the container. TWebBrowser is the internet explorer engine, you just need to use its navigate method to assign a Word document, it would identify the format automatically. Let us see the codes.

Create a new project, drag a TWebBrowser onto the form. Adding codes as below. Here we assume you have a word document saved in "c:\docuemnts\myword.doc". Of course you could put a button on the form then use its OnClick event to specify a document in the running time.

procedure TWordPreviewForm.FormCreate(Sender: TObject) ;
   //This codes would open a Word document you specify in WebBrowser1.
   WebBrowser1.Navigate('c:\docuemnts\myword.doc') ;

In the onNavigateComplete2 event, you could adding these codes to control some attributes of the opened document.
procedure TWordPreviewForm.WebBrowser1NavigateComplete2(Sender: TObject;
   const pDisp: IDispatch; var URL: OleVariant) ;
     with (WebBrowser1.Document AS _Document) do
       //Hides all non-printing characters such as enters, bookmarks, tab characters, etc.
       ActiveWindow.View.ShowAll := False;

       //Hides the table grid lines. 
       ActiveWindow.View.TableGridlines := False;

       //Hides the rulers.
       ActiveWindow.DisplayRulers := False;

       //Sets the "view type" to a "Print layout" mode.
       ActiveWindow.View.type_ := wdPageView;

Now run your program to see if the effect is what you need.

Add comment

Security code

Programming - Delphi