通過 VBA 將格式化的電子郵件正文複製到新的 Word 文檔中並保留文本格式 (Copy formatted Email body into new Word document via VBA and keep the text formatting)


問題描述

通過 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 klinguSuper Symmetry)

參考文件

  1. Copy formatted Email body into new Word document via VBA and keep the text formatting (CC BY‑SA 2.5/3.0/4.0)

#outlook #ms-word #vba






相關問題

Outlook 2007/2010 中的 Vspace (Vspace in Outlook 2007/2010)

Outlook 2010 自動完成流(緩存的聯繫人) (Outlook 2010 autocomplete stream ( cached contacts))

如何格式化電子郵件中的字符串以便 Outlook 打印換行符? (How do I format a String in an email so Outlook will print the line breaks?)

通過 Office 加載項將內容添加到 Outlook 電子郵件正文 (Adding content to an Outlook email body via an Office Add-In)

Outlook Addin 中的 Recipient.Name 和 ExchangeUser.Name 有什麼區別? (What is the Difference Between Recipient.Name and ExchangeUser.Name in Outlook Addin?)

運行時錯誤,用於電子郵件自動化的 VBA EXCEL (Runtime Error, VBA EXCEL for email automation)

將 Crystal Reports 轉換為 HTML 並作為正文發送到我的郵件中 (Crystal Reports into HTML and sending in my mail as body)

通過 VBA 將格式化的電子郵件正文複製到新的 Word 文檔中並保留文本格式 (Copy formatted Email body into new Word document via VBA and keep the text formatting)

Outlook 在啟動時忽略加載項的 HKEY_LOCAL_MACHINE 條目 (Outlook ignoring HKEY_LOCAL_MACHINE entry for add-in on startup)

如何知道是否收到或發送了電子郵件? (How to know if an e-mail was received or sent?)

如何從 Excel 表格創建電子郵件? (How to create emails from Excel table?)

如何在 Outlook 正文中左對齊從 Excel 範圍創建的 HTML 表格? (How to Left-Align HTML Table, created from an Excel Range, in an Outlook Body?)







留言討論