Automation MSWord from Delphi

Dear Sir,
I would like to control MSWord from Delphi. Using Variants is a simple way, but a bit slow. I am trying to get some information about the other way - through DispInterface using MSWord8.olb, which should be  performing the automation tasks a bit better.

I would really appreciate your help.

Best regards from
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

nrobinConnect With a Mentor Commented:
What you can do is load Word up, click on the Tools|Macro and edit one, so that the VB editor appears.  From here you can pres F2 to see the objects and their properties.  You can use this to look at what you can access.


import type library is faster than variants.

Do the import, choose for create unit.

Do something like:
myWord: _Application;
myWord := CoApplication.Create;
myWord.Visible := True;
//maximize by handle, if you like:
hndl := FindWindow(PChar('OpusApp'), nil);
if hndl <> 0 then
   ShowWindow(hndl, SW_MAXIMIZE);

//new doc
Template := 'NORMAL.DOT';
NewTemplate := False;
myWord.Documents.Add(Template, NewTemplate);
//with some text

Well, it's a start. Hope I wrote correct code...:-)

zelenkaAuthor Commented:
Ok, what next?

How can I get a list of exported methods and properties? Is a documentation of a MSWOrd8.Olb available on the market?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

To get a list you have to import that type libray in Delphi.

I don't have Delphi started at the moment... ..something like menu:
project | import type library | create unit.

Add this imported unit to your project uses list, and you can use all properties and methods.

As far as I know there's no complete documentation of some kind, just a lot of info on the internet.

How to know what to do:
record a macro in Word and do what you want to do. View the macro, find the methods in the imported library and rewrite the macro in Delphi.

Automating f.e. Word Perfect is easier, there's a tool called: OLE Command Inserter; translating your macro and inserting it into Delphi automatically. I never found something like that for Word, but thanslation macro's works.

Good luck,

by the by:

by importing a type library you could miss some functionality. For the most complete set of funtionality, you can use File | open and open a type library (f.e. .olb file) in the type library editor. I still do prefer an imported unnit.
What would you like to do?

All Courses

From novice to tech pro — start learning today.