473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Automation - xlapp.Quit does not kill Excel

This code runs okay, but I cannot quit Excel. I've read that this may be
due to the way I reference the object variables, but I don't know how else I
would reference them. How do I get Excel to quit gracefully?

Thanks in advance.

Set xlapp = GetObject(, "Excel.Application")
'create new on error if none exists

Set xlwkb = xlapp.Workbooks.Open(strTarget)
For Each fldSub In fld.SubFolders
strSheetName = fldSub.Name
strMdb = fld & "\" & strSheetName
If LinkTable(strMdb) Then
j = xlwkb.Worksheets.Count
xlwkb.Worksheets.Add(After:=Worksheets(j)).Name = strSheetName
'DoCmd.TransferSpreadsheet transferType:=acExport,
queryname:="qryMean", fileName:=strTarget
'DoCmd.TransferSpreadsheet acExport, 8, "qryMean", strTarget,
True
bytWsCt = bytWsCt + 1
xlwkb.Save
End If
Next fldSub

xlapp.Quit ********* 'why doesn't this work?

Select Case Err.Number
Case 429
Set xlapp = CreateObject("Excel.Application")
Resume Next
Nov 13 '05 #1
3 8629
This line:
xlwkb.Worksheets.Add(After:=Worksheets(j)).Name =
strSheetName

is creating a new reference because you're not fully declaring the
Worksheets(j) to be a property of xlwkb object.

Try this:
xlwkb.Worksheets.Add(After:=xlwkb.Worksheets(j)).N ame =
strSheetName

By the way, I have never seen syntax of
.Name = strSheetName
at the end of a Worksheets.Add method. Is this a copy/paste error?
--

Ken Snell
<MS ACCESS MVP>

"deko" <de**@hotmail.com> wrote in message
news:iX*****************@newssvr13.news.prodigy.co m...
This code runs okay, but I cannot quit Excel. I've read that this may be
due to the way I reference the object variables, but I don't know how else
I
would reference them. How do I get Excel to quit gracefully?

Thanks in advance.

Set xlapp = GetObject(, "Excel.Application")
'create new on error if none exists

Set xlwkb = xlapp.Workbooks.Open(strTarget)
For Each fldSub In fld.SubFolders
strSheetName = fldSub.Name
strMdb = fld & "\" & strSheetName
If LinkTable(strMdb) Then
j = xlwkb.Worksheets.Count
xlwkb.Worksheets.Add(After:=Worksheets(j)).Name = strSheetName
'DoCmd.TransferSpreadsheet transferType:=acExport,
queryname:="qryMean", fileName:=strTarget
'DoCmd.TransferSpreadsheet acExport, 8, "qryMean", strTarget,
True
bytWsCt = bytWsCt + 1
xlwkb.Save
End If
Next fldSub

xlapp.Quit ********* 'why doesn't this work?

Select Case Err.Number
Case 429
Set xlapp = CreateObject("Excel.Application")
Resume Next

Nov 13 '05 #2
> This line:
xlwkb.Worksheets.Add(After:=Worksheets(j)).Name =
strSheetName

is creating a new reference because you're not fully declaring the
Worksheets(j) to be a property of xlwkb object.


Thanks - that was indeed it.

As an aside, do you know what the best way is to dump an Access Recordset
into an Excel Workseet? I've tried this:

xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset rst
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CurrentRegion.Columns.AutoFit

but something is not right...

Anyway, for added protection against any lingering instances of Excel, I've
added this:

Private Function CleanUp(procName As String)
On Error GoTo 0
Dim objProcList As Object
Dim objWMI As Object
Dim objProc As Object
'create WMI object instance
Set objWMI = GetObject("winmgmts:")
Debug.Print "Cleaning up " & procName
If Not IsNull(objWMI) Then
'create object collection of Win32 processes
Set objProcList = objWMI.InstancesOf("win32_process")
For Each objProc In objProcList 'iterate through enumerated
collection
If UCase(objProc.Name) = UCase(procName) Then
objProc.Terminate (0)
Debug.Print procName & " was terminated"
End If
Next
End If
Set objProcList = Nothing
Set objWMI = Nothing
End Function

I call it at the end of automation Function like this:

xlapp.Quit
Call CleanUp("Excel")
Set db = Nothing
Set xlapp = Nothing

Nov 13 '05 #3
I have seen other posts from people who do like using CopyFromRecordset for
EXCEL. I personally have not used it, so I cannot comment specifically on
it.

Not sure what you mean by "but something is not right..."
--

Ken Snell
<MS ACCESS MVP>
"deko" <de**@hotmail.com> wrote in message
news:pc*****************@newssvr21.news.prodigy.co m...
This line:
xlwkb.Worksheets.Add(After:=Worksheets(j)).Name =
strSheetName

is creating a new reference because you're not fully declaring the
Worksheets(j) to be a property of xlwkb object.


Thanks - that was indeed it.

As an aside, do you know what the best way is to dump an Access Recordset
into an Excel Workseet? I've tried this:

xlapp.Workbooks(strXlsFile).Worksheets(j + 1).CopyFromRecordset rst
xlapp.Workbooks(strXlsFile).Worksheets(j +
1).CurrentRegion.Columns.AutoFit

but something is not right...

Anyway, for added protection against any lingering instances of Excel,
I've
added this:

Private Function CleanUp(procName As String)
On Error GoTo 0
Dim objProcList As Object
Dim objWMI As Object
Dim objProc As Object
'create WMI object instance
Set objWMI = GetObject("winmgmts:")
Debug.Print "Cleaning up " & procName
If Not IsNull(objWMI) Then
'create object collection of Win32 processes
Set objProcList = objWMI.InstancesOf("win32_process")
For Each objProc In objProcList 'iterate through enumerated
collection
If UCase(objProc.Name) = UCase(procName) Then
objProc.Terminate (0)
Debug.Print procName & " was terminated"
End If
Next
End If
Set objProcList = Nothing
Set objWMI = Nothing
End Function

I call it at the end of automation Function like this:

xlapp.Quit
Call CleanUp("Excel")
Set db = Nothing
Set xlapp = Nothing

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jimmer | last post by:
I've got what should be an easy automation problem, but the solution simply isn't coming to me. I've got several public variables set up for automation as follows: Public gappExcel As...
5
by: Gary Cobden | last post by:
I have a problem with the following code, which leaves an instance of Excel visible in Task Manager. By a process of elimination I have got it down to the fact that something in the...
7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
2
by: Jean | last post by:
Hi all, I have a problem automating an Excel object from Access. I have the following fucntion that creates a table in Excel from an Access query: Function CreateTable2(strSourceName As...
4
by: dave | last post by:
I have an Access 97 application running quite happily in Windows 2000. If I upgrade a machine to Windows 2000 SP4 then I get an error in one area which is this - I use Automation to run an instance...
2
by: andreas | last post by:
Hi, Who can gives me a explanation for this Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlApp As New Excel.Application()...
10
by: Esmael | last post by:
Hi to all, /*****************************/ OS-WIn XP SP2 VB6 SP6 /*****************************/ Is their anyone who can help me with this: Source code written on VB6.
4
by: oliver james | last post by:
I'm trying to automate an Excel spreadsheet from Access. I've established a link and loaded data from a recordset onto a worksheet in Excel. I now want to perform some manipulation on the data. I...
7
by: =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post by:
I have a vb.net app that opens an excel worksheet, reads data and then closes the sheet. Im noticing that the Excel process is still running after I have closed and disposed of my excel objects. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.