473,698 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hanging Excel Instance

After reading 30+ threads on the subject and implementing pertinent
changes I still have an instance of Excel that won't close.
I hope its due to my poor coding and someone can spot the error.
I've used Dev Avish's code as a starting point and the Sub does what I
want less the hanging instance.

Please help if you can, Rick

Public Sub Email_Supplier( )
Dim dbs As DATABASE
Dim qdfSupplierCode As QueryDef
Dim qdfSupplierData As QueryDef
Dim rsSupplierData As Recordset
Dim rsSupplierCode As Recordset
Dim varRecords As Variant
Dim Rcount As Integer
Dim intCodeCounter As Integer
Dim objXL As Excel.Applicati on
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim intLastCol As Integer
Dim strCode As String
Dim strName As String

Const conMAX_ROWS = 1000
Const conWKB_NAME = "C:\MSA_Open_Or der_Update.xlt"
Const conSHT_NAME = "Open Orders"

Set objXL = Excel.Applicati on

objXL.Applicati on.DisplayAlert s = False

'*** Return Database object pointing to current database.
Set dbs = CurrentDb
'*** Get predefined QueryDef.
Set qdfSupplierCode =
dbs.QueryDefs!q ry_Unique_Suppl ier_with_Open_O rders
'*** Open Recordset
Set rsSupplierCode = qdfSupplierCode .OpenRecordset

'*** Count records in recordset
rsSupplierCode. MoveLast
Rcount = rsSupplierCode. RecordCount
'*** Build Array
rsSupplierCode. MoveFirst
varRecords = rsSupplierCode. GetRows(Rcount)
rsSupplierCode. MoveFirst

For intCodeCounter = 0 To Rcount - 1
strCode = varRecords(0, intCodeCounter)
strName = varRecords(1, intCodeCounter)
'*** Get predefined QueryDef.
Set qdfSupplierData =
dbs.QueryDefs!q ry_Open_Order_U pdate_Report
qdfSupplierData .Parameters!str SupplierCode = varRecords(0,
intCodeCounter)
'*** Open Recordset
Set rsSupplierData =
qdfSupplierData .OpenRecordset( dbOpenSnapshot)
'Load Excel Sheets
With objXL
Set objWkb = .Workbooks.Open (conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Workshee ts(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Workshee ts.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRang e.Columns.Count
With objSht
.Activate
.Range(.Cells(4 , 1), .Cells(conMAX_R OWS, _
intLastCol)).Cl earContents
.Range("A4").Co pyFromRecordset rsSupplierData
.Range("A2").Va lue = "Supplier Code: " & strCode & "
Supplier Name: " & strName
.Range("A4").Se lect
End With
End With

objXL.ActiveWor kbook.SaveAs FileName:="C:\" & strCode &
".xls", _
FileFormat:=xlN ormal, Password:="", WriteResPasswor d:="",
_
ReadOnlyRecomme nded:=False, CreateBackup:=F alse

rsSupplierCode. MoveNext
Next intCodeCounter
objXL.Applicati on.Quit
objXL.Applicati on.DisplayAlert s = True

Set qdfSupplierCode = Nothing
Set rsSupplierCode = Nothing
Set qdfSupplierData = Nothing
Set rsSupplierData = Nothing
Set dbs = Nothing
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing

End Sub
Nov 13 '05 #1
1 3550
"Rick Brown" <rb*******@comp userve.com> wrote in message
news:82******** *************** **@posting.goog le.com...
After reading 30+ threads on the subject and implementing pertinent
changes I still have an instance of Excel that won't close.
I hope its due to my poor coding and someone can spot the error.
I've used Dev Avish's code as a starting point and the Sub does what I
want less the hanging instance.

Please help if you can, Rick

Public Sub Email_Supplier( )
Dim dbs As DATABASE
Dim qdfSupplierCode As QueryDef
Dim qdfSupplierData As QueryDef
Dim rsSupplierData As Recordset
Dim rsSupplierCode As Recordset
Dim varRecords As Variant
Dim Rcount As Integer
Dim intCodeCounter As Integer
Dim objXL As Excel.Applicati on
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim intLastCol As Integer
Dim strCode As String
Dim strName As String

Const conMAX_ROWS = 1000
Const conWKB_NAME = "C:\MSA_Open_Or der_Update.xlt"
Const conSHT_NAME = "Open Orders"

Set objXL = Excel.Applicati on

objXL.Applicati on.DisplayAlert s = False

'*** Return Database object pointing to current database.
Set dbs = CurrentDb
'*** Get predefined QueryDef.
Set qdfSupplierCode =
dbs.QueryDefs!q ry_Unique_Suppl ier_with_Open_O rders
'*** Open Recordset
Set rsSupplierCode = qdfSupplierCode .OpenRecordset

'*** Count records in recordset
rsSupplierCode. MoveLast
Rcount = rsSupplierCode. RecordCount
'*** Build Array
rsSupplierCode. MoveFirst
varRecords = rsSupplierCode. GetRows(Rcount)
rsSupplierCode. MoveFirst

For intCodeCounter = 0 To Rcount - 1
strCode = varRecords(0, intCodeCounter)
strName = varRecords(1, intCodeCounter)
'*** Get predefined QueryDef.
Set qdfSupplierData =
dbs.QueryDefs!q ry_Open_Order_U pdate_Report
qdfSupplierData .Parameters!str SupplierCode = varRecords(0,
intCodeCounter)
'*** Open Recordset
Set rsSupplierData =
qdfSupplierData .OpenRecordset( dbOpenSnapshot)
'Load Excel Sheets
With objXL
Set objWkb = .Workbooks.Open (conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Workshee ts(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Workshee ts.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRang e.Columns.Count
With objSht
.Activate
.Range(.Cells(4 , 1), .Cells(conMAX_R OWS, _
intLastCol)).Cl earContents
.Range("A4").Co pyFromRecordset rsSupplierData
.Range("A2").Va lue = "Supplier Code: " & strCode & "
Supplier Name: " & strName
.Range("A4").Se lect
End With
End With

objXL.ActiveWor kbook.SaveAs FileName:="C:\" & strCode &
".xls", _
FileFormat:=xlN ormal, Password:="", WriteResPasswor d:="",
_
ReadOnlyRecomme nded:=False, CreateBackup:=F alse

rsSupplierCode. MoveNext
Next intCodeCounter
objXL.Applicati on.Quit
objXL.Applicati on.DisplayAlert s = True

Set qdfSupplierCode = Nothing
Set rsSupplierCode = Nothing
Set qdfSupplierData = Nothing
Set rsSupplierData = Nothing
Set dbs = Nothing
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing

End Sub

Change:
Set objXL = Excel.Applicati on
To:
Set objXL = New Excel.Applicati on

There are alos other issues. I would not try to do anything with
objXL.Applicati on after I had called .Quit apart from set it to nothing. I
would also set the objects to nothing in the reverse order to how they had
been created - so that you save, close and set to nothing the worksheet
object before setting the application object to nothing.

Other, issues are error handling which could be improved to make sure you do
your best to quit any hidden instance of Excel if your code started one.
And on that point, are you sure you always want to start a fresh instance of
Excel, even if the user already has Excel open. In fact you could
re-structure the code a bit so it had discrete functions called from within
the main sub to make error handling easier.

A final point to consider, is that if you are distributing this application
to other users (who may change their version of Office) you may be better
off using late binding and calling CreateObject("E xcel.Applicatio n")

Nov 13 '05 #2

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

Similar topics

8
3366
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to process this .txt file. Goal: I am working on a vba script to:
5
3034
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use the below API: TargetWorkbook.Save TargetWorkbook.Close
12
3219
by: elziko | last post by:
I'm using late binding (I must) to automate Excel. My code opens Excel after createing and poulating some sheets. My problem is that when the user finally decides to close Excel its process is left running until my application closes. I have tried setting my Excel.Application object to Nothing. I have tried to then fore the GC into action using:
5
2467
by: David C. Allen | last post by:
I have a vb.net app that is controling excel 2000 thru the com interop interface. I have referenced the excel 9.0 library and have cut down the code in the problem subroutine to this: Dim objExcelApp As New Excel.Application objExcelApp.Quit() objExcelApp = Nothing The problem is excel is hanging in memory and has to be closed by the task
2
21944
by: Powerguy | last post by:
Hi all, I am looking for a way to get the Process id (or a handle) of an EXCEL process created from within my code. For example when the following code is executed: Dim EXL As Excel.Application = New Excel.Application a new instance of EXCEL.EXE is created in the task manager.
16
2695
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
9
4545
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso TypeOf obj Is IDisposable Then DirectCast(obj, IDisposable).Dispose()
16
5180
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub Storage_Click() On Error GoTo Err_Storage_Click
24
10585
rauty
by: rauty | last post by:
Hi all, I'm trying to access the most recent instance of Excel so I can paste data. What works for me is if Excel isn't already opened, I open it and can paste the data where I want to. What isn't working for me is if there are multiple instances of Excel open. I'll explain: Say I have 3 instances of Excel opened, each with one workbook. Instance 1 = Book1, instance 2 = Book2, and instance 3 = Book3. I want to paste my data to Book3,...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9023
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
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 we have to send another system
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.