A
AnExpertNovice
I have weeded the code down to near minimal to demonstrate the problem.
Wanted:
To have Access Create an Excel Workbook, save it, (preferably not have to
close the Excel workbook, but that is a later issue), and have the Excel
process terminate without having to close the Access database.
The problem:
If a range is referenced in Access the Excel process does not terminate
until Access is terminated.
Demonstrating the problem:
The code below is a complete and self contained module. A very simple
module.
Executing it at is works fine. (It creates a Book1.xls in the default Excel
folder.)
In the second module are these 4 commented lines.
' Selection.Value = "test"
' With .Range(Cells(1, 1), Cells(1, 1))
'' .value = "test"
' End With
Uncommenting the line: Selection.Value = "test"
causes the Excel application to not terminate until Access is closed.
Uncommenting the two following lines (note, not even assigning a value)
' With .Range(Cells(1, 1), Cells(1, 1))
' End With
causes the Excel application to not terminate until Access is closed.
Note: I have not yet tried defining a range object and saying rngCurr =
..Range(Cells(1, 1), Cells(1, 1))
and then at some point setting rngCurr = Nothing.
============ The code is below
Option Compare Database
Option Explicit
Dim gbooAbort As Boolean
Sub test()
On Error GoTo ErrorRoutine
Dim xlApp As Excel.Application
Dim xlWkbk As Excel.Workbook
gbooAbort = False
'Excel has no process running per Taskmanager
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Add
xlApp.Visible = True
Set xlWkbk = xlApp.Workbooks(1)
Call test2(CLng(12345), xlApp, xlWkbk)
If gbooAbort Then
GoTo ExitRoutine
End If
xlWkbk.Save
ExitRoutine:
On Error Resume Next
xlWkbk.Close
Set xlWkbk = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Number & ": " & Err.Description
gbooAbort = True
Resume ExitRoutine
End Sub
Private Sub test2(lngValue As Long, xlApp As Excel.Application, xlWkbk As
Excel.Workbook)
On Error GoTo ErrorRoutine
'Name the worksheet
xlApp.ActiveSheet.Name = CStr(lngValue)
With xlApp.ActiveSheet
.Range("A2").Select
' Selection.Value = "test"
' With .Range(Cells(1, 1), Cells(1, 1))
'' .value = "test"
' End With
End With
ExitRoutine:
On Error Resume Next
Exit Sub
ErrorRoutine:
MsgBox Err.Number & ": " & Err.Description
gbooAbort = True
Resume ExitRoutine
End Sub
Wanted:
To have Access Create an Excel Workbook, save it, (preferably not have to
close the Excel workbook, but that is a later issue), and have the Excel
process terminate without having to close the Access database.
The problem:
If a range is referenced in Access the Excel process does not terminate
until Access is terminated.
Demonstrating the problem:
The code below is a complete and self contained module. A very simple
module.
Executing it at is works fine. (It creates a Book1.xls in the default Excel
folder.)
In the second module are these 4 commented lines.
' Selection.Value = "test"
' With .Range(Cells(1, 1), Cells(1, 1))
'' .value = "test"
' End With
Uncommenting the line: Selection.Value = "test"
causes the Excel application to not terminate until Access is closed.
Uncommenting the two following lines (note, not even assigning a value)
' With .Range(Cells(1, 1), Cells(1, 1))
' End With
causes the Excel application to not terminate until Access is closed.
Note: I have not yet tried defining a range object and saying rngCurr =
..Range(Cells(1, 1), Cells(1, 1))
and then at some point setting rngCurr = Nothing.
============ The code is below
Option Compare Database
Option Explicit
Dim gbooAbort As Boolean
Sub test()
On Error GoTo ErrorRoutine
Dim xlApp As Excel.Application
Dim xlWkbk As Excel.Workbook
gbooAbort = False
'Excel has no process running per Taskmanager
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Add
xlApp.Visible = True
Set xlWkbk = xlApp.Workbooks(1)
Call test2(CLng(12345), xlApp, xlWkbk)
If gbooAbort Then
GoTo ExitRoutine
End If
xlWkbk.Save
ExitRoutine:
On Error Resume Next
xlWkbk.Close
Set xlWkbk = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Number & ": " & Err.Description
gbooAbort = True
Resume ExitRoutine
End Sub
Private Sub test2(lngValue As Long, xlApp As Excel.Application, xlWkbk As
Excel.Workbook)
On Error GoTo ErrorRoutine
'Name the worksheet
xlApp.ActiveSheet.Name = CStr(lngValue)
With xlApp.ActiveSheet
.Range("A2").Select
' Selection.Value = "test"
' With .Range(Cells(1, 1), Cells(1, 1))
'' .value = "test"
' End With
End With
ExitRoutine:
On Error Resume Next
Exit Sub
ErrorRoutine:
MsgBox Err.Number & ": " & Err.Description
gbooAbort = True
Resume ExitRoutine
End Sub