How to export web page to word file and send it as an emailattachment?

Discussion in 'ASP .Net' started by Author #1, Aug 9, 2009.

  1. Author #1

    Author #1 Guest

    In my asp.net 3.5 application, I need to export part of a web page to
    MS Word format and send it as email attachment.

    I know how to export to MS Word and download it to desktop. But, I am
    not sure how I can capture the Word file stream in my code behind and
    send it as an email attachment.

    Any hint is highly appreciated. Thanks.
    Author #1, Aug 9, 2009
    #1
    1. Advertising

  2. Author #1

    Author #1 Guest

    On Aug 9, 5:24 am, "Mark Rae [MVP]" <> wrote:
    > "Author #1" <> wrote in message
    >
    > news:...
    >
    > > In my asp.net 3.5 application, I need to export part of a web page to
    > > MS Word format and send it as email attachment.

    >
    > > I know how to export to MS Word and download it to desktop.

    >
    > How are you doing that...?
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Thank you. I am using the example given at http://aspalliance.com/794

    I can put the string value (e.g. strMyAttachment) into a MemoryStream
    object and create an Attachment as

    UTF8Encoding utf8 = new UTF8Encoding();
    byte[] myAttachmentBytes = utf8.GetBytes(strMyAttachment);
    MemoryStream myAttachmentStream = new MemoryStream
    (myAttachmentBytes );
    Attachment attachment = new Attachment(myAttachmentStream ,
    "myattachment.doc");
    attachment.ContentType = new System.Net.Mime.ContentType
    ("application/msword");

    Then add this attachment object to my MailMessage object and send it.
    This is OK.

    The problem: After I receive the email, I download the attachment to
    my desktop, open it in MS Word, only to see the file presented in raw
    HTML code with HTML tags. Users don't want to see this, they want to
    see it rendered properly.

    The button click event handler code is pasted here (same as that in
    http://aspalliance.com/794).

    protected void btnCreateWordDoc_Click(object sender, EventArgs e)
    {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/
    msword";

    string strFileName = "GenerateDocument" + ".doc";
    HttpContext.Current.Response.AddHeader("Content-
    Disposition",
    "inline;filename=" + strFileName);

    StringBuilder strHTMLContent = new StringBuilder();

    strHTMLContent.Append(" <h1 title='Heading'
    align='Center'style='font-family: verdana; font -size: 80 % ; color:
    black'><u>Document Heading</u> < / h1 > ".ToString());

    strHTMLContent.Append("<br>".ToString());
    strHTMLContent.Append(
    "<table align='Center'>".ToString());

    // Row with Column headers
    strHTMLContent.Append("<tr>".ToString());
    strHTMLContent.Append("<td style='width:100px; background:
    #99CC00'><b>Column 1 < / b > < / td >".ToString());
    strHTMLContent.Append("<td style='width:100px;background:
    #99CC00'><b>Column 2 </b></td>".ToString());
    strHTMLContent.Append("<tdstyle='width:100px; background:
    #99CC00'><b>Column 3</b></td>".ToString());
    strHTMLContent.Append("</tr>".ToString());

    // First Row Data
    strHTMLContent.Append("<tr>".ToString());
    strHTMLContent.Append("<td style='width:100px'>a</
    td>".ToString());
    strHTMLContent.Append("<td style='width:100px'>b</
    td>".ToString());
    strHTMLContent.Append("<td style='width:100px'>c</
    td>".ToString());
    strHTMLContent.Append("</tr>".ToString());

    // Second Row Data
    strHTMLContent.Append("<tr>".ToString());
    strHTMLContent.Append("<td style='width:100px'>d</
    td>".ToString());
    strHTMLContent.Append("<td style='width:100px'>e</
    td>".ToString());
    strHTMLContent.Append("<td style='width:100px'>f</
    td>".ToString());
    strHTMLContent.Append("</tr>".ToString());
    strHTMLContent.Append("</table>".ToString());

    strHTMLContent.Append("<br><br>".ToString());
    strHTMLContent.Append("<p align='Center'> Note : This is
    adynamically generated word document </p> ".ToString());
    HttpContext.Current.Response.Write(strHTMLContent);
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();
    }
    Author #1, Aug 10, 2009
    #2
    1. Advertising

  3. Author #1

    Author #1 Guest

    On Aug 10, 5:49 pm, "Mark Rae [MVP]" <> wrote:
    > "Author #1" <> wrote in message
    >
    > news:...
    >
    > >>> I know how to export to MS Word and download it to desktop.

    >
    > >> How are you doing that...?

    >
    > > Thank you.  I am using the example given athttp://aspalliance.com/794

    >
    > Just to be clear, that method doesn't actually create a Word document - it
    > creates an HTML document and gives it a .doc extension for displaying in a
    > web browser. This technique fools the browser into thinking that it's
    > receiving a Word document, but it most certainly does *not* create a native
    > Word document...
    >
    > This is fine for creating a Word "document" for viewing in a web browser.
    > However, whenever I need to create a proper Word document (e.g. for
    > emailing), I use this:http://www.aspose.com/categories/file-format-components/aspose.words-...
    >
    > Under no circumstances contemplate using server-side Office automation for
    > this, as it's not supported by Microsoft because Office wasn't designed
    > (currently, anyway!) to be used this way:http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2Note
    > specifically the bold paragraph.
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Thank you. Yes, I agree with you. That code sample simply .doc-
    extension-s an HTML file. (I am using .doc-extension as a verb).

    To give you more details. On my web page in question, I have two
    buttons:

    [ Export to Word ] [ Send To Boss ]

    If a user clicks the [ Export to Word ] button, the browser asks the
    user to save the file to hard drive. After the user saves it to the
    hard drive, if s/he opens it in MS Word, the file displays perfectly
    just like in a browser. In other words, the raw HTML tags are not
    shown.

    If the user clicks the [ Send To Boss ] button, the HTML string gets
    sent to the boss's email as an .doc-extensioned attachment. Perfect.
    But, after the boss saves the attachment, he opens it in MS Word, MS
    Word displays it as raw HTML string with the ugly HTML tags!

    The aspose.com tool is certainly a solution to this problem, but I
    don't have the funds.

    So, I am running into a dead end? There is no solution to this
    problem? Any further ideas are highly appreciated. Thanks.
    Author #1, Aug 11, 2009
    #3
  4. Author #1

    Author #1 Guest

    On Aug 11, 12:52 pm, "Mark Rae [MVP]" <> wrote:
    > "Author #1" <> wrote in message
    >
    > news:...
    >
    > > There is no solution to this problem?

    >
    > I've never found one. Having said that, since using Aspose I've never needed
    > to look for one...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    OK, thanks. I am curious about this: If we save it to desktop from
    the [Export to Word] button and open it in Word, MS Word displays it
    just fine. But if we send the same HTML string as an .doc-extensioned
    email attachment, then we save it from our email client, open it in MS
    Word, MS Word displays raw HTML tags. Do you have an idea about this
    discrepancy? Thanks.
    Author #1, Aug 11, 2009
    #4
  5. Author #1

    Author #1 Guest

    On Aug 12, 5:39 am, "Mark Rae [MVP]" <> wrote:
    > "Author #1" <> wrote in message
    >
    > news:...
    >
    > > OK, thanks.  I am curious about this: If we save it to desktop from
    > > the [Export to Word] button and open it in Word, MS Word displays it
    > > just fine.  But if we send the same HTML string as an .doc-extensioned
    > > email attachment, then we save it from our email client, open it in MS
    > > Word, MS Word displays raw HTML tags.  Do you have an idea about this
    > > discrepancy?

    >
    > I can only assume that it's something that your email client is doing.
    >
    > I'd suggest using a file comparison utility to compare the two different
    > files and see what the differences are and then contact the supplier of your
    > email client...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    I doubt it has anything to do with the email client. I tried both
    gmail and Mozilla ThunderBird. Same behavior: download it from email,
    open it in MS Word and MS Word displays raw HTML tags.

    *My guess* (I am not sure how MS Word works)

    When a user clicks [Export to Word], the code behind adds some
    response header to it as shown in the code I pasted. Such header
    information is stored in the downloaded file, and when MS Word opens
    it, such information actually instructs MS Word to display it as a web
    page.

    However, when we simply send this HTML string as an attachment, such
    response header information is missing in this attachment, and
    therefore, nothing tells MS Word to display the file as a web page.
    As a result, MS Word displays as a pure text file.

    That said, I've tried adding content type and content disposition into
    the attachment, but it didn't help. Maybe something else is missing.

    Does my conjecture sound reasonable?
    Author #1, Aug 14, 2009
    #5
  6. Author #1

    Author #1 Guest

    On Aug 14, 10:02 am, "Mark Rae [MVP]" <> wrote:
    > "Author #1" <> wrote in message
    >
    > news:...
    >
    > > I'd suggest using a file comparison utility to compare the two different
    > > files and see what the differences are and then contact the supplier of
    > > your
    > > email client...

    >
    > What were the results of that...?
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    I tried Diff Doc, but the only thing it does is to compare the text.
    So I gave it up.

    Then, I checked the HTML source of the two copies of files:

    1) file_thru_export_button.doc
    2) file_thru_email_attachment.doc

    Guess what, file_thru_export_button.doc is a nicely formatted HTML
    file like this: <html><head></head><body> my HTML contents are here </
    body></html>
    whereas file_thru_email_attachment.doc does not have <html><head></
    head><body> and </body></html> to wrap up my HTML contents.

    Once I edit file_thru_email_attachment.doc to wrap up my HTML contents
    with <html><head></head><body> and </body></html>, MS Word displays it
    perfectly. It is obvious, as as long as your document is a well-
    formatted HTML doc, MS Word will work as a browser.

    Demystified and easy! Thank you very much!
    Author #1, Aug 14, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,588
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  2. =?Utf-8?B?RXJpayBLLiBTY2hpbmRlbGRlY2tlcg==?=

    Export datagrid to Word, force page break

    =?Utf-8?B?RXJpayBLLiBTY2hpbmRlbGRlY2tlcg==?=, Oct 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    899
    Steve C. Orr [MVP, MCSD]
    Oct 3, 2006
  3. Steve
    Replies:
    6
    Views:
    613
    Eric Layman
    Mar 3, 2007
  4. Replies:
    1
    Views:
    1,487
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jul 2, 2008
  5. Ami
    Replies:
    5
    Views:
    355
    Bob Barrows [MVP]
    Feb 4, 2008
Loading...

Share This Page