
Automation: Using Access to Generate Excel Files while using Excel Statistics Add-ins
Okay....major frustration wall encountered:
Scenario: I am using Access to open an Excel template, dump database
recordsets into the template, and then kick off a macro that uses an Excel
Add-in (ATPVBAEN.XLA) to generate a histogram and descriptive statistics on
the same worksheet. The final product will have this process take place
over about 30 worksheets. However, while testing a sample of code to get
this to work I have encountered nothing but frustration while dealing with
add-ins. If there is anyone out there that has done this before I would
appreciate your assitance and guidance! The over all process of resolving
this may be faster via e-mail so I am including mine as follows:
follows. Note that the entire code runs without error BUT....the two pieces
of the macro that call upon the Add-in functions are skipped without error.
Also, the original template displays the DATA ANALYSIS add-in in the tools
drop down BUT the Excel workbook created from that template does not display
the add-in. *scratches head vigorously*. I am using the poen method rather
than the add method to reference the template
Thanks and I appreciate ANY assistance, I can get regarding this issue, in
advance!
Rick
_________________________________________________________________
'Set global variables
Dim appExcel As New Excel.Application
Dim addXL As Excel.AddIn
_________________________________________________________________
Public Sub TEST_PL_Mthly_CycleTime_Discos()
'###########################################################################
############################################
'Set declarations
Dim dbs As Database
Dim rsQueries As Recordset
Set dbs = CurrentDb()
'Select the pre-formed report template
appExcel.Workbooks.Open
"z:\Privateline\Reports\Templates\Template_PL_Mthly_CycleTime_Discos.xls"
appExcel.Visible = True
appExcel.ActiveWindow.Zoom = 75
'Determine if Add-in is loaded,if not, load\install add-in
Call Load_XL_Addin("C:\Program Files\Microsoft
Office\Office\Library\Analysis\Atpvbaen.xla")
'Select starting insert position for data
appExcel.Sheets(1).Select
appExcel.Sheets(1).Cells(4, 1).Select
Set rsQueries = dbs.OpenRecordset("Select * from
CycleTime_MasterDataPull_Discos where PROD='DS0' and Center <>'Access
Provisioning'", dbOpenSnapshot)
appExcel.Sheets(1).Cells(4, 1).CopyFromRecordset rsQueries
'If no records qualify print notification on worksheet
If rsQueries.RecordCount < 1 Then
appExcel.Sheets(1).Cells(5, 1) = "NO DATA QUALIFIED"
appExcel.Sheets(1).Cells(5, 1).Font.Size = 10
appExcel.Sheets(1).Cells(5, 1).Font.Bold = True
End If
'Clear recordset
Set rsQueries = Nothing
'Run the Excel MACRO after the recordset has been copied to EXCEL worksheet
appExcel.Application.Run "CycleTime"
'***************************************************************************
*****************
'MACRO LOGIC THAT IS BEING CALLED BY THE PRIOR
STATEMENT(Exists within EXCEL template)
'Application.Run "ATPVBAEN.XLA!Histogram",
ActiveSheet.Range("$M$4:$M$2000") _
' , ActiveSheet.Range("$R$1"),
ActiveSheet.Range("$O$1:$O$11"), False, False _
' , False, False
'Application.Run "ATPVBAEN.XLA!Descr",
ActiveSheet.Range("$M$4:$M$2000"), _
' ActiveSheet.Range("$U$1"), "C", False,
True
'***************************************************************************
*****************
'Reset the starting cursor position to page one since this report has
multiple pages
appExcel.Sheets(1).Select
'Save the EXCEL report to a new location and modify the name to the date
that report was created
If
Dir("Z:\PrivateLine\Reports\Prod\Monthly\PL_Mthly_CycleTime_Discos.xls") <>
"" Then _
Kill
("Z:\PrivateLine\Reports\Prod\Monthly\PL_Mthly_CycleTime_Discos.xls")
appExcel.ActiveWorkbook.SaveAs
("Z:\PrivateLine\Reports\Prod\Monthly\PL_Mthly_CycleTime_Discos.xls")
appExcel.ActiveWorkbook.Close
'EXIT TASKS
Set rsQueries = Nothing
Set dbs = Nothing
Set appExcel = Nothing
Set addXL = Nothing
End Sub
THIS CODE CHECKS FOR THE EXISTENCE OF THE ADD-IN
___________________________________________________________
Function Load_XL_Addin(strFilePath As String) As Boolean
Dim strAddInName As String
On Error Resume Next
'Set string to Add-in file name.
strAddInName = "Atpvbaen.xla"
'Remove extension from file name to get add-in name.
strAddInName = Left(strAddInName, Len(strAddInName) - 4)
'Attempt to return reference to Add-in.
Set addXL = appExcel.AddIns(strAddInName)
If Err <> 0 Then
Debug.Print Err.Description
Err.Clear
'If add-in is not in collection, add it.
Set addXL = appExcel.AddIns.Add(strFilePath)
Debug.Print Err.Description
If Err <> 0 Then
Debug.Print Err.Description
'If error occurs, exit procedure.
Load_XL_Addin = False
GoTo Load_XL_Addin_End
End If
End If
'Load Add-in
If Not addXL.Installed Then addXL.Installed = True
Load_XL_Addin = True
Load_XL_Addin_End:
Exit Function
End Function