Hour 22. Controlling Other Applications Using Automation


What You'll Learn in This Hour:

  • Creating a reference to an automation library

  • Creating an instance of an automation server

  • Manipulating the objects of an automation server

In Hour 16, "Designing Objects Using Classes," you learned how to use classes to create objects. In that hour, I mentioned that objects could be exposed to outside applications. Excel, for example, exposes most of its functionality as a set of objects. The process of using objects from another application is called automation. The externally accessible objects of an application compose its object model. Using automation to manipulate a program's object model enables you to reuse components. You can use automation with Excel, for instance, to perform complex mathematical functions using the code that's been written and tested within Excel instead of writing and debugging the complex code yourself.

Programs that expose objects are called servers, and the programs that consume those objects are called clients. Creating automation servers requires advanced skills, including a thorough understanding of programming classes. On the other hand, creating clients to use objects from other applications is relatively simple. In this hour, you'll learn how to create a client application that uses objects of an external server application.

To understand automation, you're going to build two projects. The first will be a Microsoft Excel clienta program that automates Excel via Excel's object model. The second project will automate Microsoft Word.

By the Way

These exercises are designed to work with Microsoft Excel 2003 and Microsoft Word 2003. You must have these programs installed in order for the examples to work.


Begin by creating a new Windows Application named Automate Excel. Right-click Form1.cs in the Solution Explorer, choose Rename, and then change the name of the default form to frmMain.cs. Next, set the form's Text property to Automate Excel.

Next, add a button to the form by double-clicking the Button item in the toolbox and set the button's properties as follows:

Property

Value

Name

btnAutomateExcel

Location

96,128

Size

104,23

Text

Automate Excel





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net