Special Offers

Building Applications with Microsoft® Outlook® Version 2002
Author Randy Byrne
Pages 944
Disk 1 Companion CD(s)
Level All Levels
Published 07/25/2001
ISBN 9780735612730
ISBN-10 0-7356-1273-0
Price(USD) $59.99
To see this book's discounted price, select a reseller below.

More Information

About the Book
Table of Contents
Sample Chapter
Related Series
Related Books
About the Author

Support: Book & CD

Rate this book
Barnes Noble Amazon Quantum Books


Chapter 12: The Northwind Contact Management Application continued

The Outlook Shared Activities Add-In

The Northwind Contact Management application uses the Outlook Shared Activities COM Add-in. See the discussion on page 553, "Installing the Outlook Shared Activities Add-In," for instructions on installing this COM Add-in and its source code. The following discussion highlights some of the features of this add-in. The functionality of the add-in that relates to SharePoint Portal Server is discussed in Chapter 16. For additional details on creating COM Add-ins, see Chapter 14, "Creating COM Add-Ins with Visual Basic."

Displaying an Explorer Toolbar

When a user navigates to the Companies folder, the Create Shared Items toolbar (shown in Figure 12-5 on page 538) is displayed in the Outlook Explorer window so that the user can create a shared item without opening the form in the folder. Explorer toolbars provide a professional touch to your application and guide the user in ways that were not possible in Outlook 97 and Outlook 98. In the Northwind Contact Management application, a COM Add-in creates the Explorer toolbar.

Using the BeforeFolderSwitch Event

To provide an Explorer toolbar that appears only when a certain folder is the current folder, you write code in the BeforeFolderSwitch event of an Explorer object variable. When the correct folder is active, you make your custom toolbar visible. Otherwise you hide the Create Shared Items toolbar because it isn’t appropriate to see this toolbar when some other folder is current. The following code shows or hides the Create Shared Items Explorer toolbar depending upon the current folder. If the current folder has a subfolder named Setup that contains the CRM Administration form, the Create Shared Items toolbar is displayed. Otherwise, it is hidden.

Private Sub objExpl_BeforeFolderSwitch(ByVal NewFolder As Object, _
   Cancel As Boolean)
   On Error Resume Next
   ‘Used when NewFolder is file system folder
   If NewFolder Is Nothing Then
      objCommandBar.Visible = False
      Exit Sub
   End If
   Call CBSharedItemsShowOrHide(NewFolder)
End Sub

Sub CBSharedItemsShowOrHide(objFolder As MAPIFolder)
   On Error Resume Next
   Dim objFolderSetup As MAPIFolder
   Dim myAdmin As PostItem
   Dim myRestrictItems As Outlook.Items
   Dim objCommandBar As CommandBar
   Set objCommandBar = _
      golApp.ActiveExplorer.CommandBars("Create Shared Items")
   Set objFolderSetup = objFolder.Folders("Setup")
   If objFolderSetup Is Nothing Then
      objCommandBar.Visible = False
      Set myRestrictItems = objFolderSetup.Items.Restrict _
         ("[MessageClass]=‘IPM.Post.CRM Administration’")
      Set myAdmin = myRestrictItems.GetFirst
      If myAdmin Is Nothing Then
         objCommandBar.Visible = False
         objCommandBar.Visible = True
      End If
   End If
End Sub

For additional information on command bars and the Office Object Model, see Chapter 11, "Using Visual Basic, VBA, or VBScript with Outlook." To learn more about event procedures, see Chapter 9, "Raise Events and Move to the Head of the Class."

Running a Procedure When a User Clicks a Toolbar Button

Each command bar button on the Create Shared Items toolbar is declared using the WithEvents keyword in the OutAddIn class. The click event for the button fires when the user clicks a command bar button and a procedure is called in basNorthwind, creating a shared item in the appropriate folder. Here is an example of the procedure that is called when the Shared Appointment button is clicked:

Private Sub CBBAppt_Click(ByVal Ctrl As Office.CommandBarButton, _
   CancelDefault As Boolean)
   Call basNorthwind.cmdAppointment_Click
End Sub

Sub cmdAppointment_Click()
   Call ButtonClick(olAppointmentItem)
End Sub

The ButtonClick procedure does all the work of creating and displaying the new shared item in the correct folder. The pintItemType argument lets the ButtonClick procedure know what type of shared item is being created. GetFolder is another helper function that delivers the proper folder location where new items are created. Let’s take a look at the code now:

Sub ButtonClick(pintItemType)
   Dim objTargetFolder As MAPIFolder
   Dim objNewItem
   Dim oItem As ContactItem
   On Error Resume Next
   If golApp.ActiveExplorer.Selection.Count = 0 Then
      MsgBox "You must select a company " _
      & "before you can create a shared item.", vbInformation
      Exit Sub
   End If
   Set oItem = golApp.ActiveExplorer.Selection.Item(1)
   If GetFolder(pintItemType, objTargetFolder, oItem) Then
      ‘ Create item
      Set objNewItem = objTargetFolder.Items.Add
      Select Case pintItemType
         Case olMailItem
            Call SetMailProps(objNewItem, objTargetFolder, oItem)
         Case olContactItem
            Call SetContactProps(objNewItem, oItem)
         Case olJournalItem
            Call SetJournalProps(objNewItem, oItem)
         Case olAppointmentItem
            Call SetAppointmentProps(objNewItem, oItem)
         Case olTaskItem
            Call SetTaskProps(objNewItem, oItem)
      End Select
      Call objNewItem.Links.Add(oItem)
   End If
End Sub

Function GetFolder(pintFolderType, objFolder As MAPIFolder, oItem)
   Dim objMyFolders As Outlook.Folders
   Dim i As Integer
   GetFolder = False
   Set objMyFolders = oItem.Parent.Folders
   i = 1
   Do While i <= objMyFolders.Count
      If objMyFolders.Item(i).DefaultItemType = pintFolderType Then
         Set objFolder = objMyFolders.Item(i)
         GetFolder = True
         Exit Do
      End If
      i = i + 1
   If Not GetFolder Then
      MsgBox gconstErrorFolder, vbCritical, gconstAppName
   End If
End Function

SetAppointmentProps copies information from a Company or Company Contact item to the appointment. A similar procedure is called for each shared item created by the Create Shared Items toolbar. Because a custom form with a message class of IPM.Appointment.Shared Appointment is the default form in the Shared Calendar folder, objNewItem is created as a Shared Appointment form rather than as a standard Appointment form. A custom Appointment form in the Shared Calendar folder offers additional functionality that is not available in the default form.

Sub SetAppointmentProps(pobjNewItem, oItem As ContactItem)
   pobjNewItem.Companies = oItem.CompanyName
   pobjNewItem.BillingInformation = oItem.Account
End Sub

Outlook Security:
The SetMailProps call shown in the ButtonClick procedure you just saw will display the Outlook E-Mail Security Update Address Book Warning dialog box when a user clicks the Shared Mail command bar button. If you want to suppress this Warning dialog box, you should add the Outlook Shared Activities Add-in to the list of trusted COM Add-ins in the Administrative form. For additional details, see Chapter 13, "Distributing and Securing Applications."

Creating a Personal Copy of Shared Items

The following code in the Shared Appointment form creates a copy of the appointment in a user’s personal calendar if she toggles the Inspector command bar button to Personal Copy On. By default, the Personal Copy and Shared Folder user-defined fields are set to True. When the Shared Appointment form Item_Write event detects that Personal Copy and Shared Folder are True, a copy of the shared appointment item is placed in the user’s personal Calendar folder.

Sub Item_Write
   Dim objCopyItem
   Dim objNS
   Dim objPersonalFolder
   Const olFolderCalendar = 9
   On Error Resume Next
   If UserProperties("Shared Appt") And UserProperties("Personal Copy") Then
      Set objCopyItem = Item
      Set objPersonalFolder = _
      objCopyItem.Move objPersonalFolder
      objCopyItem.UserProperties("Shared Appt") = False
   End If
End Sub

Enabling and Disabling Inspector Menu Bar Commands

Certain commands on the Company and Company Contact menu bars must be disabled to make the application function correctly. For example, because our sample application is for shared appointments, tasks, and so forth, you don’t want the user to select New Task For Contact on the Actions menu. If the user selects this command, the new task will be placed in her personal Tasks folder rather than the Shared Tasks folder. Consequently, we need to enable and disable commands on the Actions menu.

An effective way to control Inspector command bars is by using the Open event of the contact item. You’ll run into a problem if you attempt to disable the menu bar commands through VBScript code in the Item open event of your custom form. The Inspector menu bar applies to all contact item inspectors. If a user opens a standard contact item, the Actions menu commands will be disabled incorrectly. As a workaround, you can use Outlook events to enable and disable commands on the Actions menu. You can create an object variable for items where the base message class is IPM.Contact and raise item-level events on that object variable. You should instantiate the item-level object variable in the NewInspector event of the Inspectors collection object. Whenever a new Inspector is created by a user action or program code, a new objContactItem object variable is created. By declaring objContactItem using the WithEvents keyword in the OutAddIn class module, you will be able to write code behind the objContactItem_Open event procedure to modify the command bars for the item. The following code enables or disables commands on the Actions menu of a contact item depending upon the message class of the item. For the two custom contact items in this application, commands on the Actions menu are disabled.

Private Sub objContactItem_Open(Cancel As Boolean)
   On Error Resume Next
   Dim objCBMenu
   Set objCBMenu = _
      objContactItem.GetInspector.CommandBars("Menu Bar").Controls("Actions")
   If objContactItem.MessageClass = "IPM.Contact.Company" _
      Or objContactItem.MessageClass = "IPM.Contact.Company Contact" Then
      objCBMenu.Controls("New Contact").Enabled = False
      objCBMenu.Controls("New Contact from Same Company").Enabled = False
      objCBMenu.Controls("New Meeting Request to Contact").Enabled = False
      objCBMenu.Controls("New Appointment with Contact").Enabled = False
      objCBMenu.Controls("New Journal Entry for Contact").Enabled = False
      objCBMenu.Controls("New Task for Contact").Enabled = False
      objCBMenu.Controls("Link").Enabled = False
      objCBMenu.Controls("Post Reply to Folder").Enabled = False
      objCBMenu.Controls("Copy to Personal Contacts").Enabled = False
      objCBMenu.Controls("New Contact").Enabled = True
      objCBMenu.Controls("New Contact from Same Company").Enabled = True
      objCBMenu.Controls("New Meeting Request to Contact").Enabled = True
      objCBMenu.Controls("New Appointment with Contact").Enabled = True
      objCBMenu.Controls("New Journal Entry for Contact").Enabled = True
      objCBMenu.Controls("New Task for Contact").Enabled = True
      objCBMenu.Controls("Link").Enabled = True
      objCBMenu.Controls("Post Reply to Folder").Enabled = True
      objCBMenu.Controls("Copy to Personal Contacts").Enabled = True
   End If
End Sub

Extending Your Application with Public Methods

The basic functionality of the Outlook Shared Activities COM Add-in centers around the addition of an Explorer toolbar. In this respect, this add-in is typical. However, it has some special features that allow you to extend your application beyond the addition of Explorer toolbars. For example, VBScript code behind forms cannot make Windows API calls. The Outlook Shared Activities COM Add-in wraps Telephony Application Programming Interface (TAPI) Calls in a public method Dial Phone. A similar approach of wrapping complex code in a method call is used for both the ShowDocumentDialog and GetConnectionString methods. Unlike a COM Add-in created with Office XP Developer, a COM Add-in created in Visual Basic can expose public properties and methods that are available to other applications or to the VBScript code behind the forms that compose your application. For example, the Post Office Document dialog box shown in Figure 12-9 (on page 543) allows the user to select from a range of templates in order to create a document that is posted to the Shared Documents folder. The following code from the Company form calls the ShowDocumentDialog method to display the Post Office Document dialog box:

Sub cmdDocument_Click()
   On Error Resume Next
   Dim objFolder, objDM
   ‘Item has to be saved, otherwise linking does not work.
   If Item.EntryId = "" then
      If MsgBox(gstrErrorButton, vbYesNo + vbQuestion, _
         gstrAppName) = vbYes Then
         Exit Sub
      End if
   End If
   Set objFolder = OpenMAPIFolder(gstrComp & "\Shared Documents")
   Set objDM = CreateObject("OutlookSharedActivities.OutAddIn")
   objDM.ShowDocumentDialog _
      objFolder, Item.CompanyName, Item.Account
End Sub

When the user clicks the OK button in the Post Office Document dialog box, a variety of strategies are employed to create the Office document from a template. Each Office application dictates the code used to create a new Office document from a template. In Excel 2002, for example, it is possible to use the NewWorkbook and WorkBookOpen events of the Excel Application object to determine which document the user has selected from the Excel 2002 Task Pane. The following example shows how the cmdOK_Click procedure in frmDocument creates an Office document, sets the Billing Information field to the value in the Item Account field, and then displays the item. This functionality would be impossible to achieve in VBScript.

Private Sub cmdOK_Click()
   Dim strSubject As String
   Dim strFilePath As String
   Dim oMsg As DocumentItem
   On Error Resume Next
   ‘Excel document
   If optExcel = True Then
      Set xlApp = New Excel.Application
      xlApp.DisplayAlerts = False
      If Val(xlApp.Version) <= 9 Then
         If xlApp.Dialogs(xlDialogNew).Show Then
            ‘User selected item
            strSubject = _
            If Trim(strSubject) = vbNullString Then
               strSubject = "Excel Document"
            End If
            strFilePath = GetTempDir & CleanFileName(strSubject) & ".xls"
            xlApp.Visible = True
            With xlApp.ActiveWorkbook
               .CustomDocumentProperties.Add _
                  Name:="Company Name", LinkToContent:=False, _
                  Value:=m_CompanyName, _
               .SaveAs strFilePath
            End With
            Set oMsg = m_Folder.Items.Add("IPM.Document.Excel.Sheet.8")
            oMsg.Attachments.Add strFilePath
            oMsg.Subject = strSubject
            oMsg.MessageClass = "IPM.Document.Excel.Sheet.8"
            oMsg.BillingInformation = m_Account
         End If
         ‘Excel 2002
         xlApp.Visible = True
         Exit Sub
      End If
   ‘PowerPoint document
   ElseIf optPPT Then
      Set ppApp = New PowerPoint.Application
      ppApp.DisplayAlerts = ppAlertsNone
      strSubject = InputBox("Presentation Subject:", "PowerPoint Document")
      If Trim(strSubject) = vbNullString Then
         strSubject = "PowerPoint Document"
      End If
      ppApp.Visible = msoTrue
      strFilePath = GetTempDir & CleanFileName(strSubject) & ".ppt"
      With ppApp.ActivePresentation
         .BuiltinDocumentProperties("Subject") = strSubject
         .CustomDocumentProperties.Add _
            Name:="Company Name", LinkToContent:=False, _
            Value:=m_CompanyName, _
         .SaveAs strFilePath
      End With
      Set oMsg = m_Folder.Items.Add("IPM.Document.PowerPoint.Show.8")
      oMsg.Attachments.Add strFilePath
      oMsg.Subject = strSubject
      oMsg.MessageClass = "IPM.Document.PowerPoint.Show.8"
      oMsg.BillingInformation = m_Account
   ‘Word Document
   ElseIf optWord Then
      Set wdApp = New Word.Application
      wdApp.DisplayAlerts = wdAlertsNone
      If wdApp.Dialogs(wdDialogFileNew).Show Then
         strSubject = _
            wdApp.ActiveDocument. _
         If Trim(strSubject) = vbNullString Then
            strSubject = "Word Document"
         End If
         strFilePath = GetTempDir & CleanFileName(strSubject) & ".doc"
         wdApp.Visible = True
         With wdApp.ActiveDocument
            .CustomDocumentProperties.Add _
               Name:="Company Name", LinkToContent:=False, _
               Value:=m_CompanyName, _
            .SaveAs strFilePath
         End With
         Set oMsg = m_Folder.Items.Add("IPM.Document.Word.Document.8")
         oMsg.Attachments.Add strFilePath
         oMsg.Subject = strSubject
         oMsg.BillingInformation = m_Account
         ‘Set MessageClass explicitly for Word documents
         oMsg.MessageClass = "IPM.Document.Word.Document.8"
      End If
   End If
   Unload Me
   Kill strFilePath
End Sub

Previous   |  Table of Contents   |   Next

Top of Page

Last Updated: Saturday, July 7, 2001