OL98: How to Programmatically Determine If Outlook Is Online or Offline

ID: Q200333


The information in this article applies to:
  • Microsoft Outlook 98


SUMMARY

This article provides methods you can use to programmatically determine whether Outlook is online or offline.


MORE INFORMATION

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 Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
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 in 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 approach 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:
Q181035 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 


REFERENCES

For additional information about how to obtain CDO, click the article number below to view the article in the Microsoft Knowledge Base:

Q171440 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:
Q180826 OL98: Resources for Custom Forms and Programming
Q182349 OL98: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol98

Keywords : kbOutlookObj
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 11, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.