Online Book Reader

Home Category

Access Cookbook - Ken Getz [277]

By Root 2040 0
work through each of the procedures you'll find in that module.

TIP

Although this section gives you a good start working with Outlook programmatically, you'll find that Outlook has an extremely rich and powerful object model, allowing you to work with contacts, mail items, and schedule items, as well as the entire Outlook user interface.

The first block of code in basAutomateOutlook looks like this:

Private ola As Outlook.Application

Private nsp As Outlook.NameSpace

Public Sub InitOutlook( )

' Initialize a session in Outlook.

Set ola = New Outlook.Application

' Return a reference to the MAPI layer.

Set nsp = ola.GetNamespace("MAPI")

' Let the user log into Outlook with the Outlook

' Profile dialog, then create a new session.

nsp.Logon , , True, False

End Sub

Public Sub CleanUp( )

' Clean up public object references.

Set nsp = Nothing

Set ola = Nothing

End Sub

This code block includes module-level variables that refer to the Outlook Application and Namespace objects. Each example (and any code you write that works with Outlook) will probably need these variables as well, so it made sense to simply make them module-level, available to all procedures in the module.

Each procedure in this example calls the InitOutlook procedure, which instantiates a new copy of Outlook if it's not already running, or grabs onto the existing instance if it is already running. (Outlook does not allow itself to start up multiple times, so you'll never have multiple copies concurrently running in memory.) After this code runs, you can use the variable ola to refer to the running instance of Outlook:

Set ola = New Outlook.Application

Next, the code creates a new Workspace object. You're required to log in whenever you work with data within the Outlook data store, and the Namespace object provides this capability. Since you pass in the parameter MAPI to the GetNameSpace method, it might appear that there are other namespaces you might want to use, but that's not the case; Outlook uses only the MAPI namespace, and you'll always pass that parameter to the GetNameSpace method:

' Return a reference to the MAPI layer.

Set nsp = ola.GetNamespace("MAPI")

Finally, the InitOutlook procedure calls the Logon method of the Namespace object, allowing you to log into Outlook. If Outlook is already running, you won't see a dialog. If not, you'll see the standard dialog shown in Figure 12-18.

Figure 12-18. This familiar dialog appears when you log into Outlook

The portion of the code that handles logon is:

' Let the user log into Outlook with the Outlook

' Profile dialog, then create a new session.

nsp.Logon , , True, False

TIP

You might want to investigate the Namespace object's Logon method in Outlook's online help—it has several options that allow you to pass authentication information within the method call. You can control whether to show the dialog, as well.

Next in the sample module, the CleanUp procedure releases the module-level variables. If your code started up Outlook (that is, it wasn't already running), releasing those variables should allow Outlook to shut down.

The AddContact method, shown here, simply creates a new Outlook contact, given the information you pass to it:

Public Sub AddContact(varFirstName As Variant, varLastName As Variant, _

varAddress As Variant, varCity As Variant, varState As Variant, _

varPostalCode As Variant, varEmail As Variant)

Dim cti As Outlook.ContactItem

InitOutlook

Set cti = ola.CreateItem(olContactItem)

cti.FirstName = varFirstName & ""

cti.LastName = varLastName & ""

cti.HomeAddressStreet = varAddress & ""

cti.HomeAddressCity = varCity & ""

cti.HomeAddressState = varState & ""

cti.HomeAddressPostalCode = varPostalCode & ""

cti.Email1Address = varEmail & ""

cti.Display

Set cti = Nothing

CleanUp

End Sub

This procedure accepts parameters containing all the fields you gathered on your Access form. (Look back at the call to the AddContact method to see that you're passing in all the values from the original form.) It starts by initializing Outlook,

Return Main Page Previous Page Next Page

®Online Book Reader