OL2000: How to Programmatically Synchronize Folders (201434)



The information in this article applies to:

  • Microsoft Outlook 2000

This article was previously published under Q201434

SUMMARY

This article describes how to use the Microsoft Outlook object model to synchronize folders. The programming examples in this article use Microsoft Visual Basic (VB) and Visual Basic Scripting Edition (VBScript) to perform this task.

MORE INFORMATION

The Outlook object model exposes objects and methods for programmatic access to synchronization. The SyncObjects collection contains the Quick Synchronization groups that have been created on the computer and provides a method to initiate synchronization.

The Outlook object model does not provide any support for programmatically creating these synchronization groups. You must create them by using the Outlook user interface. Before you use any of the following examples, make sure that you are using a profile that is set up to use offline folders.

To create a new quick synchronization group:
  1. Click the Tools menu, point to Synchronize, and then click Offline Folder Settings.
  2. Click the Quick Synchronization tab, and then click New.
  3. Type a name for the group, and then click OK.
  4. Click the name of the new group, and then click Choose Folders.
  5. Click to select the check box next to each folder that you want to include in the group, and then click OK.
The new synchronization group is available in the Outlook object model through the SyncObjects collection.

SyncObjects Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: IMPORTANT: SyncObjects is a new feature in the Outlook 2000 object model. If your solution must work with previous versions of Outlook, use the "CommandBars" approach that is explained in further detail below.

The following SyncObjects automation code demonstrates how to programmatically initiate synchronization by using the default SyncObject, "All Folders" (without quotation marks). For additional information about problems implementing the SyncObjects example from the Help files, click the article number below to view the article in the Microsoft Knowledge Base:

241917 OL2000: SyncObject.Start Causes Fatal Error in Outlook

This example is designed to run from the built-in "ThisOutlookSession" code module in the Outlook 2000 Visual Basic for Applications project. Note that the objSyncObject variable is dimensioned by using the WithEvents keyword which provides notification when synchronization has completed and allows the objects to be properly released from memory.
'General declarations
Dim colSyncObjects As Outlook.SyncObjects
Dim WithEvents objSyncObject As Outlook.SyncObject

Sub StartSync()
   Set colSyncObjects = Session.SyncObjects
   Set objSyncObject = colSyncObjects("All Folders")
   objSyncObject.Start
End Sub
				

CommandBars Examples

You can start synchronization by programmatically pushing the appropriate synchronization CommandBar button on the Tools menu.

Automation Example

Before you use the following automation code from a Visual Basic or Visual Basic for Applications project, create a reference to the Microsoft Outlook 9.0 Object Library.
Sub SynchronizeOutlookFolders()

   Dim outapp As Outlook.Application
   Dim olns As Outlook.NameSpace
   Dim MyInbox As Outlook.MAPIFolder
   Dim MyExplorer As Outlook.Explorer
   Dim MyMenu As CommandBar
   Dim MyCmd As CommandBarControl

   Set outapp = New Outlook.Application
   Set olns = outapp.GetNamespace("MAPI")
   Set MyInbox = olns.GetDefaultFolder(olFolderInbox)
   Set MyExplorer = MyInbox.GetExplorer
   ' Reference the Tools menu
   Set MyMenu = MyExplorer.CommandBars.Item("Tools")
   ' Reference the Synchronize All Folders command
   Set MyCmd = MyMenu.Controls("Synchronize").Controls("All Folders")
   ' Execute the Synchronize All Folders command
   MyCmd.Execute
   MyExplorer.Close

   Set MyCmd = Nothing
   Set MyMenu = Nothing
   Set MyExplorer = Nothing
   Set MyInbox = Nothing

End Sub
				

Script Example

The following example uses a custom Outlook form and script to synchronize Outlook folders:
  1. Open a new e-mail message form.
  2. On the Tools menu, point to Forms, and then click Design This Form.
  3. Click the P.2 tab on the form.
  4. Use the Control Toolbox to add a CommandButton control to the page.
  5. On the Form menu, click View Code. Type or paste the following code into the Script Editor window.
    Sub CommandButton1_Click()
       Set MyExplorer = Application.ActiveExplorer
       Set MyMenu = MyExplorer.CommandBars.Item("Tools")
       Set MyCmd = MyMenu.Controls("Synchronize").Controls("All Folders")
       MyCmd.Execute
       Set MyCmd = Nothing
       Set MyMenu = Nothing
       Set MyExplorer = Nothing
    End Sub
    					
  6. On the Form menu, click Run This Form.
  7. Click the custom button on the P.2 tab to run the code.

REFERENCES

For additional information about how to programmatically add a public folder to the Favorites folder and then set it for offline use, click the article number below to view the article in the Microsoft Knowledge Base:

263175 OL2000: How to Programmatically Set a Public Folder Offline

For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:

146636 OL2000: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:9/29/2003
Keywords:kbhowto KB201434