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
- Open Excel and press
Alt + F11to open the Visual Basic for Applications editor. - 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.
- 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
- 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.
- Close the VBA editor and return to Excel.
- Press
Alt + F8to open the Macro dialog box, select CloseOutlook, then click Run. - 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 ofGetObject—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—
GetObjectwill returnNothing, so theQuitcommand does nothing. This is expected behavior and not an error. - Forgetting to set the object to
Nothingafter 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 Nextto prevent runtime errors if Outlook isn’t running—it’s safer than letting the macro crash. - Always use
Set olApp = NothingafterQuitto 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
CloseOutlooksubroutine 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 GoToif 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
CreateObjectinstead ofGetObject. Replace it withGetObject(, "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
Quitto verify execution:MsgBox "Closing Outlook..."before andMsgBox "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. TheGetObject 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. TheGetObject 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 usingCreateObject("Outlook.Application"). This approach gives you full control over lifecycle.