Microsoft KB Archive/250402
Article ID: 250402
Article Last Modified on 1/27/2007
- Microsoft Outlook 97 Standard Edition
This article was previously published under Q250402
This article provides methods you can use to programmatically determine whether Outlook is online or offline.
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:
The Outlook object model does not provide a direct way to determine whether or not Outlook is online or offline. However, there are two common approaches that you can use to accomplish this.
Error Trap a Specific Public Folder
When Outlook is offline, you cannot reference public folders that are available while Outlook is online. If you attempt to do so, Outlook will generate a run-time error. Therefore you can use use error trapping in your code to determine whether or not Outlook is currently online. The key advantage of using this approach is that you do not need to use the Collaborative Data Object (CDO) object model, so you do not need to make sure that CDO is installed on all of the client computers. The disadvantage to using this approach is that it will only work in a Microsoft Exchange Server environment.
The following Visual Basic Scripting Edition (VBScript) function can be placed in a custom form. As implemented below, it is designed to run whenever an item is opened.
Sub Item_Open() MsgBox "Online = " & IsOutlookOnline End Sub Function IsOutlookOnline() Dim objNS Dim objPF Dim objAPF Dim objMyFolder Set objNS = Application.GetNamespace("MAPI") Set objPF = objNS.Folders("Public Folders") Set objAPF = objPF.Folders("All Public Folders") On Error Resume Next Err.Clear ' Try to reference a typical public folder Set objMyFolder = objAPF.Folders(1) If Err.Number <> 0 Then IsOutlookOnline = False Else IsOutlookOnline = True End If Set objMyFolder = Nothing Set objPF = Nothing Set objAPF = Nothing Set objNS = Nothing End Function
Use the CDO Object Library
You can use CDO to check the value of a MAPI property representing whether or not Outlook is online. The disadvantage to this apprroach is that you must ensure that the CDO object library is installed on all of the client computers on which this solution will run.
For additional information and an example of using CDO to determine the offline status, click the article number below to view the article in the Microsoft Knowledge Base:
181035 HOWTO: Determine If a CDO Session Is Online or Offline from VB
The following code sample can be used in an Outlook form.
Sub Item_Open() MsgBox "Online = " & IsOutlookOnline End Sub Function IsOutlookOnline() Dim objSession Dim objInfoStore Dim bolOffline Set objSession = CreateObject("MAPI.Session") ' Use the existing Outlook session objSession.Logon "", "", False, False Set objInfostore = objSession.GetInfoStore(objSession.Inbox.StoreID) ' Check if it's offline bolOffline = objInfostore.Fields(&H6632000B) 'PR_STORE_OFFLINE If bolOffline Then IsOutlookOnline = False Else IsOutlookOnline = True End If Set objInfoStore = Nothing Set objSession = Nothing End Function
For additional information about how to obtain CDO, click the article number below to view the article in the Microsoft Knowledge Base:
171440 INFO: Where to Acquire the Collaboration Data Objects Libraries
For additional information about creating solutions with Outlook, click the article numbers below to view the articles in the Microsoft Knowledge Base:
166368 OL97: How to Get Help Programming with Outlook
170783 OL97: Q&A: Questions About Customizing or Programming Outlook
Additional query words: OutSol OutSol97
Keywords: kbhowto kbprogramming KB250402