'Automating Outlook offline / online

Most of my tasks are done via Outlook 2016 ie most of the work I do is composing and responding to emails. If I don't switch Outlook offline, I get distracted from the constant inflow. And often times, within an hour or so I don't need to respond because the task has been dealt with.

I can switch Outlook offline /online manually, but I was wondering if there was a Macro that would automate this.

My schedule is as follows

7am Online 8:45 Offline 10:45 Online 10:50 Offline 12:45 Online 12:50 Offline 14:30 Online 14:35 Offline 15:55 Online 16:00 Offline 16:55 Online 17:00 Offline

with the ability to manually override?

Is this possible?

I can do basic Excel macros, but I've never made an Outlook macro



Solution 1:[1]

Thanks to Diane at Slipstick , and a little bit of my own work, I've found a solution. See https://forums.slipstick.com/threads/95555-vba-to-switch-outlook-online-offline/

There are three sets of macros - one to switch offline, one to switch online, and then a macro to assign a task for the scheduling. The first two macros are here, the third you can find in the forum reference above.

Hope this helps someone

Sub SetOffline()

Dim oNS As NameSpace
Set oNS = Application.Session

If oNS.ExchangeConnectionMode <> olCachedOffline And _
oNS.ExchangeConnectionMode <> olOffline Then

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim objExpl As Outlook.Explorer
Set olApp = Application
Set olNS = olApp.GetNamespace("MAPI")
Set objExpl = olApp.ActiveExplorer

objExpl.CommandBars.ExecuteMso ("ToggleOnline")

End If

End Sub

and

Sub OnlineStatus()
Dim oNS As NameSpace
Set oNS = Application.Session

If oNS.ExchangeConnectionMode = olOnline

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim objExpl As Outlook.Explorer
Set olApp = Application
Set olNS = olApp.GetNamespace("MAPI")
Set objExpl = olApp.ActiveExplorer

objExpl.CommandBars.ExecuteMso ("ToggleOnline")

End If
End Sub

Solution 2:[2]

In Extended MAPI (C++ or Delphi) - you can use IMAPIOfflineMgr::SetCurrentState.
In case of other languages besides C++ or Delphi, you can use Redemption (I am its author) and its RDOSession.Offline property:

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
Session.Offline = true

Keep in mind however that IMAPIOfflineMsg interface cannot be marshaled between different processes (e.g. outlook.exe and excel.exe), so neither IMAPIOfflineMgr nor RDOSession.Offline would work from Excel VBA. You can try to simulate a click on the "Work Offline" button using either Accessibility API or, again, using Redemption and its SafeExplorer object:

set sExplorer = CreateObject("Redemption.SafeExplorer")
sExplorer.Item = Application.ActiveExplorer
set Ribbon =  sExplorer.Ribbon
oldActiveTab = Ribbon.ActiveTab
Ribbon.ActiveTab = "Send / Receive"
set Control = Ribbon.Controls("Work Offline")
Control.Execute
Ribbon.ActiveTab = oldActiveTab 'restore the active tab

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jo8888
Solution 2