我試圖從VB中的Word 2007文檔的文本生成Outlook 2007中的電子郵件正文。我可以訪問Word和Outlook對象庫,並且可以從Word文檔讀取字符串並將其寫入Outlook,但我需要保持Word文檔格式不變。.NET在Word中從Outlook生成電子郵件文本
其目的是允許用戶編輯Word文檔並始終讓我的程序生成的電子郵件與文檔同步。
有誰知道如何做到這一點?
我試圖從VB中的Word 2007文檔的文本生成Outlook 2007中的電子郵件正文。我可以訪問Word和Outlook對象庫,並且可以從Word文檔讀取字符串並將其寫入Outlook,但我需要保持Word文檔格式不變。.NET在Word中從Outlook生成電子郵件文本
其目的是允許用戶編輯Word文檔並始終讓我的程序生成的電子郵件與文檔同步。
有誰知道如何做到這一點?
我終於得到了這個令人滿意的工作。它花了一些時間,所以我想我會分享我最終使用的。
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
下面是如何從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
偉大的東西,在這裏四捨五入的代碼片段,如果你看中是在發送Word文檔作爲從簡單的VBScript附件...
Dim outlook, nameSpace, mailItem
Set outlook = WScript.CreateObject("Outlook.Application")
Set nameSpace = outlook.GetNameSpace("MAPI")
Set mailItem = outlook.CreateItem(0)
mailItem.Recipients.Add "[email protected]"
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
-1。不是非常有效,因爲你1)打開一個Word的實例,當你不需要時,2)你正在使用'WholeStory'而沒有考慮它不會做什麼[header/footers],3)你正在使用Windows剪貼板,從而清空已經存在的任何內容,而不用恢復它。 – 2010-06-24 19:19:40