|
Question : Output and Email file
|
|
I need to output some data from the database, put it in a file (preferrably Excel), auto-generate an email with that file as an attachment. But I don't want the user to have to specify an output location and I cannot hard-code an output location. I've tried: DoCmd.SendObject acSendReport, stDocName, acFormatXLS, "[email protected]" but my long memo fields get truncated in the spreadsheet.
If I use: DoCmd.OutputTo acReport, stDocName then the user has to specify the output location and I have to know where it is to automatically attach it to the email I generate
What's the best way to do this? Thx,
mv
|
|
Answer : Output and Email file
|
|
this codes will create the excel file and save to desktop of the user
Sub export2Excel() Dim rs As DAO.Recordset, iCol As Integer, iRow As Integer Dim xlObj As Object, Sheet As Object
If Dir(Environ("userProfile") & "\Desktop\MyExcel.xls") <> "" Then
Kill Environ("userProfile") & "\Desktop\MyExcel.xls" End If
Set xlObj = CreateObject("Excel.Application") xlObj.Workbooks.Add
Set rs = CurrentDb.OpenRecordset("tbl_Deviations")
Set Sheet = xlObj.activeworkbook.worksheets("sheet1") 'copy the headers For iCol = 0 To rs.Fields.Count - 1 Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name Next rs.MoveFirst With Sheet iRow = 2 Do Until rs.EOF .cells(iRow, 1).Value = rs("DevID") .cells(iRow, 2).Value = rs("DevInteger") .cells(iRow, 3).Value = rs("DevStatus") .cells(iRow, 4).Value = rs("Rev") .cells(iRow, 5).Value = rs("TPInstance") .cells(iRow, 6).Value = rs("DevDateObserved") .cells(iRow, 7).Value = rs("DevSystem") .cells(iRow, 8).Value = rs("DevProtocolInteger") .cells(iRow, 9).Value = rs("DevSection") .cells(iRow, 10).Value = CStr(Nz(rs("DevValRequirement"), "")) .cells(iRow, 11).Value = CStr(Nz(rs("DevDescription"), "")) .cells(iRow, 12).Value = rs("DevReportedBy") .cells(iRow, 13).Value = rs("DevInvestComp") .cells(iRow, 14).Value = CStr(Nz(rs("DevInvestComments"), "")) .cells(iRow, 15).Value = rs("DevInvestCat") .cells(iRow, 16).Value = CStr(Nz(rs("DevInvestCatComments"), "")) .cells(iRow, 17).Value = rs("DevInvestProdImpact") .cells(iRow, 18).Value = rs("DevInvestProdImpactDiscNo") .cells(iRow, 19).Value = CStr(Nz(rs("DevInvestRationale"), "")) .cells(iRow, 20).Value = CStr(Nz(rs("DevInvestProdImpactRationale"), "")) .cells(iRow, 21).Value = rs("DevInvestRetestReq") .cells(iRow, 22).Value = CStr(Nz(rs("DevInvestCAPA"), "")) .cells(iRow, 23).Value = rs("DevInvestValApprover") .cells(iRow, 24).Value = rs("DevInvestQualApprover") .cells(iRow, 25).Value = rs("DevInvestCompleted") .cells(iRow, 26).Value = CStr(Nz(rs("DevConclusion"), "")) .cells(iRow, 27).Value = rs("DevConcValApprover") .cells(iRow, 28).Value = rs("DevConcQualApprover") .cells(iRow, 29).Value = rs("DevClosed") .cells(iRow, 30).Value = rs("DevClosedDate") iRow = iRow + 1 rs.MoveNext Loop .Name = "MySheet" End With rs.Close
xlObj.activeworkbook.saveas Environ("userProfile") & "\Desktop\MyExcel.xls"
Set Sheet = Nothing xlObj.Quit Set xlObj = Nothing End Sub
see this link to create an email with attachments
http://support.microsoft.com/?kbid=209948
|
|
|
|