cancel
Showing results for 
Search instead for 
Did you mean: 

Exporting a crystal report as PDF and Attaching to an email via code - Filename Issuses?

Former Member
0 Kudos

Post Author: alynch

CA Forum: .NET

I need to export a crystal report as a pdf and send it out via email. I have created a subroutine that works but the attached filename come up as "untitled.txt" so the receiving machine believes it is a text file. If I rename it on the recipients machine to a ".pdf" I can open it with acrobat and it looks OK. Does anyone know how to rename the file as a pdf prior to sending it out?

Thank You.

al

I have included a copy of the subroutine:

Dim repdoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()

Dim diskOpts As New CrystalDecisions.Shared.DiskFileDestinationOptions()

Dim ExpOpts As CrystalDecisions.Shared.ExportOptions

Dim MailOpts As New CrystalDecisions.Shared.MicrosoftMailDestinationOptions()

repdoc = Me.CrystalReport11

repdoc.Load("C:\Documents and Settings\User\My Documents\Visual Studio 2005\Projects\WindowsApplication3\WindowsApplication3\CrystalReport1.rpt")

ExpOpts = repdoc.ExportOptions

With ExpOpts

.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.MicrosoftMail

.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

End With

With MailOpts

.MailMessage = "Message"

.MailToList = "enter email adress here"

.MailSubject = "Attached is a PDF file - .net Export test "

End With

ExpOpts.DestinationOptions = MailOpts

Try

repdoc.Export()

Catch err As Exception

MessageBox.Show(err.ToString())

End Try

End Sub

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Post Author: Knight

CA Forum: .NET

I had this same problem today, here's what I used. Its built in 2 parts. Step one loops throught and exports PDF copies of a traking report. Step 2 builds an email list from a SQl query and sends it.STEP ONE:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Declare the some variables Dim row1 As DataRow Dim brokerID As String Dim brokername As String Dim brokerEmail As String Dim shipdate As String Dim crParameterFieldDefinitions As ParameterFieldDefinitions Dim crParameterFieldDefinition As ParameterFieldDefinition Dim crParameterValues As New ParameterValues() Dim crParameterDiscreteValue As New ParameterDiscreteValue() Dim crParameterFieldDefinitions1 As ParameterFieldDefinitions Dim crParameterFieldDefinition1 As ParameterFieldDefinition Dim crParameterValues1 As New ParameterValues() Dim crParameterDiscreteValue1 As New ParameterDiscreteValue() Dim CrReport As New CrystalReport1() ' Report Name Dim report As ReportDocument = "O:KNIFormats_ReportPrivateSMStageShipmentInfo" Dim CrExportOptions As ExportOptions Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions() Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions() Dim SHIPDATE123 As String SHIPDATE123 = _month & "/" & _day & "/" & year Dim db As String db = System.Configuration.ConfigurationSettings.AppSettings("Datalogin") Dim sqlCon As New SqlConnection sqlCon.ConnectionString = db Dim strsql1 As String strsql1 = "SELECT tblSOOrd_Hdr.BrokerCd, tblSO_Ord_Hdr.BrokerName, tblSM_Ship_Hdr.ShipDate " strsql1 &= "FROM tblSO_Ord_Hdr INNER JOIN tblSM_Ship_Hdr ON tblSO_Ord_Hdr.Locale = tblSM_Ship_Hdr.Locale AND tblSO_Ord_Hdr.OrdNo = tblSM_Ship_Hdr.OrdNo AND " strsql1 &= " tblSO_Ord_Hdr.RlsNo = tblSM_Ship_Hdr.RlsNo INNER JOIN tblSys_Cust_Broker ON tblSO_Ord_Hdr.BrokerCd = tblSys_Cust_Broker.BrokerCd " strsql1 &= "GROUP BY tblSO_Ord_Hdr.BrokerCd, tblSM_Ship_Hdr.ShipDate, tblSO_Ord_Hdr.BrokerName " strsql1 &= "HAVING (tblSM_Ship_Hdr.ShipDate = '" & SHIPDATE123 & "') " Dim da1 As New SqlDataAdapter(strsql1, sqlCon) Dim worktbl1 As DataTable worktbl1 = New DataTable("tblEmail") da1.Fill(worktbl1) da1.FillSchema(worktbl1, SchemaType.Source) If worktbl1.Rows.Count > 0 Then For Each row1 In worktbl1.Rows brokerID = CStr(row1("BrokerCd")).Trim brokername = CStr(row1("BrokerName")).Trim shipdate = CStr(row1("ShipDate")).Trim CrReport.Load() crParameterDiscreteValue.Value = shipdate crParameterFieldDefinitions = CrReport.DataDefinition.ParameterFields crParameterFieldDefinition = crParameterFieldDefinitions.Item("ShipDate") crParameterValues = crParameterFieldDefinition.CurrentValues crParameterValues.Clear() crParameterValues.Add(crParameterDiscreteValue) crParameterFieldDefinition.ApplyCurrentValues(crParameterValues) crParameterDiscreteValue1.Value = brokerID crParameterFieldDefinitions1 = CrReport.DataDefinition.ParameterFields crParameterFieldDefinition1 = crParameterFieldDefinitions1.Item("Broker") crParameterValues1 = crParameterFieldDefinition1.CurrentValues crParameterValues1.Clear() crParameterValues1.Add(crParameterDiscreteValue1) crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1) CrDiskFileDestinationOptions.DiskFileName = "c:Test_Folder20" & _year & "-" & _month & "-" & day & "" & brokername & ".pdf" CrFormatTypeOptions.FirstPageNumber = 1 ' Start Page in the Report CrFormatTypeOptions.LastPageNumber = 1000 ' End Page in the Report CrFormatTypeOptions.UsePageRange = True CrExportOptions = CrReport.ExportOptions With CrExportOptions .ExportDestinationType = ExportDestinationType.DiskFile .ExportFormatType = ExportFormatType.PortableDocFormat .DestinationOptions = CrDiskFileDestinationOptions .FormatOptions = CrFormatTypeOptions End With Try CrReport.Export() Catch err As Exception MessageBox.Show("DID NOT EXPORT") End Try Next MessageBox.Show("All PDF's exported succesfully") End If End SubSTEP 2: Private Sub Button3Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim row2 As DataRow Dim SHIPDATE123 As String SHIPDATE123 = _month & "/" & _day & "/" & year Dim db1 As String db1 = System.Configuration.ConfigurationSettings.AppSettings("Datalogin") Dim sqlCon As New SqlConnection sqlCon.ConnectionString = db1 Dim strsql2 As String strsql2 = "SELECT tblSOOrd_Hdr.BrokerCd, tblSys_Cust_Broker.BrokerName, tblSM_Ship_Hdr.ShipDate, tblSys_Cust_Broker.ContactEmail " strsql2 &= "FROM tblSO_Ord_Hdr INNER JOIN tblSM_Ship_Hdr ON tblSO_Ord_Hdr.Locale = tblSM_Ship_Hdr.Locale AND tblSO_Ord_Hdr.OrdNo = tblSM_Ship_Hdr.OrdNo AND " strsql2 &= " tblSO_Ord_Hdr.RlsNo = tblSM_Ship_Hdr.RlsNo INNER JOIN tblSys_Cust_Broker ON tblSO_Ord_Hdr.BrokerCd = tblSys_Cust_Broker.BrokerCd " strsql2 &= "GROUP BY tblSO_Ord_Hdr.BrokerCd, tblSM_Ship_Hdr.ShipDate, tblSys_Cust_Broker.ContactEmail, tblSys_Cust_Broker.BrokerName, tblSys_Cust_Broker.Email_ASN " strsql2 &= "HAVING (tblSM_Ship_Hdr.ShipDate = '" & SHIPDATE123 & "') AND (tblSys_Cust_Broker.Email_ASN = 1) AND (tblSys_Cust_Broker.ContactEmail <> '') " Dim da1 As New SqlDataAdapter(strsql2, sqlCon) Dim worktbl2 As DataTable worktbl2 = New DataTable("tblEmail12") da1.Fill(worktbl2) da1.FillSchema(worktbl2, SchemaType.Source) Dim brokerID As String Dim brokername As String Dim brokerEmail As String Dim shipdate As String If worktbl2.Rows.Count > 0 Then For Each row2 In worktbl2.Rows brokerID = CStr(row2("BrokerCd")).Trim brokername = CStr(row2("BrokerName")).Trim brokerEmail = CStr(row2("ContactEmail")).Trim shipdate = CStr(row2("ShipDate")).Trim Dim mail As New MailMessage Dim att As String att = "c:Test_Folder20" & _year & "-" & _month & "-" & _day & "" & brokername & ".pdf" 'set the addresses mail.From = New MailAddress("no-reply@knoxnursery.com") mail.To.Add(brokerEmail) mail.Attachments.Add(New Attachment(att)) 'set the content mail.Subject = "Shipment Tracking report from Knox Nursery for " & SHIPDATE123 mail.IsBodyHtml = True mail.Body = "


" mail.Body &= "This shipment update has been requested by:

" 'send the message Dim smtp As New SmtpClient smtp.Send(mail) ' MessageBox.Show("No Records Found", "Failed to send", MessageBoxButtons.OK, MessageBoxIcon.Stop Next Else MessageBox.Show("No broker have opted in", "No Emails Sent", MessageBoxButtons.OK, MessageBoxIcon.Information) End If MessageBox.Show("Emails sent... ", "Emails Sent", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub

Answers (0)