Excel VBA can automate tasks across Microsoft Office applications, including closing Outlook. This is useful when you’ve sent emails via VBA and want to ensure Outlook shuts down cleanly to free up system resources or avoid multiple instances running. This method does not close Outlook if it was already open before your macro ran—only if your code opened it. You must have Outlook installed and properly configured on the same machine as Excel.

What You'll Need

  • Microsoft Excel with VBA enabled (Excel 2010 or later)
  • Microsoft Outlook installed and configured on the same computer
  • Basic knowledge of VBA editor (Alt+F11)
  • Outlook must be running or accessible via COM automation

Step-by-Step

  1. Open Excel and press Alt + F11 to open the Visual Basic for Applications editor.
  2. In the Project Explorer on the left, right-click your workbook’s name (e.g., VBAProject (Book1)) and choose Insert > Module to create a new code module.
  3. Copy and paste the following VBA code into the new module:
Sub CloseOutlook()
    Dim olApp As Object
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Not olApp Is Nothing Then
        olApp.Quit
        Set olApp = Nothing
    End If
End Sub
  1. Save your Excel file as a macro-enabled workbook (.xlsm) by clicking File > Save As, then select Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
  2. Close the VBA editor and return to Excel.
  3. Press Alt + F8 to open the Macro dialog box, select CloseOutlook, then click Run.
  4. Wait a few seconds. Outlook will close if it was opened by your VBA code or was running and accessible via COM.

Common Mistakes

  • Using CreateObject("Outlook.Application") instead of GetObject—this creates a new instance, which may not be the one you want to close and can cause multiple Outlook windows to appear.
  • Running the code when Outlook is not running—GetObject will return Nothing, so the Quit command does nothing. This is expected behavior and not an error.
  • Forgetting to set the object to Nothing after quitting, which can leave reference handles dangling in memory.
  • Assuming the code will close Outlook if it was opened manually by the user. It will, but only if the COM object can bind to it. If Outlook is locked or unresponsive, the connection may fail.
  • Running the macro on a system where Outlook is not installed or is not registered properly in the Windows COM registry.

Tips

  • Use On Error Resume Next to prevent runtime errors if Outlook isn’t running—it’s safer than letting the macro crash.
  • Always use Set olApp = Nothing after Quit to release memory and avoid memory leaks in long-running automation scripts.
  • If you’re sending emails via VBA and want to close Outlook afterward, place the CloseOutlook subroutine right after your email-sending code.
  • Test the macro with Outlook manually closed first, then again after launching Outlook normally to confirm it works both ways.
  • Consider wrapping the CloseOutlook subroutine in a Try-Catch style structure using On Error GoTo if you need detailed logging or user notifications.
  • For enterprise environments, ensure group policies don’t block COM automation between Office apps.

Troubleshooting

  • Outlook doesn’t close—First, check if Outlook is running. Open Task Manager and look for outlook.exe. If it’s there but not closing, restart Excel and try again. If it still doesn’t close, Outlook may be busy processing a large email or add-in. Wait a minute and try manually closing Outlook, then re-run the macro.
  • Run-time error '429': ActiveX component can't create object—This means Outlook isn’t registered with COM. Repair your Office installation via Windows Settings > Apps > Microsoft Office > Modify > Quick Repair, then reboot.
  • Macro is disabled—Excel may block macros by default. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose Enable all macros temporarily for testing (revert after).
  • Outlook opens but doesn’t close—You may have used CreateObject instead of GetObject. Replace it with GetObject(, "Outlook.Application"). The comma is critical—it tells VBA to connect to a running instance.
  • Permission denied—Antivirus or corporate security software may block automation. Temporarily disable real-time scanning or contact your IT department.
  • Code runs but nothing happens—Add a message box before and after Quit to verify execution: MsgBox "Closing Outlook..." before and MsgBox "Outlook closed" after. If the first appears but not the second, Outlook is unresponsive.

Frequently Asked Questions

Can this code close Outlook if it was opened by another program?

Yes, as long as Outlook is running and accessible via COM automation. The GetObject method connects to any active Outlook instance, regardless of how it was launched.

Will this close Outlook even if I have unsaved drafts or emails open?

Yes, it will force close Outlook, potentially losing unsaved drafts. Always ensure emails are sent or saved before running this macro.

Does this work on Mac?

No. VBA on macOS does not support COM automation to Outlook the same way Windows does. This method only works on Windows.

What if I have multiple Outlook profiles? Will it close the right one?

Yes. The GetObject method connects to the default profile currently running. If multiple profiles are open simultaneously (rare), it still targets the active COM instance.

Can I close Outlook without using VBA?

Yes—manually click the X button, or use Task Manager to end the outlook.exe process. But VBA automates this for batch scripts or scheduled tasks.

Why doesn’t Outlook reopen automatically after I close it with VBA?

It won’t. VBA only closes the instance. To reopen it, you’d need separate code using CreateObject("Outlook.Application"). This approach gives you full control over lifecycle.