Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Freeing up a reference to Excel from VB

Status
Not open for further replies.

Slarti

Programmer
Feb 5, 2003
45
GB
Using the code below I retrieve a Recordset from the Excel file and can run and run this code and it works each time. My problem lies in freeing up the Excel file to be read by other users following import of the recordset. Once I've run this, trying to access the file via Excel reports that the file is in use. I think I free up the reference to Excel at the end of the first procedure but must be making a fundamental mistake somewhere?

Private Sub

Set Excel = New Excel.Application
Set Workbook = Excel.Workbooks.Open("U:\CIS Details\CISs.xls")

Workbook.Activate

Set Worksheet = Workbook.Sheets("CIS List")

Dim X As Integer

With Worksheet
m_sHere = Chr(64 + ActiveCell.SpecialCells(xlLastCell).Column) & ActiveCell.SpecialCells(xlLastCell).Row
End With

MsgBox m_sHere

Here2

Set Worksheet = Nothing

Set Workbook = Nothing

Set Excel = Nothing

' Set db = DBEngine.OpenDatabase("U:\CIS Details\CIS.mdb")

End Sub

Private Sub Here2()

sTableName = "[CIS List$A1:" & m_sHere & "]"

'Open the ADO connection to the Excel workbook
Set oConn = New ADODB.Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "U:\CIS Details\CISs.xls" & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;"""
Set oRs = New ADODB.Recordset
oRs.Open "SELECT * FROM " & sTableName, oConn, adOpenStatic, adLockOptimistic
oRs.MoveLast
nRows = oRs.RecordCount
nCols = oRs.Fields.Count

End Sub
 
Try closing workbook before setting to nothing

Workbook.close (optional parameters)

Try quiting application before setting to nothing
Excel.Quit

[flowerface]




I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
Doing this I no longer get the message but when I try to open the workbook I get a shadow of Excel opening with white space where a/the worksheet should be. Excel seems to be working in tat the menu bar/toolbar is active but it can't open the workbook to view.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top