Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Import/Export (DTS) and Replication (2000)
 Using xp_sendmail within DTS

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-06 : 10:12:30
Hi,
I have a DTS package that creates an excel report and I would like to email it using xp_sendmail.

How do I set this up?

I have the following code:
(The DTS package runs, but no email is sent)


Function Main( )
dim xlapp
dim xlwb
dim xlws
dim xlrng
dim adoconn
dim adors
dim x
dim y
Dim sYear
Dim sMonth
Dim sDay
Dim sRptFileName
Dim sqlMail
Dim i

sYear=Year(DATE()-1)
sMonth=Month(DATE()-1)
sDay=Day(DATE()-1)
IF sDay<10 THEN
sDay="0" & sDay
END IF
IF sMonth<10 THEN
sMonth="0" & sMonth
END IF

set adoconn = createobject("ADODB.Connection")
adoconn.connectionstring = "driver={SQL Server};" & _
"server=ITITPADB01;uid=timecontroluser;trusted_connection=True;database=timeControl"

adoconn.open

set adors = createobject("ADODB.Recordset")
adors.open "Select * from [AssocHours]", adoconn

set xlapp = createobject("Excel.Application")
set xlwb = xlapp.workbooks.add
set xlws = xlwb.activesheet

xlapp.visible = true
set xlrng = xlws.cells(2,1)
y = adors.fields.count -1
for x = 0 to y
xlws.cells(x+1).value = adors.fields(x).name
next

xlrng.copyfromrecordset adors

Dim iEndData
Dim sEndColumn

sEndColumn = "M"
For iEndData=1 to 100
xlApp.Range("A" & iEndData).Select
xlApp.Range("A1:" & sEndColumn & iEndData).Select
xlApp.Selection.Font.Bold = TRUE
With xlapp.Selection.Font
.Name = "Tahoma"
.Size = 8
.ColorIndex = 0
End With
xlApp.Selection.HorizontalAlignment = &HFFFFEFF4
Exit For
Next

xlws.columns.autofit

sRptFileName = "\\ititpafs01\pds\INETPUB\FTPROOT\accumen\AssocHoursReport\ITI-HVR Associated Hours Report " & sYear & sMonth & sDay & ".xls"

set oFSO = CreateObject("Scripting.FileSystemObject")
IF oFSO.FileExists(sRptFileName) Then
oFSO.DeleteFile(sRptFileName)
END IF

set oFSO = nothing

xlwb.saveas (sRptFileName)
xlwb.close
xlapp.quit

Dim sSubject

sSubject = "REPORT"
Set xlApp = Nothing

DTSGlobalVariables("sGblReportFileName").value =sRptFileName
DTSGlobalVariables("sGblMailSubject").value ="ITI-HVR Associated Hours Report For " & sYear & "-" & sMonth & "-" & sDay

sqlMail = "EXEC master.dbo.xp_sendmail " & vbCrLf
sqlMail = sqlMail & "@recipients = '" & DTSGlobalVariables("sGblEmailReportsTo").value & "', " & vbCrLf
sqlMail = sqlMail & "@attachments = '" & DTSGlobalVariables("sGblReportFileName").value & "', " & vbCrLf
sqlMail = sqlMail & "@message = 'Report Attached', " & vbCrLf
sqlMail = sqlMail & "@copy_recipients = '" & DTSGlobalVariables("sGblEmailReportsCC").value & "', " & vbCrLf
sqlMail = sqlMail & "@subject = '" & DTSGlobalVariables("sGblMailSubject").value & "'"

DTSGlobalVariables("sMailSQL").value = sqlMail

Next

Main = DTSTaskExecResult_Success
End Function
   

- Advertisement -