A user wanted to set Outlook to offline/online mode automatically depending on network connection, using either PowerShell or VBScript.
The VBA script at VBA to switch Outlook online/offline doesn't meet the user's need because it is not controlled externally. While Outlook doesn't have a timer, Task or Meeting reminders can trigger a macro.
The VBScript or PowerShell versions can be triggered by Task Scheduler. While the VBA version isn't all that efficient - it's one click to set Outlook offline yourself, it could be triggered using a reminder.
Set Outlook Offline
Paste this script into notepad and save with the .vbs extension. Double-click to run it.
VBScript version
Dim appOutlook Dim blnIsOffline On Error Resume Next Set appOutlook = CreateObject("Outlook.Application") If Err.Number = 0 Then blnIsOffline = appOutlook.Session.Offline If Not blnIsOffline Then appOutlook.ActiveExplorer.CommandBars.ExecuteMso ("ToggleOnline") End If Else End If If Not (appOutlook Is Nothing) Then Set appOutlook = Nothing
PowerShell version
This checks to see if Outlook is offline, if not, it sets it offline. To set offline mode if it not offline, the toggle goes in the Else
To use this, paste into Notepad then save the script with the .ps1 extension. You can create a task in Task Scheduler or right-click on the ps1 and choose Run with PowerShell.
$outlook = New-Object -ComObject Outlook.Application if ($outlook.session.offline) { write-host "Outlook has been set to work offline mode." ($outlook.ActiveExplorer()).CommandBars.ExecuteMso("ToggleOnline") } else { write-host "Outlook is in online mode" #($outlook.ActiveExplorer()).CommandBars.ExecuteMso("ToggleOnline") }
VBA macro version
Paste this code into Outlook's VB Editor.
Sub SetOfflineMode() Dim appOutlook Dim blnIsOffline On Error Resume Next Set appOutlook = CreateObject("Outlook.Application") If Err.Number = 0 Then blnIsOffline = appOutlook.Session.Offline If Not blnIsOffline Then appOutlook.ActiveExplorer.CommandBars.ExecuteMso ("ToggleOnline") End If Else End If If Not (appOutlook Is Nothing) Then Set appOutlook = Nothing End Sub
Set Outlook Online
VBScript version
Dim appOutlook Dim blnIsOffline On Error Resume Next Set appOutlook = CreateObject("Outlook.Application") If Err.Number = 0 Then blnIsOffline = appOutlook.Session.Offline If blnIsOffline Then appOutlook.ActiveExplorer.CommandBars.ExecuteMso ("ToggleOnline") End If Else End If If Not (appOutlook Is Nothing) Then Set appOutlook = Nothing
PowerShell version
This checks to see if Outlook is offline, if so, sets it online. Remove tyhe X to uncomment the second line and it will toggle Outlook on or offline.
$outlook = New-Object -ComObject Outlook.Application if ($outlook.session.offline) { write-host "Outlook has been set to work offline mode." ($outlook.ActiveExplorer()).CommandBars.ExecuteMso("ToggleOnline") } else { write-host "Outlook is in online mode" #($outlook.ActiveExplorer()).CommandBars.ExecuteMso("ToggleOnline") }
VBA macro version
Thie checks the status and if offline, goes online.
Sub SetOnlineMode() Dim appOutlook Dim blnIsOffline On Error Resume Next Set appOutlook = CreateObject("Outlook.Application") If Err.Number = 0 Then blnIsOffline = appOutlook.Session.Offline If blnIsOffline Then appOutlook.ActiveExplorer.CommandBars.ExecuteMso ("ToggleOnline") End If Else End If If Not (appOutlook Is Nothing) Then Set appOutlook = Nothing End Sub
How to use PowerShell version
Type PowerShell on the start menu. For editing and testing PowerShell scripts, open Windows PowerShell ISE.
If you don't have the white editor section at the top, click the Show Script Pane button.
Paste the PowerShell script in the window, edit it as needed then click the Run button. Errors (if any) will be in the bottom.
Save the script with a .ps1 extension. You can right-click on it and choose Run using PowerShell.
If you receive an error message "File C:\path\to\script-name.ps1 cannot be loaded because running scripts is disabled on this system.", you need to change the execution policy.
To change the execution policy in PowerShell start PowerShell in administrator mode and run this command. Enter Y to allow it to change the policy.
Set-ExecutionPolicy RemoteSigned
How to use the VBA macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor