Send word content in an email.

In VB.net, what is the easiest way to put the content of a word document into and email and preserve the formatting.
LVL 1
Un-ObtainiumAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
>>saving the word document as a html document does not change the result. any other suggestions?

if you read the newly saved file as a text file to provide it to your body, it will work. You have to forget the .Content.Text property.
0
 
Éric MoreauSenior .Net ConsultantCommented:
you would need to convert it as HTML but it can generate very ugly and not universal HTML.
0
 
Un-ObtainiumAuthor Commented:
I have tried the following, but it does not hold the formatting. How do I get the html from the word doc?
Imports System.Net.Mail
Imports Microsoft.Office.Interop
 
Public Function SendEmails(ByVal sEmailFrom As String, ByVal sEmailSubject As String, _
                            ByVal sSMTP As String, ByVal sTo As String, ByVal sCC As String(), _
                            ByVal sFileName As String) As Boolean
        Dim bResult As Boolean = True
        Dim oMessage As New MailMessage(sEmailFrom, sTo)
        Dim oSMTP As New SmtpClient
        oSMTP.Host = sSMTP
        oMessage.Subject = sEmailSubject
        For Each sCCAddress As String In sCC
            oMessage.CC.Add(sCCAddress)
        Next
        oMessage.IsBodyHtml = True
        oMessage.Body = GetWordContent(sFileName)
        Try
            oSMTP.Send(oMessage)
        Catch ex As Exception
            MsgBox(ex.Message)
            bResult = False
        End Try
 
        Return bResult
    End Function
 
Private Function GetWordContent(ByVal sFileName As String) As String
        Dim sResult As String = String.Empty
        Dim oWord As New Word.Application
        Dim oDoc As Word.Document
        oWord.Visible = False
        Try
            oDoc = oWord.Documents.Add(sFileName)
            sResult = oDoc.Content.Text
        Catch ex As Exception
 
        End Try
 
        Return sResult
    End Function

Open in new window

0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
Éric MoreauSenior .Net ConsultantCommented:
The reason is that Content.Text returns only the text (no formatting).

have a look at http://west-wind.com/weblog/posts/1299.aspx
or maybe you can save it as a HTML document (using the SaveAs method of Word)

I use Aspose components to do that kind of transformation: http://www.aspose.com/categories/file-format-components/aspose.words-for-.net-and-java/default.aspx
0
 
Un-ObtainiumAuthor Commented:
saving the word document as a html document does not change the result. any other suggestions?
0
 
Un-ObtainiumAuthor Commented:
That worked. But like you said in the begining the html produced is not perfect. It does not handle some special characters from the word document.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.