473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.Application
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_Order_Update.xlt"
Const conSHT_NAME = "Open Orders"

Set objXL = Excel.Application

objXL.Application.DisplayAlerts = False

'*** Return Database object pointing to current database.
Set dbs = CurrentDb
'*** Get predefined QueryDef.
Set qdfSupplierCode =
dbs.QueryDefs!qry_Unique_Supplier_with_Open_Orders
'*** 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!qry_Open_Order_Update_Report
qdfSupplierData.Parameters!strSupplierCode = 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.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRange.Columns.Count
With objSht
.Activate
.Range(.Cells(4, 1), .Cells(conMAX_ROWS, _
intLastCol)).ClearContents
.Range("A4").CopyFromRecordset rsSupplierData
.Range("A2").Value = "Supplier Code: " & strCode & "
Supplier Name: " & strName
.Range("A4").Select
End With
End With

objXL.ActiveWorkbook.SaveAs FileName:="C:\" & strCode &
".xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="",
_
ReadOnlyRecommended:=False, CreateBackup:=False

rsSupplierCode.MoveNext
Next intCodeCounter
objXL.Application.Quit
objXL.Application.DisplayAlerts = 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 3533
"Rick Brown" <rb*******@compuserve.com> wrote in message
news:82*************************@posting.google.co m...
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.Application
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_Order_Update.xlt"
Const conSHT_NAME = "Open Orders"

Set objXL = Excel.Application

objXL.Application.DisplayAlerts = False

'*** Return Database object pointing to current database.
Set dbs = CurrentDb
'*** Get predefined QueryDef.
Set qdfSupplierCode =
dbs.QueryDefs!qry_Unique_Supplier_with_Open_Orders
'*** 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!qry_Open_Order_Update_Report
qdfSupplierData.Parameters!strSupplierCode = 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.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRange.Columns.Count
With objSht
.Activate
.Range(.Cells(4, 1), .Cells(conMAX_ROWS, _
intLastCol)).ClearContents
.Range("A4").CopyFromRecordset rsSupplierData
.Range("A2").Value = "Supplier Code: " & strCode & "
Supplier Name: " & strName
.Range("A4").Select
End With
End With

objXL.ActiveWorkbook.SaveAs FileName:="C:\" & strCode &
".xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="",
_
ReadOnlyRecommended:=False, CreateBackup:=False

rsSupplierCode.MoveNext
Next intCodeCounter
objXL.Application.Quit
objXL.Application.DisplayAlerts = 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.Application
To:
Set objXL = New Excel.Application

There are alos other issues. I would not try to do anything with
objXL.Application 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("Excel.Application")

Nov 13 '05 #2

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

Similar topics

8
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...
5
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...
12
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...
5
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...
2
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...
16
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...
9
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...
16
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...
24
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
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...

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.