We help IT Professionals succeed at work.

Access controlling Excel

I have an Access app that dumps out excel data files, I need excel open for part 1 of the process but for the dump of the file I need to close excel.  I can not seem to figure out the Access VBA to determine if an Excel file is open and close the open excel file dump the excel files and then code to reopen that excel file for the file process
Comment
Watch Question

You might check below thread out:
http://www.cpearson.com/excel/IsFileOpen.aspx

I am sure there are plenty of more simular functions like this.

Hope this helps,
Daniel
Check also this thread by Ron de Bruin, scroll down at part "Test if file is open":
http://www.rondebruin.nl/exist.htm
Commented:
Const ERR_APP_NOTRUNNING As Long = 429
dim oExcel as Object

    Set oExcel = GetObject(, "Excel.Application")
    If Err = ERR_APP_NOTRUNNING Then
       Set oExcel = New Excel.Application
    End If

That will get you a handle to Excel (if it isn't running a new copy otherwise the running copy)
Then you can do stuff like


    Dim wb As Excel.Workbook
    For Each wb In oExcel.Workbooks
        wb.Close True
    Next
 
to close and save all open workbooks

to open a workbook

set wb = oExcel.Workbooks.Open MyFilename

to open a blank workbook

set wb = oExcel.Workbooks.Add

Author

Commented:
How can I close a particuliar Excel workbook, in case the user has multiple open.  I need only to close 1 workbook that is locking linked excel files that I need to update

Commented:
oExcel.Workbooks("WorkbookName").close

Author

Commented:
I get am error on

Dim wb As Excel.Workbook

User-defined type not defined

Commented:
you have to have a reference to excel on your references (under tools in the vba editor) to do that, but if you do

dim wb as Object

that will work too.