views:

130

answers:

2

I am attempting to generate the body of an email in Outlook 2007 from the text of a Word 2007 document in VB. I have access to both the Word and Outlook object libraries, and I can read from the Word document to get a string and write it to Outlook, but I need to keep the formatting from the Word document intact.

The purpose will be to allow users to edit the word document and always have the emails my program generates be in sync with the document.

Does anyone know how to do this?

+1  A: 

I finally got this working satisfactorally. It took some doing, so I thought I'd share what I ended up using.

Private Sub CreateEmail()
    Dim wordApp As Word.ApplicationClass = New Word.ApplicationClass()

    Dim file As Object = "PATH TO WORD DOCUMENT"

    Dim nullobj As Object = System.Reflection.Missing.Value

    Dim doc As Word.Document = wordApp.Documents.Open( _
                file, nullobj, nullobj, nullobj, nullobj, nullobj, _
                nullobj, nullobj, nullobj, nullobj, nullobj, nullobj)
    doc.ActiveWindow.Selection.WholeStory()
    doc.ActiveWindow.Selection.Copy()
    Dim data As IDataObject = Clipboard.GetDataObject
    body = data.GetData(DataFormats.Html, True).ToString
    Dim delimiter As Char() = "<".ToCharArray()
    body = "<" + (body.Split(delimiter, 3))(2)
    doc.Close()
    My.Computer.Clipboard.SetText(body)
    SendMail()
End Sub

Private Sub DisplayMail()
    Dim Errmsg As String

    Try
        If Len(mailto) = 0 Then
            Errmsg = "You must designate a recipient."
            MsgBox(Errmsg, MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        If GetOutlook() = True Then
            'Set the properties of the mail item
            mItem = CType(mOutlookApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
            mItem.Recipients.Add(mailto)
            mItem.BCC = bcc
            mItem.Subject = Me.subject
            mItem.HTMLBody = body

            'Save email to Outlook draft folder of the user
            mItem.Display()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
Caleb Thompson
-1. Not very efficient in that you're 1) opening an instance of Word when you don't have to, 2) you're using `WholeStory` without accounting for what it doesn't do [headers/footers], and 3) you're using the Windows Clipboard, thus emptying anything that is already there without restoring it.
Otaku
+1  A: 

Great stuff, to round off the code snippets here, if you fancy just sending the word doc as an attachment from a simple VBScript...

Dim outlook, nameSpace, mailItem

Set outlook = WScript.CreateObject("Outlook.Application")
Set nameSpace = outlook.GetNameSpace("MAPI")
Set mailItem = outlook.CreateItem(0)

mailItem.Recipients.Add "recipient@address"
mailItem.Subject = "Mail Subject"
mailItem.Body = "The body of the mail item" & vbcrlf & _ 
        "Put whatever you want in here!"

mailItem.Attachments.Add("\\FULLUNC\PATH\TO Your File\Called\Whatever.doc").DisplayName = "Attached File"

mailItem.Send

nameSpace.Logoff
blissapp