Microsoft Office Project 2003 Inside Out

As mentioned in the discussion of the Tools menu earlier in this chapter, one of the most powerful functionalities of the VBA environment is its capability to extend itself beyond the bounds of the host program, which can be done in two main ways:

Working with External References

Working with external references usually means either adding ActiveX controls to your forms or using external type libraries to do things that aren't part of the object model for the program you're using. The Microsoft Web Browser control, for example, is an ActiveX control commonly added to forms, whereas a reference to one of the Microsoft ActiveX Data Objects (ADO) libraries might be used to provide easy access to data sources.

Using ActiveX Controls

Working with ActiveX controls is probably the easiest way to use external references. Controls that have been registered on your computer (this happens automatically when controls are installed on your computer) appear in the Additional Controls dialog box (click Tools, Additional Controls) and can be added to the Toolbox. After a control appears in the Toolbox, you can use it just as you would any of the intrinsic controls in the Microsoft Forms library that is included with Microsoft Project.

All ActiveX controls have common properties for a name , physical dimensions and position, tab order, and so on, in addition to whatever properties and methods they supply as part of their specialized functionality. All the general information you know from working with the intrinsic controls also applies to new controls, enabling you to concentrate on working with the new capabilities the control provides.

Note  

Many ActiveX controls also have type libraries associated with them, which appear in the object browser. These libraries might be listed in the Library box by the filename of the library instead of by the name of the control as it appears in the Additional Controls dialog box.

Using External Libraries

An external type library (sometimes called a "helper library") is really nothing more than an object model that doesn't have a visual element and doesn't represent the Visual Basic aspect of another program. Type libraries are installed in association with a program or the operating system.

After you add a reference to a library (click Tools, References), the objects, methods, properties, events, and constants defined in the library appear in the object browser. There are no special rules for working with an external type library and you can use its members as you would the members of the Microsoft Project object model.

The following code is an example of working with an external type library. It uses the Microsoft ActiveX Data Objects 2.6 Library to access the values of certain fields for each record in a Jet (Microsoft Access) database:

Sub ReadDataFile(strFileName As String) ' The Connection object is found in the ADO library ' The New keyword in a variable declaration creates an object ' reference at the same time as the variable is declared Dim conData As New Connection ' The Recordset object is found in the ADO library Dim rstTopic As New Recordset Dim strSelect As String Dim bytCount As Byte ' Connect to data source ' ConnectionString is a property of the Connection object conData.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "& _ strFileName ' This Open method is for the Connection object conData.Open ' Select the records and open a recordset strSelect = "SELECT * FROM Topic" ' This Open method is for the Recordset object rstTopic.Open strSelect, conData ' Move to the first record in the Recordset object rstTopic.MoveFirst ' Read through each record to find the values I want For bytCount =1To rstTopic.Fields!byt1.Value rstTopic.MoveNext ' gudtText is a global, user-defined collection object gudtText.blnNumbered.Add rstTopic.Fields!byt1.Value gudtText.bytMatchNode.Add rstTopic.Fields!byt2.Value gudtText.bytNumber.Add rstTopic.Fields!byt3.Value gudtText.strText.Add rstTopic.Fields!str2.Value Next bytCount ' Close the reference to the Connection object conData.Close ' Clean up the object references Set rstTopic = Nothing Set conData = Nothing End Sub

Automating Microsoft Project

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:

Follow these steps to create a simple example of automating Microsoft Project from any Microsoft Office program:

  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 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 MSProject.Quit pjDoNotSave

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 without a Library Reference

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:

Follow these steps to create a simple example of automating Microsoft Project from any program that supports Visual Basic or Visual Basic Scripting edition (VBScript):

  1. Define an object variable and then use the CreateObject method from the Visual Basic (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:

    Proj.Quit 0

  4. Discard the object variable's reference to Microsoft Project:

    Set Proj = Nothing

    Note  

    Using the Visual Basic Editor       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.

Категории