使用 VBA 宏选择和复制 Outlook 电子邮件正文 [英] Selecting and copying Outlook email body with a VBA macro
问题描述
我是 Excel 中 VBA 宏的初学者,这是 Outlook 中的第一次尝试,但我正在尝试这样做:
I'm a beginner to VBA macros in Excel, and this is the first attempt in Outlook, but here's what I am trying to do:
在 Outlook 2010 中,为按钮分配一个宏,当按下该按钮时,
In Outlook 2010, assign a macro to a button that, when pushed,
- 获取活动电子邮件的整个正文
- 将包含所有格式和 html 的正文复制到剪贴板
- 打开一个新的 Word 文档
- 将剪贴板的内容粘贴到这个词文档
- 清除剪贴板
到目前为止,我只有下面的第 1 步和第 3 步(我想知道我是否在第 1 步中以错误的方式进行了此操作):
So far, all I have are steps 1 and 3 (and I wonder if I'm going about this the wrong way in step 1) below:
Sub pasteToWord()
Dim activeMailMessage As Outlook.MailItem 'variable for email that will be copied.
Dim activeBody
Dim clearIt As String 'Intended to eventually clear clipboard.
'Code to get to the body of the active email.
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then _
Set activeMailMessage = ActiveExplorer.Selection.Item(1)
activeBody = activeMailMessage.Body
'MsgBox activeBody
'^This displayed what I want in plaintext form,
'so I think im on the right track
'Code to copy selection to clipboard
'Code to open new Word doc
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Add
WordApp.Visible = True
'Code to paste contents of clipboard to active word document
'Code to clear clipboard
End Sub
非常感谢您提供填写上述空白的任何指导.
Any guidance to fill in the blanks above would be much appreciated.
感谢 David Zemens,这是迄今为止最接近的.我想我缺少一些参考,因为我的编译器不理解 ClearClipboard() 函数的DataObject".它确实复制并粘贴到带有格式的 word 中,如下所示(尽管我不得不注释掉最后一个函数以避免错误):
Here is what has come the closest so far, thanks to David Zemens. I think I am missing some reference though, because my compiler doesn't understand "DataObject" for the ClearClipboard() function. It does copy and paste into word with formatting though, as is below (though I had to comment out the last function to avoid errors):
Sub pasteToWord()
Dim WordApp As Word.Application 'Need to link Microsoft Word Object library
Dim wdDoc As Word.Document 'for these to be understood by compiler
Dim activeMailMessage As Outlook.MailItem
Dim activeBody As String
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
'Get a handle on the email
Set activeMailMessage = ActiveExplorer.Selection.Item(1)
'Ensure Word Application is open
Set WordApp = CreateObject("Word.Application")
'Make Word Application visible
WordApp.Visible = True
'Create a new Document and get a handle on it
Set wdDoc = WordApp.Documents.Add
'Copy the formatted text:
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'Paste to the word document
wdDoc.Range.Paste
'Clear the clipboard entirely:
Call ClearClipBoard
End If
End Sub
Public Sub ClearClipBoard()
Dim oData As New DataObject 'object to use the clipboard -- Compiler error,
'I think I'm missing a reference here.
oData.SetText Text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
End Sub
推荐答案
此方法会复制所选邮件项中的格式化文本,并将其粘贴到word文档中:
This method will copy the formatted text from the selected mailitem, and paste it in to word document:
Dim WordApp As Word.Application
Dim wdDoc As Word.Document
Dim activeMailMessage As MailItem
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
'Get a handle on the email
Set activeMailMessage = ActiveExplorer.Selection.Item(1)
'Ensure Word Application is open
Set WordApp = CreateObject("Word.Application")
'Make Word Application visible
WordApp.Visible = True
'Create a new Document and get a handle on it
Set wdDoc = WordApp.Documents.Add
'Copy the formatted text:
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'Paste to the word document
wdDocument.Range.Paste
'Clear the clipboard entirely:
Call ClearClipBoard
End If
注意 使用 类似于此处描述的功能:
Public Sub ClearClipBoard()
Dim oData As New DataObject 'object to use the clipboard
oData.SetText Text:=Empty 'Clear
oData.PutInClipboard 'take in the clipboard to empty it
End Sub
这篇关于使用 VBA 宏选择和复制 Outlook 电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!