QUOTE (ICE929RR @ Mar 4 2010, 10:27 AM)

Hi,
Here's a code that I use to export to various sheets in a Excel template file and then send the Excel file by e-mail.
It works fine, but I know that the code can be optimized....
Hope it helps
Cheers
Luís
Lisbon-Portugal
***********
Public Function ExportQuery() As String
On Error GoTo Err_Handler
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sOutput As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim IRecords As Long
Dim IRecords1 As Long
Dim IRecords2 As Long
Dim J As Long
Dim strPeriod As String
Dim strYear As String
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim strAppName As String
Dim MyAtt1
Dim MyAtt2
DoCmd.SetWarnings False
Set FS = CreateObject("Scripting.FileSystemObject")
strlocal = "G:\Opex_File"
strData = Format(Now(), "dd-mm-yyyy hhmmss")
DoCmd.Hourglass True
On Error Resume Next
timeini = Time
sTemplate = CurrentProject.path & "\OPEX Information Template.xls"
'sOutput = CurrentProject.path & "\OPEX Information " & strData & ".xls"
sOutput = strlocal & "\OPEX Information " & strData & ".xls"
If Dir(sOutput) <> "" Then Kill sOutput
FileCopy sTemplate, sOutput
Set appExcel = New Excel.Application
appExcel.Visible = True
Set wbk = appExcel.Workbooks.Open(sOutput)
' WARRANTS **************************
' Só envia os que não foram marcados como "enviados"
sSQL = "SELECT [Warrants].* " & _
"FROM [Warrants] " & _
"WHERE ((([Warrants].Enviado)=No));"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
J = 3
Do Until rst.EOF
With wbk.Sheets("Warrants")
IRecords = IRecords + 1
'.Range("G3") = rst.Fields("Nome").Value
.Cells(J, 1).Value = rst.Fields("Nome").Value
.Cells(J, 2).Value = rst.Fields("Nome abreviado").Value
.Cells(J, 3).Value = rst.Fields("Tipo").Value
.Cells(J, 4).Value = rst.Fields("Activo subjacente").Value
.Cells(J, 5).Value = rst.Fields("Preço de Exercício").Value
.Cells(J, 6).Value = rst.Fields("Quantidade").Value
.Cells(J, 7).Value = rst.Fields("Data de Vencimento").Value
.Cells(J, 8).Value = rst.Fields("Preço de Emissão").Value
.Cells(J, 9).Value = rst.Fields("Paridade").Value
.Cells(J, 10).Value = rst.Fields("ISIN").Value
.Cells(J, 11).Value = rst.Fields("Código OPEX").Value
.Cells(J, 12).Value = rst.Fields("Código CVM").Value
.Cells(J, 13).Value = rst.Fields("Emitente").Value
.Cells(J, 14).Value = rst.Fields("Segmento").Value
.Cells(J, 15).Value = rst.Fields("UDN").Value
.Cells(J, 16).Value = rst.Fields("Data admissão").Value
.Cells(J, 17).Value = rst.Fields("Estilo").Value
.Cells(J, 18).Value = rst.Fields("Emissão").Value
.Cells(J, 19).Value = rst.Fields("Lote Mínimo").Value
.Cells(J, 20).Value = rst.Fields("Moeda").Value
End With
J = J + 1
rst.MoveNext
Loop
' TURBO-WARRANTS **************************
sSQL = "SELECT [Turbo-Warrants].* " & _
"FROM [Turbo-Warrants] " & _
"WHERE ((([Turbo-Warrants].Enviado)=No));"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
J = 3
Do Until rst.EOF
With wbk.Sheets("Turbo-Warrants")
IRecords1 = IRecords1 + 1
'.Range("G3") = rst.Fields("Nome").Value
.Cells(J, 1).Value = rst.Fields("Nome").Value
.Cells(J, 2).Value = rst.Fields("Nome abreviado").Value
.Cells(J, 3).Value = rst.Fields("Tipo").Value
.Cells(J, 4).Value = rst.Fields("Activo subjacente").Value
.Cells(J, 5).Value = rst.Fields("Preço de Exercício").Value
.Cells(J, 6).Value = rst.Fields("Barreira").Value
.Cells(J, 7).Value = rst.Fields("Quantidade").Value
.Cells(J, 8).Value = rst.Fields("Data de Vencimento").Value
.Cells(J, 9).Value = rst.Fields("Preço de Emissão").Value
.Cells(J, 10).Value = rst.Fields("Paridade").Value
.Cells(J, 11).Value = rst.Fields("ISIN").Value
.Cells(J, 12).Value = rst.Fields("Código OPEX").Value
.Cells(J, 13).Value = rst.Fields("Código CVM").Value
.Cells(J, 14).Value = rst.Fields("Emitente").Value
.Cells(J, 15).Value = rst.Fields("Segmento").Value
.Cells(J, 16).Value = rst.Fields("UDN").Value
.Cells(J, 17).Value = rst.Fields("Data admissão").Value
.Cells(J, 18).Value = rst.Fields("Estilo").Value
.Cells(J, 19).Value = rst.Fields("Emissão").Value
.Cells(J, 20).Value = rst.Fields("Lote Mínimo").Value
.Cells(J, 21).Value = rst.Fields("Moeda").Value
.Cells(J, 22).Value = rst.Fields("Data de Exclusão").Value
End With
J = J + 1
rst.MoveNext
Loop
' Inline Warrants **************************
sSQL = "SELECT [Inline Warrants].* " & _
"FROM [Inline Warrants] " & _
"WHERE ((([Inline Warrants].Enviado)=No));"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
J = 3
Do Until rst.EOF
With wbk.Sheets("Inline Warrants")
IRecords2 = IRecords2 + 1
'.Range("G3") = rst.Fields("Nome").Value
.Cells(J, 1).Value = rst.Fields("Nome").Value
.Cells(J, 2).Value = rst.Fields("Nome abreviado").Value
.Cells(J, 3).Value = rst.Fields("Activo subjacente").Value
.Cells(J, 4).Value = rst.Fields("Barreira inferior").Value
.Cells(J, 5).Value = rst.Fields("Barreira superior").Value
.Cells(J, 6).Value = rst.Fields("Quantidade").Value
.Cells(J, 7).Value = rst.Fields("Maturidade").Value
.Cells(J, 8).Value = rst.Fields("Preço").Value
.Cells(J, 9).Value = rst.Fields("Paridade").Value
.Cells(J, 10).Value = rst.Fields("ISIN").Value
.Cells(J, 11).Value = rst.Fields("Código OPEX").Value
.Cells(J, 12).Value = rst.Fields("Código CVM").Value
.Cells(J, 13).Value = rst.Fields("Emitente").Value
.Cells(J, 14).Value = rst.Fields("Segmento").Value
.Cells(J, 15).Value = rst.Fields("UDN").Value
.Cells(J, 16).Value = rst.Fields("Data admissão").Value
.Cells(J, 17).Value = rst.Fields("Estilo").Value
.Cells(J, 18).Value = rst.Fields("Emissão").Value
.Cells(J, 19).Value = rst.Fields("Lote Mínimo").Value
.Cells(J, 20).Value = rst.Fields("Moeda").Value
.Cells(J, 21).Value = rst.Fields("Data de Exclusão").Value
End With
J = J + 1
rst.MoveNext
Loop
wbk.Close True
appExcel.Quit
timefim = Time
Total = IRecords + IRecords1 + IRecords2
MsgBox "Foram processados: " & vbCrLf _
& " " & vbCrLf _
& " -> " & IRecords & " Warrants" & vbCrLf _
& " -> " & IRecords1 & " Turbo-Warrants" & vbCrLf _
& " -> " & IRecords2 & " Inline Warrants" & vbCrLf _
& " " & vbCrLf _
& " " & vbCrLf _
& "Total de " & Total & " registos" & vbCrLf _
& "processados em " + Format(timefim - timeini, "hh:nn:ss")
' Envia E-Mail
Call Shell(strAppName, 1)
strSubject = "Opex Information"
strEMailMsg = "Please find attached an updated file with Citi's and Commerzbank's warrants being negociated in Euronext." & Chr(10) & Chr(10) _
& "Best regards," & Chr(10) & Chr(10) _
& "*********" & Chr(10) _
& "*********" & Chr(10) _
& "*********" & Chr(10) _
& "*********" & Chr(10) _
& "*********" & Chr(10) _
& " " & Chr(10) _
& " " & Chr(10) _
& " " & Chr(10) _
& " " & Chr(10) _
& " "
MyAtt2 = sOutput
Const olMailItem As Long = 0
Const olFormatPlain As Long = 1
'Dim olApp As Object
'Dim objMail As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = "youremail@mail.com; youremail@mail.com; youremail@mail.com"
.CC = "youremail@mail.com"
.Subject = strSubject
.Importance = 2
'.BodyFormat = olFormatPlain
.Body = strEMailMsg
.Attachments.Add (MyAtt2)
.Display
End With
DoCmd.OpenQuery "UPDT Opex information 1 enviado", acNormal, acEdit ' Marca "Yes" no enviado,
DoCmd.OpenQuery "UPDT Opex information 2 enviado", acNormal, acEdit ' caso a Data Exclusão não seja nula
DoCmd.OpenQuery "UPDT Opex information 3 enviado", acNormal, acEdit
DoCmd.SetWarnings True
Set wbk = Nothing
'appExcel.Quit
Set appExcel = Nothing
Set rst = Nothing
Set dbs = Nothing
DoCmd.Hourglass False
Err_Handler:
ExportQuery = Err.Description
'Resume exit_Here
End Function
Thanks Luis! I think this will be very helpful. Particularly the part about assigning specific values to specific cells. I have not played with that type of code in Access before, so thank you for the push in the right direction!

Just curious, does this code create a copy of an existing Excel Spreadsheet and update the sheets inside? Or does it create a new blank workbook and sheets? From your code it appears that you are using a copy of a template file, which is perfect for me.
Thanks again!
Nate