VBA to close extra instance of Excel running

sagain2k

Board Regular
Joined
Sep 8, 2002
Messages
94
I have a button on an Access switchboard form that when clicked runs a query that outputs the results into an Excel workbook called Courses.xls, replacing the previous file of the same name (saving the result).

What it's doing is creating a whole new instance of Excel running, despite the fact that Excel was already open. This creates problems for me since Excel macros that need to be run that belong to a workbook from the first occurrence Excel aren't able to switch to the Courses.xls workbook since it "belongs" to a diffrent occurence of Excel.

What code can I add to this so it will either close Excel after creating this Courses.xls workbook, or have it be able to create the workbook without having to open up a new occurrence of Excel? Any ideas welcome...thanks!

Here's the code:

Private Sub cmdCourses_Click()
On Error GoTo Err_cmdCourses_Click

'THIS CREATES THE COURSES.XLS FILE AND REPLACES THE OLD FILE
'BUT OPENS A NEW OCCURRENCE OF EXCEL
DoCmd.OutputTo acQuery, "qryCourses", "MicrosoftExcel(*.xls)", _
"q:\qryResults\Courses.xls", True, ""

Exit_cmdCourses_Click:
Exit Sub

Err_cmdCourses_Click:
MsgBox Err.Description
Resume Exit_cmdCourses_Click
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Why not use the TransferSpreadsheet method instead of OutputTo to export the query?

ASFAIK it won't open Excel.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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