Microsoft KB Archive/200333

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

Article ID: 200333

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 98 Standard Edition

-



This article was previously published under Q200333





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 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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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:

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

