Chapter 5: Automating Windows and Applications


In Brief

In this chapter, you will first learn how to script applications, Control Panel applets, and Windows and Wizards from the command line. You will then learn about automation and how to script the Windows shell and most common applications (for example, Word, Excel, Internet Explorer). Finally, you will learn how to use send-keys to automate applications that do not easily support conventional scripting methods . In later chapters, you will learn how to automate Windows and applications to perform more specific tasks (such as adding shares, controlling services, or performing backups ).

Automation

Automation was originally created as a method for applications to easily access and control one another. Application automation originally developed from Dynamic Data Exchange (DDE), grew to Object Linking and Embedding (OLE), developed into OLE automation, and eventually turned into just Automation. Automation interfaces with applications through Component Object Model (COM) objects. COM objects are ActiveX controls that contain isolated sections of reusable code. Through automation, you can create documents, save files, play sounds, and even control the operating system, depending on whether it has an object model.

Visual Basic for Applications

Microsoft Office applications support a scripting language called Visual Basic for Applications (VBA). VBA, which is based on Visual Basic, is the standard programming language to control Microsoft Office application functions remotely. Application developers can use VBA to call other application functions from within their projects.

Note  

Applications that support VBA are known as "customizable applications."

A common method to produce easy VBA code is to record a macro and edit it in the built-in Visual Basic editor. To record a new macro, start an Office application and select ToolsMacroRecord New Macro. After you have started recording, perform the functions you would like to code and then stop the macro recording. Next, start the Visual Basic Editor by selecting ToolsMacroVisual Basic Editor. After the editor opens, select ToolsMacro, highlight your macro, and click Edit. In Figure 5.1, you can see the VBA code of all the functions you have just recorded.

click to expand
Figure 5.1: Editing a recorded Office macro.

Through Windows Script Host, you can use VBScript to call many VBA functions to automate Office applications. There are three steps to controlling an application through automation: accessing the application object, controlling the application, and closing the application object.

Accessing the Application Object

The application object is the top-level object, which allows you to send data to an application object and manipulate a program through it. As you learned in Chapter 1, in order to gain access to an object, you must first use the CreateObject method and set it to a variable:

 Set  variable  = CreateObject("  object.  Application") 

Once the instance is created, you can use this variable throughout your script to access all the methods within the object. Here is a list of the most common automation identifiers:

  • Access.Application ”Used to automate Microsoft Access

  • Excel.Application ”Used to automate Microsoft Excel

  • InternetExplorer.Application ”Used to automate Microsoft Internet Explorer

  • Outlook.Application ”Used to automate Microsoft Outlook

  • PowerPoint.Application ”Used to automate Microsoft PowerPoint

  • Shell.Application ”Used to automate Microsoft Windows

  • Word.Application ”Used to automate Microsoft Word

Microsoft Office contains help files on how to use automation with the various Microsoft Office applications. To view these files, run the Office setup and install the help files for Visual Basic. Run the application's help feature and search for "VBA HELP."

Changing the Application Visibility

After you've instantiated an application object, most of the objects start in hidden mode. This allows you to manipulate the object and perform various tasks before making the object visible. To make the object visible, set the object's visible state to true:

  Variable  .Visible = True 

Similarly, you can hide the object by setting the visible state to False .

Closing the Application Object

After you are finished with the application object, you should close it to free up system resources. To close an application object, proceed as follows :

  Variable  .Quit 

If an application object is not closed properly, that application will remain in memory regardless of its visibility or use. You should leave objects open only if you plan to use them at a later moment, such as using Microsoft Outlook to send admin alerts.




Windows Admin Scripting Little Black Book
Windows Admin Scripting Little Black Book (Little Black Books (Paraglyph Press))
ISBN: 1933097108
EAN: 2147483647
Year: 2004
Pages: 89

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