.NET Generate email text in Outlook from Word
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?
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
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
Here's an example of how you can do this from Outlook VBA (adding a reference to Word OM). You can use this to port to .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
精彩评论