Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Send word content in an email.

Posted on 2009-02-11
6
Medium Priority
?
328 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.
0
Comment
Question by:Un-Obtainium
  • 3
  • 3
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23613303
you would need to convert it as HTML but it can generate very ugly and not universal HTML.
0
 
LVL 1

Author Comment

by:Un-Obtainium
ID: 23613585
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 23613942
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Un-Obtainium
ID: 23614420
saving the word document as a html document does not change the result. any other suggestions?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1500 total points
ID: 23614661
>>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
 
LVL 1

Author Comment

by:Un-Obtainium
ID: 23615165
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Integration Management Part 2

804 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question