Automation Problem with Access 97/Excel 97 
Author Message
 Automation Problem with Access 97/Excel 97

        Hi there --
        I have VBA code in Access 97 which is opening an Automation object reference to
Excel 97.  Even though I have code that is supposed to quit Excel and then set the
reference to nothing, I still have an instance of Excel listed as a running program
when I go into the Task Manager.  The only way I can get it to go away is by
physically closing Access.
        Here's the code I'm using.  I've tried various permutations but so far haven't
been successful.  Any suggestions would be appreciated.
        Thanks in advance,
        Stephen

Sub ExcelRpt()

On Error GoTo ErrHandler

Dim xlObj As Excel.Application
Err.Number = 0
On Error GoTo NotLoaded
Set xlObj = GetObject(, "Excel.Application")
NotLoaded:
If Err.Number = 429 Then
    Set xlObj = CreateObject("Excel.Application")
    intError = Err.Number
End If
xlObj.Visible = True

'Define the variable to hold the Excel file name and location.
strFileName = "C:\Audit\Audit.xls"

'Open the file and make it visible.
xlObj.Workbooks.Open strFileName

'Go to the Import spreadsheet.  Clear the old data and position the
'cell point at A2 for the new data.
Worksheets("Import").Activate
xlObj.Goto Worksheets("Import").Range("A2")
Set rng = ActiveCell.CurrentRegion
rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count).Select
Selection.Clear

Set db = CurrentDb()
'The strSQL statement string comes from a global variable
Set rs = db.OpenRecordset(strSQL)

'Insert the records into the spreadsheet.

'Get the total number of records in the query.
rs.MoveLast
rs.MoveFirst
intCount = rs.RecordCount

'Set the initial values for the row and column coordinates.
x = 2   'Initial row value
y = 1   'Initial column value

'Loop through all the fields in all the records, obtain the values, and
'insert them into the spreadsheet.
xlObj.DisplayAlerts = False
For y = 1 To (rs.Fields.Count - 1)
    For i = 1 To intCount
        xlObj.Worksheets("Import").Cells(x, y).Value = rs.Fields(y).Value
        If rs.EOF = False Then
            rs.MoveNext
        End If
        x = x + 1
    Next i
    x = 2
    rs.MoveFirst
Next y

'Enter the total number of records for this audit.
'xlObj.Range("AuditRecordCount").Value = (ActiveCell.CurrentRegion.Rows.Count - 1)
'Create range names for the fields.
xlObj.Goto Worksheets("Import").Range("A2")
Set rng = Nothing
Set rng = ActiveCell.CurrentRegion
rng.Select
Selection.CreateNames Top:=True

ExitSub:

xlObj.ActiveWorkbook.Save
xlObj.ActiveWorkbook.Close

If intError = 429 Then
    xlObj.Quit
    Err.Number = 0
End If

Set rs = Nothing
Set db = Nothing
Set xlObj = Nothing

Exit Sub

ErrHandler:

If Err.Number <> 0 Then
    MsgBox Err.Number & " " & Err.Description
End If

xlObj.ActiveWorkbook.Close SaveChanges:=False
xlObj.Quit

Set rs = Nothing
Set db = Nothing
Set xlObj = Nothing

End Sub



Thu, 17 Jun 2004 08:28:26 GMT  
 Automation Problem with Access 97/Excel 97
Stephen,

You're missing the final release of rng

Set rng = Nothing

If anything "points" to the Excel object it will remain in memory.

Steve



Quote:
> Hi there --
> I have VBA code in Access 97 which is opening an Automation object
reference to
> Excel 97.  Even though I have code that is supposed to quit Excel and then
set the
> reference to nothing, I still have an instance of Excel listed as a
running program
> when I go into the Task Manager.  The only way I can get it to go away is
by
> physically closing Access.
> Here's the code I'm using.  I've tried various permutations but so far
haven't
> been successful.  Any suggestions would be appreciated.
> Thanks in advance,
> Stephen

> Sub ExcelRpt()

> On Error GoTo ErrHandler

> Dim xlObj As Excel.Application
> Err.Number = 0
> On Error GoTo NotLoaded
> Set xlObj = GetObject(, "Excel.Application")
> NotLoaded:
> If Err.Number = 429 Then
>     Set xlObj = CreateObject("Excel.Application")
>     intError = Err.Number
> End If
> xlObj.Visible = True

> 'Define the variable to hold the Excel file name and location.
> strFileName = "C:\Audit\Audit.xls"

> 'Open the file and make it visible.
> xlObj.Workbooks.Open strFileName

> 'Go to the Import spreadsheet.  Clear the old data and position the
> 'cell point at A2 for the new data.
> Worksheets("Import").Activate
> xlObj.Goto Worksheets("Import").Range("A2")
> Set rng = ActiveCell.CurrentRegion
> rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count).Select
> Selection.Clear

> Set db = CurrentDb()
> 'The strSQL statement string comes from a global variable
> Set rs = db.OpenRecordset(strSQL)

> 'Insert the records into the spreadsheet.

> 'Get the total number of records in the query.
> rs.MoveLast
> rs.MoveFirst
> intCount = rs.RecordCount

> 'Set the initial values for the row and column coordinates.
> x = 2   'Initial row value
> y = 1   'Initial column value

> 'Loop through all the fields in all the records, obtain the values, and
> 'insert them into the spreadsheet.
> xlObj.DisplayAlerts = False
> For y = 1 To (rs.Fields.Count - 1)
>     For i = 1 To intCount
>         xlObj.Worksheets("Import").Cells(x, y).Value = rs.Fields(y).Value
>         If rs.EOF = False Then
>             rs.MoveNext
>         End If
>         x = x + 1
>     Next i
>     x = 2
>     rs.MoveFirst
> Next y

> 'Enter the total number of records for this audit.
> 'xlObj.Range("AuditRecordCount").Value =

(ActiveCell.CurrentRegion.Rows.Count - 1)

- Show quoted text -

Quote:
> 'Create range names for the fields.
> xlObj.Goto Worksheets("Import").Range("A2")
> Set rng = Nothing
> Set rng = ActiveCell.CurrentRegion
> rng.Select
> Selection.CreateNames Top:=True

> ExitSub:

> xlObj.ActiveWorkbook.Save
> xlObj.ActiveWorkbook.Close

> If intError = 429 Then
>     xlObj.Quit
>     Err.Number = 0
> End If

> Set rs = Nothing
> Set db = Nothing
> Set xlObj = Nothing

> Exit Sub

> ErrHandler:

> If Err.Number <> 0 Then
>     MsgBox Err.Number & " " & Err.Description
> End If

> xlObj.ActiveWorkbook.Close SaveChanges:=False
> xlObj.Quit

> Set rs = Nothing
> Set db = Nothing
> Set xlObj = Nothing

> End Sub



Thu, 17 Jun 2004 09:50:35 GMT  
 Automation Problem with Access 97/Excel 97
        Thanks for catching that, but it turned out not to be the problem.  Someone in
another newsgroup suggested using "End" at the end of the procedure to force the
clearing of all variables and references.  That did the trick.
        Stephen
Quote:

>Stephen,

>You're missing the final release of rng

>Set rng = Nothing

>If anything "points" to the Excel object it will remain in memory.

>Steve



>> Hi there --
>> I have VBA code in Access 97 which is opening an Automation object
>reference to
>> Excel 97.  Even though I have code that is supposed to quit Excel and then
>set the
>> reference to nothing, I still have an instance of Excel listed as a
>running program
>> when I go into the Task Manager.  The only way I can get it to go away is
>by
>> physically closing Access.
>> Here's the code I'm using.  I've tried various permutations but so far
>haven't
>> been successful.  Any suggestions would be appreciated.
>> Thanks in advance,
>> Stephen

>> Sub ExcelRpt()

>> On Error GoTo ErrHandler

>> Dim xlObj As Excel.Application
>> Err.Number = 0
>> On Error GoTo NotLoaded
>> Set xlObj = GetObject(, "Excel.Application")
>> NotLoaded:
>> If Err.Number = 429 Then
>>     Set xlObj = CreateObject("Excel.Application")
>>     intError = Err.Number
>> End If
>> xlObj.Visible = True

>> 'Define the variable to hold the Excel file name and location.
>> strFileName = "C:\Audit\Audit.xls"

>> 'Open the file and make it visible.
>> xlObj.Workbooks.Open strFileName

>> 'Go to the Import spreadsheet.  Clear the old data and position the
>> 'cell point at A2 for the new data.
>> Worksheets("Import").Activate
>> xlObj.Goto Worksheets("Import").Range("A2")
>> Set rng = ActiveCell.CurrentRegion
>> rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count).Select
>> Selection.Clear

>> Set db = CurrentDb()
>> 'The strSQL statement string comes from a global variable
>> Set rs = db.OpenRecordset(strSQL)

>> 'Insert the records into the spreadsheet.

>> 'Get the total number of records in the query.
>> rs.MoveLast
>> rs.MoveFirst
>> intCount = rs.RecordCount

>> 'Set the initial values for the row and column coordinates.
>> x = 2   'Initial row value
>> y = 1   'Initial column value

>> 'Loop through all the fields in all the records, obtain the values, and
>> 'insert them into the spreadsheet.
>> xlObj.DisplayAlerts = False
>> For y = 1 To (rs.Fields.Count - 1)
>>     For i = 1 To intCount
>>         xlObj.Worksheets("Import").Cells(x, y).Value = rs.Fields(y).Value
>>         If rs.EOF = False Then
>>             rs.MoveNext
>>         End If
>>         x = x + 1
>>     Next i
>>     x = 2
>>     rs.MoveFirst
>> Next y

>> 'Enter the total number of records for this audit.
>> 'xlObj.Range("AuditRecordCount").Value =
>(ActiveCell.CurrentRegion.Rows.Count - 1)
>> 'Create range names for the fields.
>> xlObj.Goto Worksheets("Import").Range("A2")
>> Set rng = Nothing
>> Set rng = ActiveCell.CurrentRegion
>> rng.Select
>> Selection.CreateNames Top:=True

>> ExitSub:

>> xlObj.ActiveWorkbook.Save
>> xlObj.ActiveWorkbook.Close

>> If intError = 429 Then
>>     xlObj.Quit
>>     Err.Number = 0
>> End If

>> Set rs = Nothing
>> Set db = Nothing
>> Set xlObj = Nothing

>> Exit Sub

>> ErrHandler:

>> If Err.Number <> 0 Then
>>     MsgBox Err.Number & " " & Err.Description
>> End If

>> xlObj.ActiveWorkbook.Close SaveChanges:=False
>> xlObj.Quit

>> Set rs = Nothing
>> Set db = Nothing
>> Set xlObj = Nothing

>> End Sub



Thu, 17 Jun 2004 12:16:23 GMT  
 Automation Problem with Access 97/Excel 97
So out of curiosity...  how did the code "end" up looking?



Quote:
> Thanks for catching that, but it turned out not to be the problem.
Someone in
> another newsgroup suggested using "End" at the end of the procedure to
force the
> clearing of all variables and references.  That did the trick.
> Stephen


> >Stephen,

> >You're missing the final release of rng

> >Set rng = Nothing

> >If anything "points" to the Excel object it will remain in memory.

> >Steve



> >> Hi there --
> >> I have VBA code in Access 97 which is opening an Automation object
> >reference to
> >> Excel 97.  Even though I have code that is supposed to quit Excel and
then
> >set the
> >> reference to nothing, I still have an instance of Excel listed as a
> >running program
> >> when I go into the Task Manager.  The only way I can get it to go away
is
> >by
> >> physically closing Access.
> >> Here's the code I'm using.  I've tried various permutations but so far
> >haven't
> >> been successful.  Any suggestions would be appreciated.
> >> Thanks in advance,
> >> Stephen

> >> Sub ExcelRpt()

> >> On Error GoTo ErrHandler

> >> Dim xlObj As Excel.Application
> >> Err.Number = 0
> >> On Error GoTo NotLoaded
> >> Set xlObj = GetObject(, "Excel.Application")
> >> NotLoaded:
> >> If Err.Number = 429 Then
> >>     Set xlObj = CreateObject("Excel.Application")
> >>     intError = Err.Number
> >> End If
> >> xlObj.Visible = True

> >> 'Define the variable to hold the Excel file name and location.
> >> strFileName = "C:\Audit\Audit.xls"

> >> 'Open the file and make it visible.
> >> xlObj.Workbooks.Open strFileName

> >> 'Go to the Import spreadsheet.  Clear the old data and position the
> >> 'cell point at A2 for the new data.
> >> Worksheets("Import").Activate
> >> xlObj.Goto Worksheets("Import").Range("A2")
> >> Set rng = ActiveCell.CurrentRegion
> >> rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count).Select
> >> Selection.Clear

> >> Set db = CurrentDb()
> >> 'The strSQL statement string comes from a global variable
> >> Set rs = db.OpenRecordset(strSQL)

> >> 'Insert the records into the spreadsheet.

> >> 'Get the total number of records in the query.
> >> rs.MoveLast
> >> rs.MoveFirst
> >> intCount = rs.RecordCount

> >> 'Set the initial values for the row and column coordinates.
> >> x = 2   'Initial row value
> >> y = 1   'Initial column value

> >> 'Loop through all the fields in all the records, obtain the values, and
> >> 'insert them into the spreadsheet.
> >> xlObj.DisplayAlerts = False
> >> For y = 1 To (rs.Fields.Count - 1)
> >>     For i = 1 To intCount
> >>         xlObj.Worksheets("Import").Cells(x, y).Value =
rs.Fields(y).Value
> >>         If rs.EOF = False Then
> >>             rs.MoveNext
> >>         End If
> >>         x = x + 1
> >>     Next i
> >>     x = 2
> >>     rs.MoveFirst
> >> Next y

> >> 'Enter the total number of records for this audit.
> >> 'xlObj.Range("AuditRecordCount").Value =
> >(ActiveCell.CurrentRegion.Rows.Count - 1)
> >> 'Create range names for the fields.
> >> xlObj.Goto Worksheets("Import").Range("A2")
> >> Set rng = Nothing
> >> Set rng = ActiveCell.CurrentRegion
> >> rng.Select
> >> Selection.CreateNames Top:=True

> >> ExitSub:

> >> xlObj.ActiveWorkbook.Save
> >> xlObj.ActiveWorkbook.Close

> >> If intError = 429 Then
> >>     xlObj.Quit
> >>     Err.Number = 0
> >> End If

> >> Set rs = Nothing
> >> Set db = Nothing
> >> Set xlObj = Nothing

> >> Exit Sub

> >> ErrHandler:

> >> If Err.Number <> 0 Then
> >>     MsgBox Err.Number & " " & Err.Description
> >> End If

> >> xlObj.ActiveWorkbook.Close SaveChanges:=False
> >> xlObj.Quit

> >> Set rs = Nothing
> >> Set db = Nothing
> >> Set xlObj = Nothing

> >> End Sub



Fri, 18 Jun 2004 04:06:09 GMT  
 Automation Problem with Access 97/Excel 97

Quote:

>So out of curiosity...  how did the code "end" up looking?

        I'm going to go back line-by-line and reconstruct the procedure to see if I can
find where the problem is.  In another NG (microsoft.public.excel.programming) they
found a couple other sloppy things I did (or didn't) that, while they probably aren't
the cause of the problem, I need to clean up a bit.
        In terms of the "End" statement, the procedure was called by the Report_Open
method of a report.  I just added "End" to the Report_Close method and it solved the
problem.
        The report itself is using Excel to stage the data.  The user wants the data in
a format that's like a crosstab of a crosstab of a crosstab ... etc.  I realized it
would be much easier if I could just port the data over to an Excel worksheet, enter
a couple formulas to calculate the totals, then link that worksheet back to Access
which treats a worksheet just like any other table.  Reports are really just
front-ends for tables/queries, so the Report object doesn't care if it's looking at
an Access table or an Excel worksheet, it just displays the data.
        (Plus I think it's cool linking Office apps to one another ...)
        Stephen


Fri, 18 Jun 2004 05:07:09 GMT  
 Automation Problem with Access 97/Excel 97
        Okay, I went line-by-line and did some reading up on Automation.  A couple
things ...
        First, I found that I can bypass the whole CreateObject/GetObject higgly-piggly
when I already have a reference to Excel in Tools References.  For example, if
there's a reference I can write:

        Dim xlObj as New Excel.Application
        xlObj.Visible = True

        ... and that opens a new blank workbook.  The key is the use of the "New" word
which launches a new instance of Excel.
        The other interesting thing is what I see when I monitor Excel's behavior in the
Task Manager as I go line-by-line.  Excel doesn't appear in the Task Manager until
the first actual call to xlObj, e.g. xlObj.Visible = True.  When it does, it appears
as "Microsoft Excel".  If I later open a saved workbook, it appears as "Microsoft
Excel - filename.xls" with filename of course being the name of the file.
        At the end of the procedure, when xlObj.Quit runs, the title in Task Manager
changes to *just* "Excel".  Not "Microsoft Excel".  Just "Excel".
        When the line Set xlObj = Nothing runs, the "Excel" listing in Task Manager goes
away.

        I vastly simplified the code to just this:

        ********************************************

Sub ExcelRpt()

Dim xlObj As New Excel.Application

xlObj.Visible = True

'Define the variable to hold the Excel file name and location.
strFileName = "C:\Audit\Audit.xls"    'strFileName is a global string variable

'Open the file and make it visible.
xlObj.Workbooks.Open strFileName

xlObj.Quit
Set xlObj = Nothing

End Sub

        ********************************************

        If I run that code, everything is fine and all references are cleaned up by End
Sub time.
        HOWEVER ... Once I add any line of code that physically interacts with the
worksheet, it causes the problem we're discussing.
        For example, if after the .Open method I were to add these lines:

        Worksheets("Import").Activate
        xlObj.Goto Worksheets("Import").Range("A2")

        EITHER ONE OF THOSE LINES will leave "Excel" open in the Task Manager after End
Sub.  I've tried other lines, e.g. working with a range object, and the same
phenomenon occurs.
        PHYSICALLY CLOSING THE .MDB file (not Access) clears the "Excel" listing in Task
Manager.
        One thought that occurred to me was that maybe UserControl had something to do
with it.  UserControl tracks whether Excel was opened on its own (.UserControl =
True) or another application (.UserControl = False).  I did some experimenting to
check the values but that doesn't seem to be it.
        I'll keep researching, but if there are any suggestions out there, please feel
free.
        Thanks in advance,
        Stephen



Fri, 18 Jun 2004 07:52:55 GMT  
 Automation Problem with Access 97/Excel 97
Suggestions?

Sure...

This line:    Worksheets("Import").Activate    now references an invisible
WorkSheet object that you can't set to Nothing
and I believe that:
xlObj.Goto Worksheets("Import").Range("A2")    now references an invisible
Range object.

So Excel has to remain in memory.

Try the following:

Public Sub ExcelRpt()

   Dim oXApp As Excel.Application
   Dim oXWb As Excel.Workbook
   Dim oXSheet As Excel.Worksheet
   Dim oXRange As Excel.Range

   Set oXApp = New Excel.Application

   oXApp.Visible = True    'do this only for testing, slower execution if
visible

   Set oXWb = oXApp.Workbooks.Open("C:\Audit\Audit.xls")
   Set oXSheet = oXWb.Worksheets("Import")
   Set oXRange = oXSheet.Range("A2")

   '... misc code

   Set oXRange = Nothing
   Set oXSheet = Nothing
   Set oXWb = Nothing

   oXApp.Quit

   Set oXApp = Nothing

End Sub

Steve



Quote:
> Okay, I went line-by-line and did some reading up on Automation.  A couple
> things ...
> First, I found that I can bypass the whole CreateObject/GetObject
higgly-piggly
> when I already have a reference to Excel in Tools References.  For
example, if
> there's a reference I can write:

> Dim xlObj as New Excel.Application
> xlObj.Visible = True

> ... and that opens a new blank workbook.  The key is the use of the "New"
word
> which launches a new instance of Excel.
> The other interesting thing is what I see when I monitor Excel's behavior
in the
> Task Manager as I go line-by-line.  Excel doesn't appear in the Task
Manager until
> the first actual call to xlObj, e.g. xlObj.Visible = True.  When it does,
it appears
> as "Microsoft Excel".  If I later open a saved workbook, it appears as
"Microsoft
> Excel - filename.xls" with filename of course being the name of the file.
> At the end of the procedure, when xlObj.Quit runs, the title in Task
Manager
> changes to *just* "Excel".  Not "Microsoft Excel".  Just "Excel".
> When the line Set xlObj = Nothing runs, the "Excel" listing in Task
Manager goes
> away.

> I vastly simplified the code to just this:

> ********************************************

> Sub ExcelRpt()

> Dim xlObj As New Excel.Application

> xlObj.Visible = True

> 'Define the variable to hold the Excel file name and location.
> strFileName = "C:\Audit\Audit.xls" 'strFileName is a global string
variable

> 'Open the file and make it visible.
> xlObj.Workbooks.Open strFileName

> xlObj.Quit
> Set xlObj = Nothing

> End Sub

> ********************************************

> If I run that code, everything is fine and all references are cleaned up
by End
> Sub time.
> HOWEVER ... Once I add any line of code that physically interacts with the
> worksheet, it causes the problem we're discussing.
> For example, if after the .Open method I were to add these lines:

> Worksheets("Import").Activate
> xlObj.Goto Worksheets("Import").Range("A2")

> EITHER ONE OF THOSE LINES will leave "Excel" open in the Task Manager
after End
> Sub.  I've tried other lines, e.g. working with a range object, and the
same
> phenomenon occurs.
> PHYSICALLY CLOSING THE .MDB file (not Access) clears the "Excel" listing
in Task
> Manager.
> One thought that occurred to me was that maybe UserControl had something
to do
> with it.  UserControl tracks whether Excel was opened on its own
(.UserControl =
> True) or another application (.UserControl = False).  I did some
experimenting to
> check the values but that doesn't seem to be it.
> I'll keep researching, but if there are any suggestions out there, please
feel
> free.
> Thanks in advance,
> Stephen



Fri, 18 Jun 2004 11:32:08 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Automation seems to hang with Access 97 / Word 97

2. Automation Error when automating Word 97 from Access 97

3. Sending text to Word 97 from Access 97 using OLE automation

4. Sending data to excel from access - automation 97.

5. Run Access 97 Automation Subroutines from Excel?

6. Access 97 Automation - VBA For Excel

7. Access 97: VBA-DAO-Access 97 synchronization problem?

8. vba code to copy table from access 97 to excel 97

9. Urgent : Exporting Access 97 data to Excel 97

10. Sending and manipulating data from Access 97 to Excel 97

11. Exporting Access 97 data to Excel 97

12. importing excel 97 srpeadsheets to access 97

 

 
Powered by phpBB® Forum Software