問題描述
通過 VBA 將格式化的電子郵件正文複製到新的 Word 文檔中並保留文本格式 (Copy formatted Email body into new Word document via VBA and keep the text formatting)
我需要將電子郵件正文複製到新的 Word 文檔中並保持文本格式。據我所知,我可以將電子郵件正文中的文本複製到 open Word 應用程序中。但我失去了格式。當我複制 HTMLBody 時,我看到了 HTML 標記...
第二個問題是:如何從 Outlook 啟動 Word 應用程序並在我的屏幕上看到它。
提前感謝您幫助我!
這是 Outlook 中的格式化電子郵件
並且這是我希望它在 Word 中得到的結果。<a href="
參考解法
方法 1:
Each MailItem
has a word document associated with it. You simply have to get hold of this document, copy its range and paste it into your wdDoc
Try this
Sub CopyToWord()
Dim wdApp As Object
Dim wdDoc As Object
Dim oRng As Object
Dim bStarted As Boolean
Dim olItem As MailItem
Dim wdItemWordEditor As Object '* A word document
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
bStarted = True
End If
On Error GoTo 0
'EDIT for Secondary question:
'Make Word application visible for the user
wdApp.Visible = True
For Each olItem In Application.ActiveExplorer.Selection
Set wdDoc = wdApp.Documents.Add
Set wdItemWordEditor = olItem.GetInspector.WordEditor
wdItemWordEditor.Range.Copy
wdDoc.Range.Paste
'wdDoc.Range.Text = olItem.HTMLBody
'wdDoc.Range.Text = olItem.Body
'wdDoc.Range.Text = olItem.Body
Next olItem
Set wdDoc = Nothing
Set wdApp = Nothing
Set olItem = Nothing
End Sub
(by klingu、Super Symmetry)