Email report directly from the Crystal viewer

Éric Moreau
Éric Moreau used Ask the Experts™
on
Hi

I am using Crystal Reports bundled with VS2008.

A client asked me to add a feature to e-mail the report he currently sees in the viewer. I looked around the properties of the viewer and saw nothing that does that.

So I have added a plain old button to my viewer form and pasted this code I found googling (I am not sure it is related to this version of CR):
    Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
        Dim pdfOpts As PdfRtfWordFormatOptions = ExportOptions.CreatePdfRtfWordFormatOptions()
        Dim mailOpts As MicrosoftMailDestinationOptions = ExportOptions.CreateMicrosoftMailDestinationOptions()
        Dim exportOpts As ExportOptions = New ExportOptions

        pdfOpts.UsePageRange = False
        exportOpts.ExportFormatOptions = pdfOpts

        exportOpts.ExportDestinationOptions = mailOpts

        exportOpts.ExportDestinationType = ExportDestinationType.MicrosoftMail
        exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat

        With DirectCast(Me.Viewer.ReportSource, CrystalDecisions.CrystalReports.Engine.ReportDocument)
            .Export(exportOpts)
        End With
    End Sub

With this code, an "InvalidArgumentException" is raised on the .Export line. The Message is "Invalid export options".

Is this the correct way to e-mail the report? Is there another method?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shahan AyyubSenior Software Engineer

Commented:
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
My question is about sending the report directly from the viewer and not saving it first in a PDF file and then attach it to a SMTP mail which the user doesn't want (he wants his default e-mail application to handle it).
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
that doesn't work!

on http://msdn.microsoft.com/en-us/library/ms226415(v=VS.90).aspx, I can read: "To successfully export to Microsoft mail, the system first needs to have loaded mapisp32.dll. Mapisp32.dll can be loaded by running the mail client, for example Microsoft Outlook."

I have Outlook installed but I don't find Mapisp32.dll. Does somebody has it?
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
There should be an export button on the viewer.  It looks like an envelope
If you click it do you see the EMAIL or MAPI option?

If not then the export dlls aren't installed.

Is this on the development machine with VS2008 installed or a client?

If a client then what did you install on the client and the server?

mlmcc
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
Hi mlmcc

I saw that envelop icon on older version of the viewer (remember I am using the version bundled with VS2008). I don't see it in the current version (see image attached).

When I click the Export button, it only offers me files options (see the second menu).

Those images are coming from my developement PC. Do you have an idea of which dlls are not installed because when we install it, it is much an all or nothing option!
menu1.jpg
menu2.jpg
Senior .Net Consultant
Top Expert 2016
Commented:
I have been able to have the code working on VS2005 (and the version of CR that is bundled with it) on a XP machine with this code (apparently the mail options are really important).

But it won't work in my scenario because I wanted the Outlook (or default e-mail application) window to open a new message window with the report attached.


        Dim pdfOpts As PdfRtfWordFormatOptions = ExportOptions.CreatePdfRtfWordFormatOptions()
        Dim mailOpts As MicrosoftMailDestinationOptions = ExportOptions.CreateMicrosoftMailDestinationOptions()
        With mailOpts
            .MailCCList = ""
            .MailMessage = "message"
            .MailSubject = "subject"
            .MailToList = "eric.moreau@videotron.ca"
        End With
        Dim exportOpts As ExportOptions = New ExportOptions

        pdfOpts.UsePageRange = False
        exportOpts.ExportFormatOptions = pdfOpts

        exportOpts.ExportDestinationOptions = mailOpts


        exportOpts.ExportDestinationType = ExportDestinationType.MicrosoftMail
        exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat

        mrptDoc.Export(exportOpts)

Open in new window

Shahan AyyubSenior Software Engineer

Commented:
AFAIK Export option allows to export item in different software. I tested export option of the Built-in CrystalReportViewer it saves file on my desktop. So, you should send that file through smtp.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
I cannot use SMTP or I have to rebuild Outlook completly to give user access to his list of contacts!
Shahan AyyubSenior Software Engineer

Commented:
I tried to attach file like this but no good luck:

    Private Sub CrystalReportViewer1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Click
        CrystalReport11.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, "C:\Documents and Settings\Shahan.COREI3\Desktop\abc.rpt")
        Dim filename As String = "C:\Documents and Settings\Shahan.COREI3\Desktop\abc.rpt"
        Process.Start("mailto:eng.shahan@gmail.com")
        'Process.Start("mailto:eng.shahan@gmail.com /attachment:" + filename)  'not working
    End Sub

Does it shed some light on the solution??
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
the attachment argument is not supported by Outlook and many other applications. I might have found something that I will only be able to test tomorrow
Shahan AyyubSenior Software Engineer

Commented:
I have seen that this works if Outlook Express is set as default:

    Private Sub CrystalReportViewer1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Click
        CrystalReport11.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, "C:\Documents and Settings\Shahan.COREI3\Desktop\abc.rpt")
        Dim filename As String = "C:\Documents and Settings\Shahan.COREI3\Desktop\abc.rpt"
      Process.Start("mailto:eng.shahan@gmail.com?subject=Pictures from PhotoAlbum&cc=eng.shahan@gmail.com&body=This is the body text&attachment='" & filename "'")
    End Sub
Shahan AyyubSenior Software Engineer

Commented:
Okay I read your last comment.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
Nobody is using Outlook Express anymore!
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
Is your mail server running a MAPI mail program?
Crystal can only use MAPI for email.

mlmcc
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
It is not from the server it is from a workstation where (mostly) Outlook is installed. I need something that opens a new message with the report already attached that the user can complete (to, subject, message).

What I have seen is that CR sends the report/e-mail itself without showing the new message dialog to the user.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
I have something working as I want. I have added a button to my report viewer that save my report as a PDF and I use http://www.codeproject.com/KB/IP/SendFileToNET.aspx to open a new Outlook message dialog attaching the PDF automatically.
Shahan AyyubSenior Software Engineer

Commented:
I have seen this article last night but at that time you were talking to attach file in Outlook express so I rejected it. :S
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
>>you were talking to attach file in Outlook express

My only comment on OE was that nobody is using it anymore (at least in my users)! I nerver wanted to use it.
Shahan AyyubSenior Software Engineer

Commented:
>>> I wanted the Outlook (or default e-mail application) window to open a new message window with the report attached.


When you post this comment : ID:33138795 at that time I found this article.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Author

Commented:
Maybe I don't understand correctly but for me Outlook and Outlook Express are 2 very different products.
Shahan AyyubSenior Software Engineer

Commented:
May be I could not understand your requirement. But if you got a solution FINE!!!.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial