VBA to switch Outlook online/offline

Jo888

Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Most of my tasks are done via Outlook ie most of the work I do is composing and responding to emails.

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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Outlook's object model doesn't expose that but you can use ExecuteMso. The status bar will show its offline - the button will if you switch to that ribbon, but it won't update until the ribbon is refreshed. Call the macro using task reminders - Running Outlook Macros on a Schedule

Code:
Sub ToggleOffline()
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 Sub
Or you can use Redemption:
Running Outlook Macros on a Schedule
RDO - Session
 

Jo888

Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Fantastic Diane - that is really useful.

I don't necessarily need the ribbon to show, just the status bar, which I can confirm that this does.

The code you have provided is excellent. It toggles between online and offline.

I'm wondering is there is a way, rather than toggling, to switch online, or switch offline irrespective of current status. That way if I switch online in the middle of the day to get an urgent email out, I don't need to remember to switch it offline.

I could then use one set of tasks to run one macro to schedule online status, and another set of tasks to schedule offline status.
 

Jo888

Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Just figured it Out

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
 

Jo888

Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I can now point my tasks to each respective status - thanks once again - Diane
 
Top