Hale, Jim
Jim.Hale at FleetPride.com
Wed May 25 17:00:17 CDT 2005
Here are some code fragments from one of my programs that should give you an idea of how to work with Excel. You can record your macro in Excel, copy it over to Access and prepend it with the excel object variable (appExcel) and it should work with a bit of tweaking. HTH Jim Hale Public Function Load_Financials(strpathname As String) As Boolean Dim dbs2 As Database, recset(1 To 4) As Recordset Dim qryString As String, qrystring2 As String, qrystring3 As String, qrystring4 As String Dim blstate As Boolean, intmonth As Integer, x as integer Dim appExcel As Excel.Application, dblAmort As Double intmonth = CurrMonth 'get current reporting month Set dbs2 = CurrentDb Set appExcel = New Excel.Application DoCmd.Hourglass True With appExcel .Workbooks.Open strpathname, 0 qryString = "qry_BS_Excel" qrystring2 = "qry_BS_amortforCF_Excel" qrystring3 = "qryReserves_Excel" Set recset(1) = dbs2.OpenRecordset(qryString) Set recset(2) = dbs2.OpenRecordset(qrystring2) 'get amortization for the month Set recset(3) = dbs2.OpenRecordset(qrystring3) dblAmort = Nz(recset(2)("amt")) .Sheets("CF_Worksht").Select .Range("amort").FormulaR1C1 = dblAmort 'load monthly amort 'load rec provision and inventory oblolescense reserve .Worksheets("CF_Worksht").Range("ClearReserves").ClearContents If recset(3).RecordCount > 0 Then .Worksheets("CF_Worksht").Range("Reserves").CopyFromRecordset recset(3) End If 'Load Balance sheet data blstate = DataPaste("qry_BS_Excel", appExcel, recset(1)) 'load month number .Worksheets("Dates").Range("C16").FormulaR1C1 = intmonth 'load year number .Worksheets("Dates").Range("ISYear").FormulaR1C1 = CurrYear .ActiveWorkbook.Close SaveChanges:=True End With appExcel.Quit Set appExcel = Nothing For X = 1 To 3 If Not (recset(X) Is Nothing) = True Then recset(X).Close: Set recset(X) = Nothing Next X If Not (dbs2 Is Nothing) = True Then dbs2.Close: Set dbs2 = Nothing -----Original Message----- From: Mark Boyd [mailto:MarkBoyd at mcbeeassociates.com] Sent: Wednesday, May 25, 2005 4:29 PM To: Access Developers discussion and problem solving Subject: [AccessD] Excel Macro Automation from within Access I have a need to run Excel macro code from within Access VBA. Basically, the code logic flows as follows: 1. Export Access report as a new Excel file (DoCmd.OutputTo acOutputReport, strXLName, acFormatXLS, , True) 2. Run Excel macro (from within Access VBA) to format the Excel file accordingly. Does anyone have any sample code and/or resources related to this logic? Any help is greatly appreciated. Thanks, Mark Boyd I/S Supervisor McBee Associates, Inc. ----------------------------------------- This message and any attachments are intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential, and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by replying to this message, and then delete it from your system. ------------------------------------------- -- AccessD mailing list AccessD at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/accessd Website: http://www.databaseadvisors.com *********************************************************************** The information transmitted is intended solely for the individual or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you have received this email in error please contact the sender and delete the material from any computer. As a recipient of this email, you are responsible for screening its contents and the contents of any attachments for the presence of viruses. No liability is accepted for any damages caused by any virus transmitted by this email.