How to automate Windows applications

Automating Word

If you've got a copy of Word installed, you can automate the creation of a Word document with a script along these lines:

$oWord = ObjCreate("Word. Application")
$oWord.Documents.Add
$oWord.Selection.TypeText("Some words go here")
$oWord.ActiveDocument. SaveAs("c:\autoitcreatedme.doc")
$oWord.Quit

Here we've created a 'Word. Application' object (essentially loading an instance of Word) and added to its collection of documents (you could have several open at once, but this just creates the first).

The 'Selection' property corresponds to the current insert point, where the cursor would be if you'd opened the document manually, and the 'TypeText' method enters some text there. Finally we save our work to a specified path and use the 'Quit' method to close the object.

The last step isn't strictly necessary when it's the end of the script, but if we were carrying on then it would be a good idea. A Word.Application object takes up a sizeable amount of RAM, and it's worth freeing that up as soon as you can.

Word

You can extend scripts like this in many different ways. Open a document instead of creating one by replacing '$oWord.Documents.Add' with $oWord.Documents.Open("c:\filename.doc"*.

Style the text in your document by setting the font name – add $oWord.Selection.Font.Name = "Arial" – and size – use $oWord. Selection.Font.Size = "24". Add the line $oWord.PrintOut() before the final 'Quit' and it'll send your work to the printer too.

Using Outlook Microsoft

Outlook provides a powerful COM object that presents all kinds of automation possibilities. Perhaps the most useful of these is the ability to send an email from a script. Here's an example:

$oOutlook = ObjCreate("Outlook. Application")
$oMsg = $oOutlook.CreateItem(0)
$oMsg.To = "persons.name@ domain.com"
$oMsg.Subject = "COM test"
$oMsg.Body = "important content goes here"
$oMsg.Send

Our script starts by creating an 'Outlook.Application' object. This will usually load Outlook if it's not running already, but it may not always work. Launch Outlook before running the script if you get errors. The script then uses '$oOutlook' to create a further object and '$oMsg' to create a new message before setting its recipient, subject and body.

We could have set a CC address using $oMsg.CC = "you@youraddress.com" or the sender's address with $oMsg.SenderEmailAddress = me@myisp.com, but simpler is better right now.

We finish with the 'Send' method, which sets our message on its way. At least, that's the plan. In practice, you'll probably find that Outlook pops up a message complaining that a program is trying to access it to send emails. This is a security feature that's designed to stop malware using your PC to send spam, but unfortunately it'll block your legitimate script too.

warning

If you'd like to be able to send emails from a script then the best solution is to install Advanced Security for Outlook. This clever tool detects programs trying to access Outlook and gives you the option to block them if they're unexpected or run them without warnings if you're sure they're legitimate. It works on every version of Windows from 95 to 7, and every version of Outlook from 2000 to 2010. Best of all, it's free.

Going further

If you're interested in using COM to automate functions and would like to create more advanced scripts, you'll need to learn more about COM and the individual objects that different applications make available. The simplest way to go about this is to open the AutoIt Help file. Click the Contents tab and select 'Obj/COM Reference' to find a quick COM primer with some handy examples, including one on automating Excel functions.

If you expand the Word Management folder, you'll find information on some functions bundled with AutoIt that let you automate Word. Look at the example for '_Word DocAddPicture', for instance, to see how you can create a Word document from an AutoIt script and then add some pictures to it.

The AutoIt forum is another essential source of help. If you have a question, it's probably been asked before, so the Search tool should reveal a variety of useful answers. If you're lucky, someone may have already uploaded a script that does what you want in the Example Scripts section of the forum. Be sure to search the forum before you start writing something – you could save yourself a lot of time.

If you're feeling really ambitious then you could browse Microsoft's own documentation. Its pages on the Shell.Application object don't use AutoIt examples, but you'll get more details on the various methods the object makes available and how they should be used.

Alternatively, if you don't have much use for COM then you could simply wait until the next issue of PC Plus for the third and final part of this tutorial series. We'll be combining everything we've learned so far, giving our scripts an interface so that they can communicate with the user, adding some low-level tricks and showing how the leading AutoIt scripts can compete with the best that any other programming language has to offer.