Art  Delphi  Automation  History  Home  Politics  Email me Default Colours  Printable Colours

Automating Microsoft Office

Contents

For specific programs, follow the links:

Access Binder Excel
Outlook PowerPoint Word

General hints for Office automation

Downloads


 

General hints for Office automation

See also my introduction to automation, and Using Delphi 5's server components.

Sources of help

Web sites Delphi sites
Binh Ly's tutorials
Earl F. Glynn's tips pages

Non-Delphi sites
MS Visual Basic Programmer's Guide
How to find and use the MS Office Object model
Microsoft Developer's Network

Books Charlie Calvert's Delphi 4 Unleashed

If you're automating Microsoft Office, you're in luck.

For one thing, the most commonly automated programs are probably Word and Excel, followed by Outlook, and so there's some Delphi code out there showing you how to do it. (See the pages for the specific programs for links.)

For another, the Record Macro facility of programs like Word and Excel is really useful for the automation programmer. If you don't know how to do something from Delphi, just record yourself doing it in Word and then edit the macro to see what you did.

Then there are special VBA helpfiles for these applications, which helps, too. Admittedly they aren't installed by default - you have to choose them specially in the Office setup program. And, as helpfiles, they sometimes leave a certain amount to be desired. But they're a lot better than nothing. I suggest putting them on your Office shortcut bar - you'll find you use them a lot.

But of course, there are drawbacks in Office automation, too. Visual Basic for Applications, for example.


Converting from Visual Basic

All the examples in Microsoft's help files are in VBA, so it pays to make yourself familiar with it. Here are some things to watch out for when you're copying an example:

  1. Double quotation marks "like this" have to be changed to single ones 'like this'
  2. Words that are reserved words in Pascal need an underscore to be added to them
  3. If a mysterious extra parameter is demanded in Excel, you need to put in an LCID
  4. Square brackets are used in Pascal for array properties where VBA uses round ones
  5. You may need to cast to the correct interface, for example if a property is of type IDispatch

The last four points can be seen in this conversion from VBA to Delphi:

VBA Selection.End(xlDown).Select
Delphi (Excel.Selection[LCID] as Range).End_[xlDown].Select;

But don't worry - it's usually more straightforward than this!

Back to Top

Downloadable goodies

WaitForApp component - component with an event to inform you when an Office app closes. This is helpful with Excel, which has no OnQuit event, and Word97, which has a buggy one.