Excel Error Closing File from Access

B

BarryM

In Access 97 I want to open some Excel files at the same time without
creating more than one instance of Excel.

The following code works but it causes Excel to close if an individual file
is closed, but not if Excel is closed. The error message is “Excel has caused
an error in EXCEL.EXE. Excel will now close.†Any suggestions?

Many thanks in anticipation.

Public Sub GetExcelFile(strDoc As String)
On Error GoTo ErrDocs
Dim MyXL As Object

'Open Excel
Set MyXL = GetObject(, "Excel.Application") 'error if Excel not running
OpenDoc:
'Open the selected file
MyXL.Workbooks.Open strDoc
MyXL.Application.Visible = True

Exit Sub
ErrDocs:
Select Case Err
Case 429 'ActiveX component can't create object
'Excel not running, open it
Set MyXL = GetObject("", "Excel.Application")
GoTo OpenDoc
Case Else
MsgBox "Error " & Err & ": " & Error, 0 + 64, "Opening Excel Files"
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top