.NET从Word生成Outlook中的电子邮件文本

时间:2010-06-07 22:40:04

标签: vb.net outlook ms-word html-email

我试图从VB中的Word 2007文档文本生成Outlook 2007中的电子邮件正文。我可以访问Word和Outlook对象库,我可以从Word文档中读取以获取字符串并将其写入Outlook,但我需要保持Word文档的格式不变。

目的是允许用户编辑word文档,并始终让我的程序生成的电子邮件与文档同步。

有谁知道怎么做?

3 个答案:

答案 0 :(得分:1)

我终于令人满意地工作了。这花了一些时间,所以我想我会分享我最终使用的东西。

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

答案 1 :(得分:1)

很棒的东西,要在这里完善代码片段,如果您只是希望从简单的VBScript发送单词doc作为附件...

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

答案 2 :(得分:0)

以下是如何从Outlook VBA执行此操作的示例(添加对Word OM的引用)。您可以使用它来移植到.NET。

Sub CreateMail()
    Dim filePath As String
    filePath = """C:\\Users\\Me\\Desktop\\test.docx"""
    InsertBodyTextInOutlookWordEditor filePath
End Sub

Sub InsertBodyTextInOutlookWordEditor(filePath As String)
    Dim myMail As Outlook.MailItem
    Dim myInspector As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim wdRange As Word.Range

    On Error Resume Next
    Set myMail = Application.CreateItem(olMailItem)
    myMail.Subject = "Here's the latest..."
    myMail.Display
    Set myInspector = myMail.GetInspector
    Set wdDoc = myInspector.WordEditor
    If Not (wdDoc Is Nothing) Then
        Set wdRange = wdDoc.Range(0, wdDoc.Characters.Count)
        wdRange.Fields.Add Range:=wdRange, Type:=wdFieldEmpty, Text:= _
        "INCLUDETEXT  " & filePath, _
        PreserveFormatting:=True
    End If
End Sub