Word VBA to Close Excel Application

sfpowell

Board Regular
Joined
Mar 30, 2009
Messages
82
I'm using Windows 7 with MS Office 2007 and I have written some VBA code inside MS Word 2007 that performs a Mail Merge with an Excel Workbook. When the program is complete I want the code to close the Excel Application. The code
Code:
Excel.Application.ActiveWorkbook.Close False
Excel.Application.Quit
seems to work when Excel was originally opened by the program but it doesn't seem to work when Excel is already open.
Can anyone tell me how I can have the program check to see if Excel is open and if it is, close it?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try something like this. I'm using early binding of the Excel objects, so you need a reference to MS Excel Object library in your Word VBA project.
Code:
Sub Close_Excel()

    Dim Excel As Excel.Application
    Dim ExcelOpened As Boolean
    
    ExcelOpened = False
    On Error Resume Next
    Set Excel = GetObject(, "Excel.Application")
    If Excel Is Nothing Then
        Set Excel = New Excel.Application
        ExcelOpened = True
    End If
    On Error GoTo 0

    With Excel
        If ExcelOpened Then
            .Visible = True
            .Workbooks.Add
        End If
        .ActiveWorkbook.Close False
        .Quit
    End With
    
End Sub
 
Upvote 0
It worked! It's crazy some of the things you have to do to accomplish the simplest of tasks.
Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top