G
GrannyM
I have a word macro that opens an xml file in Excel, then saves it as a xls
file that the word macro pulls information from. Everything works great
unless for some reason the user aborts the macro before it has closed the xls
file. Then it leaves a hidden copy of excel with this xls file open. So the
user cannot rerun the macro until they either reboot, or ctr+alt+delete and
end the hidden copy of excel (which I really do not want them to do!). I'm
trying to write an error trap so that if this happens, the word macro can
close down any existing hidden copies of excel, but so far I haven't been
able to figure out how.
My code for opening the file is:
Set myExcel = GetObject(, "Excel.application")
'Check to see if File1.xls is already open
For Each w In myExcel.Workbooks
If LCase(myExcel.ActiveWorkbook.FullName) = "i:\file1.xls" Then
myExcel.ActiveWorkbook.Close
Exit For
End If
Next w
'Open Excel File
OpenExcel:
myExcel.Application.DisplayAlerts = False
myExcel.Workbooks.OpenXML FileName:=FileXML$,
LoadOption:=xlXmlLoadImportToList
myExcel.ActiveSheet.ListObjects("List1").Unlist
myExcel.ActiveWorkbook.SaveAs FileName:="i:\File1.XLS"
Set myXML = myExcel.ActiveWorkbook
myExcel.Application.DisplayAlerts = True
My Error code is below - the "ElseIf Err.Number = 1004 Then" piece is the
piece that does not work. Instead of finding the hidden copy of excel, it
seems to open a new hidden copy without any worksheets. (I inserted
"myExcel.visible = True" to see if I could figure out what was happening)
ErrHandle:
If Err.Number = errExcelNotRunning Then
Set myExcel = CreateObject("Excel.application")
CloseXL = True
Resume Next
ElseIf Err.Number = 1004 Then '(File1.xls is open in hidden Excel)
Set myExcel = GetObject(, "Excel.application")
If myExcel.Visible = False Then
myExcel.Quit
GoTo OpenExcel
End If
Resume
Else
MsgBox Err.Number & ": " & Err.Description
Abort = True
End If
Any ideas on how I can find this hidden copy of Excel that has been left
open and close it?
Thanks
file that the word macro pulls information from. Everything works great
unless for some reason the user aborts the macro before it has closed the xls
file. Then it leaves a hidden copy of excel with this xls file open. So the
user cannot rerun the macro until they either reboot, or ctr+alt+delete and
end the hidden copy of excel (which I really do not want them to do!). I'm
trying to write an error trap so that if this happens, the word macro can
close down any existing hidden copies of excel, but so far I haven't been
able to figure out how.
My code for opening the file is:
Set myExcel = GetObject(, "Excel.application")
'Check to see if File1.xls is already open
For Each w In myExcel.Workbooks
If LCase(myExcel.ActiveWorkbook.FullName) = "i:\file1.xls" Then
myExcel.ActiveWorkbook.Close
Exit For
End If
Next w
'Open Excel File
OpenExcel:
myExcel.Application.DisplayAlerts = False
myExcel.Workbooks.OpenXML FileName:=FileXML$,
LoadOption:=xlXmlLoadImportToList
myExcel.ActiveSheet.ListObjects("List1").Unlist
myExcel.ActiveWorkbook.SaveAs FileName:="i:\File1.XLS"
Set myXML = myExcel.ActiveWorkbook
myExcel.Application.DisplayAlerts = True
My Error code is below - the "ElseIf Err.Number = 1004 Then" piece is the
piece that does not work. Instead of finding the hidden copy of excel, it
seems to open a new hidden copy without any worksheets. (I inserted
"myExcel.visible = True" to see if I could figure out what was happening)
ErrHandle:
If Err.Number = errExcelNotRunning Then
Set myExcel = CreateObject("Excel.application")
CloseXL = True
Resume Next
ElseIf Err.Number = 1004 Then '(File1.xls is open in hidden Excel)
Set myExcel = GetObject(, "Excel.application")
If myExcel.Visible = False Then
myExcel.Quit
GoTo OpenExcel
End If
Resume
Else
MsgBox Err.Number & ": " & Err.Description
Abort = True
End If
Any ideas on how I can find this hidden copy of Excel that has been left
open and close it?
Thanks