Closing Excel - is Windows API really necessary?

R

RzB

I have the following , very simple code to create/close
an excel spreadsheet. After executing this code I was
expecting to be able to open the spreadsheet manually.
However I can only do this if I first close the Access
db that this code is running in. I have spent all day
reading and re reading posts and KB Articles but still
can't get it to work. I have seen one post that indicates
using a Win API call as a last resort.

I am using Win XP Pro and Access/Excel 2002.

Is my code ok? Or am I doing summat wrong?

Dim objXlApp As Excel.Application
Dim objXlWkb As Excel.Workbook
Dim objXlSht As Excel.Worksheet

Set objXlApp = New Excel.Application
Set objXlWkb = objXlApp.Workbooks.Add
Set objXlSht = objXlWkb.Worksheets("Sheet1")

objXlSht.Range("A1") = "HelloWorld"

objXlWkb.SaveAs ("C:\Maplspreadsheets\RzB.xls")

objXlWkb.Close
objXlApp.Quit

Set objXlSht = Nothing
Set objXlWkb = Nothing
Set objXlApp = Nothing

Thanks,
Roy
 
K

Ken Snell

My experience is that you need to set to Nothing the objects that are
"within" EXCEL, such as workbooks and worksheets and ranges and such, before
you quit the application. Try this:

Dim objXlApp As Excel.Application
Dim objXlWkb As Excel.Workbook
Dim objXlSht As Excel.Worksheet

Set objXlApp = New Excel.Application
Set objXlWkb = objXlApp.Workbooks.Add
Set objXlSht = objXlWkb.Worksheets("Sheet1")

objXlSht.Range("A1") = "HelloWorld"

objXlWkb.SaveAs ("C:\Maplspreadsheets\RzB.xls")

Set objXlSht = Nothing
objXlWkb.Close
Set objXlWkb = Nothing
objXlApp.Quit
Set objXlApp = Nothing
 

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