Home > Blockchain >  Outlook 2003: Connect IMAP server with VBA
Outlook 2003: Connect IMAP server with VBA

Time:03-31

Sometimes Outlook (2003) loses the connection to one or more IMAP server. With VBA scripts that are supposed to move mails to these mailboxes, for example, I get this error message:

"Runtime error '-972759285 (c604df0b)': Connection to server is unavailable. Outlook must be online or connected to complete this operation."

I then first have to click on "File" - "Connect to [MAILBOX...]" to establish this connection manually. I am looking for a VBA solution to automatically connect to multiple mailboxes (IMAP only), but I don't know what to look for in VBA references.

I tried this:

Sub MyTest()

    Dim myNameSpace As Outlook.NameSpace
    Set myNameSpace = Application.GetNamespace("MAPI")

    Set Application.ActiveExplorer.CurrentFolder = myNameSpace.Folders("C-Interessenten").Folders("Interessenten")
    
    Set myNameSpace = Nothing

End Sub

or this

Sub IsOLOffline()

'Determines whether Outlook is currently offline.
    
    Dim myOlApp     As Outlook.Application
    Dim myNameSpace As Outlook.NameSpace

    Set myOlApp = New Outlook.Application
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    
    Debug.Print myNameSpace.Offline

End Sub

Thank you for an idea.

CodePudding user response:

The Outlook object model doesn't provide anything for that out of the box.

The Offline property of the Namespace class returns valid information only for an Exchange profile. It's not intended for non-Exchange account types such as POP3, IMAPI, and HTTP.

You may try to use SyncObjects property of the Namespace class. It returns a set of SyncObject objects representing the Send/Receive groups for a user.

The OnError event is fired when Microsoft Outlook encounters an error while synchronizing a user's folders using the specified Send\Receive group. So, it could help with detecting such cases and then initiating a new sync.

Public WithEvents mySync As Outlook.SyncObject 
 
Sub Initialize_handler() 
 Set mySync = Application.Session.SyncObjects.Item(1) 
 mySync.Start 
 mySync.Stop 
End Sub 
 
Private Sub mySync_OnError(ByVal Code As Long, ByVal Description As String) 
 MsgBox "Unexpected sync error" & Code & ": " & Description 
End Sub

A Send\Receive group lets users configure different synchronization scenarios, selecting which folders and which filters apply.

Use the Item method to retrieve the SyncObject object from a SyncObjects object. Because the Name property is the default property of the SyncObject object, you can identify the group by name.

CodePudding user response:

Thank you for the food for thought. I have already experimented a bit by creating a separate group for each IMAP mailbox. Testing is taking a long time because the connections only break sporadically and I can't trigger the break manually. Nevertheless, I have more and more the impression that the SyncObject does not lead to a solution. Thanks anyway. Is there perhaps the possibility to call the menu item "File" - "Connect to xxx" via vba, for example via FindControl()?

  • Related