VBA not closing Excel workbook

P

Pendragon

Access03/WinXP

(Probems with initial post - my apologies if this is duplicated)

I am using what seems to be a standard process to export a query to Excel.
The code below is the first part of an IF...THEN where it is true that Excel
is open.

Excel is open and other workbooks are open. I need this part of the code to
write the data to Excel and then close the workbook but leave open Excel and
the other workbooks. The current workbook (the exported data) is not being
closed.

Dim I As Long
Dim objExcel As Excel.Application

stDocName = "c:\datafiles\qryClassRegistrations.xls"

If fIsAppRunning("Excel") Then
Set objExcel = GetObject(, "Excel.Application")
booXL = False
If fIsFileOpen(stDocName) = True Then
With objExcel
.Workbooks(1).Close False, , False
End With
End If
If fIsFileDIR(stDocName, 1) = -1 Then
Kill stDocName
End If
With objExcel
.Workbooks.Add
.ActiveSheet.Name = CStr("Registrations")
For I = 0 To rsExport.Fields.Count - 1
.cells(1, I + 1) = "'" & rsExport.Fields(I).Name
Next I
.Range(.cells(1, 1), .cells(1, rsExport.Fields.Count)).Font.Bold =
True
.Range(.cells(1, 1), .cells(1, rsExport.Fields.Count)).Columns.AutoFit
.selection.Range("A2").CopyFromRecordset rsExport
.Workbooks(1).SaveAs stDocName
.Workbooks(1).Close False, , False
End With
ELSE
.......

The line .Workbooks(1).Close False,,False is not closing the active workbook
qryClassRegistrations.xls.

Any help is much appreciated!!
 
P

Pendragon

Okay, I've just noticed that it's not the Close function that's not working,
it's the SaveAs. When this process is run, the data is being input to
Book(I) where (I) is the index, i.e., Book3. Therefore, the Close function
is trying to close a workbook that doesn't exist, I guess.

Any comments on the code would be appreciated. Something is wrong somewhere.
 

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