Use Excel macro to format sheet after exported via MS Access Datab

I

ILuvAccess

Does anyone one know how to apply formatting to an excel sheet once you've
used the Output (macro) function from your MS Access database? In so many
words I have end users who are not happy with the way the output looks when
you output from access into excel, is there anyway I can attach a macro wich
kicks off once the form has been output that will pretty the form up?
 
I

ILuvAccess

Thanks for all your help!

Danny J. Lesandrini said:
You'll need to use Excel Automation from Access. It's not trivial, but the demo
database and article posted at DBJ might help.

http://www.databasejournal.com/features/msaccess/article.php/3563671

This example shows only a little formatting, setting the NumberFormat and
WrapText properties. There are many others, like cell backcolor, forecolor
and gridlines, that you would set from within this code. I posted it below for
convenience sake, but you really should get the download and read the article.
--

Danny J. Lesandrini
[email protected]
http://amazecreations.com/datafast





Public Function ExportRequest() As String
On Error GoTo err_Handler

' Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sTempFile As String
Dim sOutput As String

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim lRecords As Long
Dim iRow As Integer
Dim iCol As Integer
Dim iFld As Integer

Const cTabTwo As Byte = 2
Const cStartRow As Byte = 4
Const cStartColumn As Byte = 3

DoCmd.Hourglass True

' set to break on all errors
Application.SetOption "Error Trapping", 0

' start with a clean file built from the template file
sTemplate = CurrentProject.Path & "\SalesTemplate.xls"
sOutput = CurrentProject.Path & "\SalesOutput.xls"
If Dir(sOutput) <> "" Then Kill sOutput
FileCopy sTemplate, sOutput

' Create the Excel Applicaiton, Workbook and Worksheet and Database object
Set appExcel = Excel.Application
Set wbk = appExcel.Workbooks.Open(sOutput)
Set wks = appExcel.Worksheets(cTabTwo)
sSQL = "select * from qrySales"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)

' For this template, the data must be placed on the 4th row, third column.
' (these values are set to constants for easy future modifications)
iCol = cStartColumn
iRow = cStartRow
If Not rst.BOF Then rst.MoveFirst
Do Until rst.EOF
iFld = 0
lRecords = lRecords + 1
Me.lblMsg.Caption = "Exporting record #" & lRecords & " to SalesOutput.xls"
Me.Repaint

For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
wks.Cells(iRow, iCol) = rst.Fields(iFld)

If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
End If

wks.Cells(iRow, iCol).WrapText = False
iFld = iFld + 1
Next

wks.Rows(iRow).EntireRow.AutoFit
iRow = iRow + 1
rst.MoveNext
Loop

ExportRequest = "Total of " & lRecords & " rows processed."
Me.lblMsg.Caption = "Total of " & lRecords & " rows processed."

exit_Here:
' Cleanup all objects (resume next on errors)
On Error Resume Next
Set wks = Nothing
Set wbk = Nothing
Set appExcel = Nothing
Set rst = Nothing
Set dbs = Nothing
DoCmd.Hourglass False
Exit Function

err_Handler:
ExportRequest = Err.Description
Me.lblMsg.Caption = Err.Description
Resume exit_Here

End Function
 
A

aaron.kempf

excel is a disease.

dont put your data into excel
let the goddamn beancounters learn how to write queries; fire them if
they can't learn it.

me and 20 other 18 year old kids learned queries in an hour 8 years
ago... i was working for a video game company; and we had to write our
own queries in order to find bugs in the software-testing process

stick it to the man and screw excel
 
A

aaron.kempf

yeah

i dont appreciate you assholes wasting company resources by making the
same xls week in and week out

get a real career spreadsheet dork
 
Top