avoorheis
asked on
can not close excel
I am manipulating some cells in an excel spreadsheet, then saving it and then closing it. When I add a few additional commands (lines that are now commented out), excel stays open (I can see it in task manager) and I get a remote server error on the second pass.
Here's the code...let me know if you see anything or have suggestions, thanks
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryPCPrePostCompare", stPathTemp & stFileName
Set xl = CreateObject("Excel.Applic ation")
Set xlw = xl.Workbooks.Open(stPathTe mp & stFileName)
Set xls = xlw.Worksheets("qryPCPrePo stCompare" )
'xls.Range(stHeader, Range(stHeader).End(xlDown )).Select
'Selection.NumberFormat = "General"
'Selection.Value = Selection.Value
xls.Range(stHeader).Font.B old = True
xls.Range(stHeader).Border s(xlBottom ).Weight = xlMedium
xls.Range(stNumberCols).Co lumns.Numb erFormat = "#,##0"
xls.Range(stPercentCols).C olumns.Num berFormat = "#,##0%"
xls.Range(stCols).Columns. AutoFit
xls.rows.AutoFit
'xlw.Activate
'ActiveWindow.SplitColumn = 1
'ActiveWindow.SplitRow = 1
'ActiveWindow.FreezePanes = True
xl.DisplayAlerts = False
xlw.SaveAs stPathTemp & stFileName
xl.DisplayAlerts = True
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
Here's the code...let me know if you see anything or have suggestions, thanks
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryPCPrePostCompare", stPathTemp & stFileName
Set xl = CreateObject("Excel.Applic
Set xlw = xl.Workbooks.Open(stPathTe
Set xls = xlw.Worksheets("qryPCPrePo
'xls.Range(stHeader, Range(stHeader).End(xlDown
'Selection.NumberFormat = "General"
'Selection.Value = Selection.Value
xls.Range(stHeader).Font.B
xls.Range(stHeader).Border
xls.Range(stNumberCols).Co
xls.Range(stPercentCols).C
xls.Range(stCols).Columns.
xls.rows.AutoFit
'xlw.Activate
'ActiveWindow.SplitColumn = 1
'ActiveWindow.SplitRow = 1
'ActiveWindow.FreezePanes = True
xl.DisplayAlerts = False
xlw.SaveAs stPathTemp & stFileName
xl.DisplayAlerts = True
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
sorry, xlw.close
i tested this:
xl.DisplayAlerts = False
xlw.SaveAs stPathTemp & stFileName
xl.DisplayAlerts = True
xl.Visible = True
xlw.Close
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
and it kills excel properly
xl.DisplayAlerts = False
xlw.SaveAs stPathTemp & stFileName
xl.DisplayAlerts = True
xl.Visible = True
xlw.Close
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
and it kills excel properly
ASKER
Thanks Sudonim,
I've tried xlw.close already and it didn't help, but, I'll try again.
I have several routines where I open an excel spreadsheet load/manipulate data and close, never any problems. The only thing different about this code seems to be the Activewindow. and Selection.
Even when I have problems, which appear as a "remote server" error and I can see an instance of Excel in the task manager, the actual file I have been using is closed properly (in other words, I can open it without getting a sharing violation).
I'll work on this over the next few days and provide more details.
I've tried xlw.close already and it didn't help, but, I'll try again.
I have several routines where I open an excel spreadsheet load/manipulate data and close, never any problems. The only thing different about this code seems to be the Activewindow. and Selection.
Even when I have problems, which appear as a "remote server" error and I can see an instance of Excel in the task manager, the actual file I have been using is closed properly (in other words, I can open it without getting a sharing violation).
I'll work on this over the next few days and provide more details.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It appears there are 2 or 3 lines that will cause some sort of problem, so, I'll start with the first:
xls.Range(stHeader, Range(stHeader).End(xlDown )).Select
When this line is not commented out, an instance of excel (seen in Task mgr) stays active (not a problem if commented out). I did set xl.visible and see the spreadsheet opens and closes, but, there is still an instance in tsk mgr.
So, I don't think the way I open and close the spreadsheet is wrong, as it does work without the above line of code, but, there is something with the above code that is causing a problem...maybe starting another instance of excel?
xls.Range(stHeader, Range(stHeader).End(xlDown
When this line is not commented out, an instance of excel (seen in Task mgr) stays active (not a problem if commented out). I did set xl.visible and see the spreadsheet opens and closes, but, there is still an instance in tsk mgr.
So, I don't think the way I open and close the spreadsheet is wrong, as it does work without the above line of code, but, there is something with the above code that is causing a problem...maybe starting another instance of excel?
ASKER
after some more testing, I've come down to this...I'll restate the problem to hopefully make it easier to isoate what I'm doing wrong:
Here is one of the line that's causing a problem. The application and worksheet do close, but, there is still an instance of excel in taskmanager. There is only one instance that ever appears. I've commented out all the other code and am just running the stuff below (except for the error handling and some definitions, but, there are not errors).
Dim xlw As Excel.Workbook
Dim xl As Excel.Application
Dim xls As Excel.Worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryPCPrePostCompare", stPath & stFileName
Set xl = CreateObject("Excel.Applic ation")
Set xlw = xl.Workbooks.Open(stPath & stFileName)
Set xls = xlw.Worksheets("qryPCPrePo stCompare" )
xls.Range(stHeader, Range(stHeader).End(xlDown )).Select <<<<< if I comment this out, no problems
xl.DisplayAlerts = False
xlw.SaveAs stPath & stFileName
xl.DisplayAlerts = True
xlw.Close
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
So, an instance of excel is still visible in task mgr. When I close Access it goes away.
Here is one of the line that's causing a problem. The application and worksheet do close, but, there is still an instance of excel in taskmanager. There is only one instance that ever appears. I've commented out all the other code and am just running the stuff below (except for the error handling and some definitions, but, there are not errors).
Dim xlw As Excel.Workbook
Dim xl As Excel.Application
Dim xls As Excel.Worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryPCPrePostCompare", stPath & stFileName
Set xl = CreateObject("Excel.Applic
Set xlw = xl.Workbooks.Open(stPath & stFileName)
Set xls = xlw.Worksheets("qryPCPrePo
xls.Range(stHeader, Range(stHeader).End(xlDown
xl.DisplayAlerts = False
xlw.SaveAs stPath & stFileName
xl.DisplayAlerts = True
xlw.Close
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing
So, an instance of excel is still visible in task mgr. When I close Access it goes away.
ASKER
found the problems:
xls.Range(stHeader, Range(stHeader).End(xlDown )).Select
should be
xls.Range(stHeader, xls.Range(stHeader).End(xl Down)).Sel ect
Selection.Value = Selection.Value
should be
xl.Selection.Value = xl.Selection.Value
ActiveWindow.SplitColumn = 1
should be
xl.ActiveWindow.SplitColum n = 1
etc, etc
but, you guys help, and I appreciate it.
xls.Range(stHeader, Range(stHeader).End(xlDown
should be
xls.Range(stHeader, xls.Range(stHeader).End(xl
Selection.Value = Selection.Value
should be
xl.Selection.Value = xl.Selection.Value
ActiveWindow.SplitColumn = 1
should be
xl.ActiveWindow.SplitColum
etc, etc
but, you guys help, and I appreciate it.
avoorheis,
you are experiencing a common occurence associated with Office Automation known as "unmanaged instances". This link explains the problem and offers a tentative solution.
http://vbcity.com/forums/topic.asp?tid=76537
you are experiencing a common occurence associated with Office Automation known as "unmanaged instances". This link explains the problem and offers a tentative solution.
http://vbcity.com/forums/topic.asp?tid=76537
nice one.
been off the pc ill with a virus.
nice one on finding it.
instead of the xl_select down thingy,(xls.Range(stHeader , Range(stHeader).End(xlDown )).Select) have a look at worksheet.usedrange.select :-P
might be xlsusedrange.select in your ap.
been off the pc ill with a virus.
nice one on finding it.
instead of the xl_select down thingy,(xls.Range(stHeader
might be xlsusedrange.select in your ap.
xl.Quit
Set xls = Nothing
Set xlw = Nothing
Set xl = Nothing