Automating a program such as Microsoft Project means that you control the program remotely, even invisibly, as if you were actually using the interface. This means, for example, that you can use the Spelling Checker feature of Microsoft Word in Microsoft Project or the math functions of Microsoft Excel from within Microsoft Project. In fact, you can even automate Microsoft Project from a Web page.
Automating with a Library Reference
Programs that support making references to the Microsoft Project object model, such as Microsoft Word, provide the easiest access to Automation:
• Setting a reference to a library by using the References item on the Tools menu automatically provides the connection between the host application and Microsoft Project.
• Many of the tools available when writing Visual Basic code in Microsoft Project are typically available in the host's development environment, such as the Auto List Members option. (Obviously, writing Automation code from a VBA-enabled program provides all the tools available from Microsoft Project because they both use the Visual Basic Editor.)
Follow these steps to create a simple example of automating Microsoft Project from any Microsoft Office program: h
1 Press Alt+F11 to start the Visual Basic Editor.
2 In the References dialog box, scroll through the Available References list and then click o the Microsoft Project 11.0 Object Library.
3 Create a new procedure and type the following code into it:
MSProject.Visible = True MSProject.FileNew
MSProject.ActiveProject.Tasks.Add "task 1" MSProject.ActiveProject.Tasks.Add "task 2" MsgBox MSProject.ActiveProject.Tasks.Count
An obvious difference from typing this code in Microsoft Project itself is the MSProject class, which represents the Microsoft Project Application object. In every other respect, though, writing code that uses members of the Microsoft Project object model in another application looks and "feels" just as it would in Microsoft Project.
Automating Microsoft Project when you can't directly link to the object model, such as when using Automation from a Web page or some similar environment, follows the same principles as when using a reference. It does, however, require more code and, more importantly, more research.
The additional code is necessary because the host application doesn't have the connection provided by the library reference. You must write code that creates a new instance of Microsoft Project as an object reference before you can use the members of the object model.
The additional research is necessary because the host application doesn't know anything about the Microsoft Project object model, which affects your code in two major ways:
• The code you're typing is essentially treated like text (there's no link to the object model to verify members of objects or automatically list arguments for methods, for example), so you must be reasonably comfortable with the object model (or at least have easy access to information about it).
• You must use the numeric values of any constants defined in the Microsoft Project object model because the host application can't know what values the constants map to without an active reference to the object model. (In this situation, it's common to declare constants of the same name as used by Microsoft Project so your code is as readable as it would be if you had a library reference.)
Follow these steps to create a simple example of automating Microsoft Project from any pro-
h gram that supports Visual Basic or Visual Basic Scripting edition (VBScript): p rr 1 Define an object variable and then use the CreateObject method from the Visual Basic
1 (or VBScript) core object library to make a connection to Microsoft Project:
Dim Proj As Object
Set Proj = CreateObject("MSProject.Application")
Note This is an example of a late-bound object reference. For more information about late-binding, see Chapter 30.
2 Enter the code that provides the functionality for your procedure, replacing "Application" (when writing a procedure in Microsoft Project) or "MSProject" (using Automation with a library reference) with the object variable defined in step 1:
Proj.Visible = True Proj.FileNew
Proj.ActiveProject.Tasks.Add "task 1" Proj.ActiveProject.Tasks.Add "task 2" MsgBox Proj.ActiveProject.Tasks.Count
3 When entering the code for the Quit method, use the numeric value of the pjDoNotSave constant:
4 Discard the object variable's reference to Microsoft Project: Set Proj = Nothing
If it is more convenient, you can enter this code in the Visual Basic Editor of any Microsoft Office program, and it behaves just as it would in a Web page or the like. If you do use the Visual Basic Editor, make sure that the project doesn't have a reference to the Microsoft Project library.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.