Overview: Automation

In the Visual C++ Developer Studio environment, you perform many tasks manually. For example, you use menu commands to open projects, change source code files, save changes to these files, and then rebuild applications that use the files.

If your tasks are routine or repetitive, you can benefit from automating them. Through Automation (formerly OLE Automation), you can reduce the time spent on these tasks, and you can prevent errors that often result from performing the tasks manually.

With Automation, you perform tasks programmatically by writing VBScript macros or Developer Studio add-ins. VBScript macros are procedures you write in the , and add-ins are in-process COM components (DLLs) you write in Visual C++ or Visual Basic.

In macros or add-ins, you perform tasks by manipulating the Developer Studio environment and its components as objects. For example, you open, edit, and close a document by manipulating it as an object. Similarly, you activate or size a window by manipulating it as an object.

An object represents one component of the development environment, or it represents a collection of related components. For example, a Document object represents one open document, whereas the Documents object represents all open documents. Similarly, a Window object represents one open window, whereas the Windows object represents all open windows. The Documents and Windows objects are called collection objects because they include collections of related objects.

You manipulate objects by using methods, properties, and events associated with the objects. Methods represent actions you take against the objects. Properties represent characteristics of the objects, such as their type or size. And, events represent conditions under which actions are taken against the objects.

Suppose you want to create a C/C++ source file quickly — by simply pressing a button. Start by writing a VBScript macro that will create the file. The macro looks like this:

Sub CreateCPPFile
   Set CPPDoc = Documents.Add(“Text”)
   CPPDoc.Language = dsCPP
End Sub

This macro does the following:

  • The Sub statement begins the macro, which has the name CreateCPPFILE. The name is arbitrary; you can use whatever name you choose.

  • The first line of the macro creates the file by adding a Document object to the collection of Document objects, using the Add method of the Documents collection.

  • The second line of the macro specifies the file type (C/C++) by setting the Language property of the Document object equal to the constant dsCPP. This constant represents C/C++.

  • The End Sub statement ends the macro.

After writing the macro, assign it to a toolbar button. Then, press the button to run the macro.

For more information about writing VBScript macros, see Overview: VBScript Macros. For details about objects you can use to perform tasks in Developer Studio, see Reference: Developer Studio Objects. And, for details about creating add-ins, see Overview: Developer Studio Add-ins.