We help IT Professionals succeed at work.

Send word content in an email.

Medium Priority
346 Views
Last Modified: 2013-11-15
In VB.net, what is the easiest way to put the content of a word document into and email and preserve the formatting.
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
you would need to convert it as HTML but it can generate very ugly and not universal HTML.

Author

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

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
saving the word document as a html document does not change the result. any other suggestions?
Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.