Excel not "letting go" after DoCmd.Transferspreadsheet (Automation)

E

EagleOne

2003

Using Access to create 20+ spreadsheets in an Access VBA macro.

To create the XL files I am using:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
"CHR_ALL_AAASITE_TBL", myPath & myFileName, True

After this command, I want to format each XL worksheet.

To do the formating, I am attempting to use Automation in Access to reachout and format the XL
sheets.

My challenge is that Excel stays in memory and hangs on to the file and not permitting the
Automation to complete. (I get object error)

I guess I need to close Excel (and the just created file) to permit the Automation to flow.

Current Sequence:

1) DoCmd.TransferSpreadsheet ........ for File1
2) Close Excel and the file just created (Not sure how to code this!!! Access; Excel etc.)

objXL.Application.Quit????

3) Open Instance of Automation to do Excel Formating
4) Repeat for file2

I have the automation down (I think...)

I have the DoCmd down (I thought)

How do I get them to work in sequence together?



TIA EagleOne
 
K

Klatuu

It is probably just a timing issue. That is, the TransferSpreadsheet has not
really completed before you try to open the file to do that automation. That
is unless there is an error in your automation code raising the error. You
might try copying the code at this site:
http://www.mvps.org/access/api/api0021.htm

You can use it to pause execution for a second or so between the transfer
and the automation.
 
E

EagleOne

Klatuu,

The issue is not the timer. I did install it but continued to get the error. BTW, the core of my
Automation was Dev Ashish's procedure from the beginning.

What seemed to fix the issue was to modify Dev Ashish's procedure at the very end:
FROM: If boolXL Then objXL.Application.Quit
TO: objXL.Application.Quit

In short, if I forced a quit each time the macro goes from file to file woth no error.

That said, IS THERE A BETTER WAY?

Following for reference only:
*****************************************************
'
' Dev Ashish's procedure
'
Sub sTestXL()
Dim objXL As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object

If fIsAppRunning("Excel") Then
Set objXL = GetObject(, "Excel.Application")
boolXL = False
Else
Set objXL = CreateObject("Excel.Application")
boolXL = True
End If

objXL.Application.workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkBook

With objActiveWkb
.Worksheets(1).Cells(1, 1) = "Hello World"
strWhat = .Worksheets(1).Cells(1, 1).value
End With

objActiveWkb.Close savechanges:=False

If boolXL Then objXL.Application.Quit

Set objActiveWkb = Nothing: Set objXL = Nothing
MsgBox strWhat
End Sub



First, I installed the timer
 
T

Tom

I use the following to transfer data...

I generally write it/troubleshoot it using early binding then switch
to late binding for actual use.

The gist of it is to use excel's CopyFromRecordset method

Hope this helps

Tom

Public Sub subGenerateExcel(RowSQL As String, ShowFile As Boolean,
Optional FileName As String)
'---------------------------------------------------------------------------------------
' Name : subGenerateExcel
' DateTime : 6/4/2008 14:18
' Author : RT Mitchell
' Purpose :
'---------------------------------------------------------------------------------------
'
Dim strRS As String
Dim strFilter As String
Dim rst As DAO.Recordset
Dim bolFlag As Boolean

'****early binding*************
'Dim objXL As Excel.Application
'Dim objWorkbk As Excel.Workbook
'Dim objWorkSht As Excel.Worksheet
'Set objXL = New Excel.Application 'early binding
'****late binding*****************
Dim objXL As Object, objWorkbk As Object, objWorkSht As Object
Set objXL = CreateObject("Excel.Application") 'late binding


'*********excel constants used with late binding
Const xlContinuous As Long = 1
Const xlThin As Long = 2
Const xlAutomatic As Long = -4105
Const xlsolid As Long = 1
Const xlEdgeLeft As Long = 7
Const xlEdgeRight As Long = 10
Const xlEdgeTop As Long = 8
Const xlEdgeBottom As Long = 9
Const xlInsideVertical As Long = 11
Const xlInsideHorizontal As Long = 12
Const xlCenter As Long = -4108
Const xlAllChanges As Long = 2
Const xlValidateList As Long = 3
Const xlValidAlertStop As Long = 1
Const xlBetween As Long = 1
Const xlUnlockedCells As Long = 1
Const xlshared As Long = 2

On Error GoTo ErrHandler

Set rst = CurrentDb.OpenRecordset(RowSQL, dbOpenDynaset, dbSeeChanges)
Set objWorkbk = objXL.Workbooks.Add
Set objWorkSht = objWorkbk.Worksheets("Sheet1")
With objWorkSht.Range("A1")
.offset(0, 0).Value = "Item No"
.offset(0, 1).Value = "Due Date"
.offset(0, 2).Value = "Forecast Date"
.offset(0, 3).Value = "Responsible Individual"
.offset(0, 4).Value = "Secondary Individual"
.offset(0, 5).Value = "Status"
.offset(0, 6).Value = "Audit Binder"
.offset(0, 7).Value = "Agency"
.offset(0, 8).Value = "Citation"
.offset(0, 9).Value = "Permit/Approval/Notification"
.offset(0, 10).Value = "Info Requirement"
.offset(0, 11).Value = "Deliverable"
.offset(0, 12).Value = "Remarks"


'export data
.offset(1, 0).CopyFromRecordset rst
'do the lines
End With
With objWorkSht.UsedRange
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'do the formats
.Columns.AutoFit
End With
'apply color
With objWorkSht.Range("A1:M1").Interior
.ColorIndex = 37
.Pattern = xlsolid
End With

'add validation to the status column
With objWorkSht.Range("F:F").Validation
.Add xlValidateList, xlValidAlertStop, xlBetween, "Not Started, In
Progress, Hold, Delivered"
.IgnoreBlank = False
.InCellDropdown = True
.InputTitle = "Status List"
.ErrorTitle = "Status List"
.InputMessage = "Select an updated status from the drop down list."
.ErrorMessage = "You must select one of the listed items for the
Status. Click Cancel to continue."
.ShowInput = True
.ShowError = True
End With

'if a file name is passed in, save the workbook and turn on highlight
changes
If Len(FileName) > 1 Then
With objWorkbk
objWorkbk.SaveAs FileName, , , , , , xlshared
.HighlightChangesOptions xlAllChanges
.ListChangesOnNewSheet = False
.HighlightChangesOnScreen = True
End With
End If


If ShowFile = True Then
objXL.Visible = True
Else
objWorkbk.Close
End If


ExitHere:
Set objWorkSht = Nothing
Set objWorkbk = Nothing
Set objXL = Nothing

Set rst = Nothing
Exit Sub

ErrHandler:
Dim strErrString As String
Select Case Err.Number
Case Else
strErrString = "Unexpected Error: " & Err.Number & vbCrLf
strErrString = strErrString & Err.Description
MsgBox strErrString, vbCritical + vbOKOnly, "Sub:
subGenerateExcel of mdlExcel"
End Select
Resume ExitHere
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top