Using a VTable or dispatch interface object to control Microsoft Word

You can use either a VTable or a dispatch object to control Microsoft Word. Using a VTable interface object

By dropping an instance of the WordApplication object onto your form, you can easily access the control using a VTable interface object. You simply call on methods of the class you just created. For Word, this is the TWordApplication class.

1 Select the button, double-click its OnClick event handler and supply the following event handling code:

procedure TForm1.Button1Click(Sender: TObject); var

FileName: OleVariant; begin if OpenDialogl.Execute then begin

FileName := OpenDialogl.FileName;

  1. Documents.Open(FileName, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam);
  2. ActiveDocument.PrintOut( EmptyParam,EmptyParam,EmptyParam, EmptyParam, EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam); end;

end;

2 Build and run the program. By clicking the button, Word prompts you for a file to print.

Using a dispatch interface object

As an alternate, you can use a dispatch interface for late binding. To use a dispatch interface object, you create and initialize the Application object using the _ApplicationDisp dispatch wrapper class as follows. Notice that dispinterface methods are "documented" by the source as returning VTable interfaces, but, in fact, you must cast them to dispatch interfaces.

1 Select the button, double-click its OnClick event handler and supply the following event handling code:

procedure TForm1.Button1Click(Sender: TObject); var

MyWord : _ApplicationDisp; FileName : OleVariant; begin if OpenDialog1.Execute then begin

FileName := OpenDialog1.FileName; MyWord := CoWordApplication.Create as

_ApplicationDisp; (MyWord.Documents as DocumentsDisp).Open(FileName,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam) ; (MyWord.ActiveDocument as _DocumentDisp).PrintOut(EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam); MyWord.Quit(EmptyParam,EmptyParam,EmptyParam); end; end;

2 Build and run the program. By clicking the button, Word prompts you for a file to print.

Was this article helpful?

0 0

Post a comment