我是一个Excel VBA宏的初学者,在Outlook中进行第一次尝试,以下是我正在尝试完成的内容:
在Outlook 2010中,将宏分配给按钮,当按下该按钮时,
任何填写上面空白的指导将不胜感激。
编辑:
到目前为止,以下是最接近的解决方案,感谢David Zemens。但我认为我缺少一些参考,因为我的编译器不理解ClearClipboard()函数的“DataObject”。虽然如下所示可以复制和粘贴带有格式的内容到Word中(尽管我必须注释掉最后一个函数以避免错误):
在Outlook 2010中,将宏分配给按钮,当按下该按钮时,
- 获取活动电子邮件的整个正文
- 复制包括所有格式和HTML在内的正文到剪贴板上
- 打开新的Word文档
- 将剪贴板的内容粘贴到该Word文档中
- 清空剪贴板
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
任何填写上面空白的指导将不胜感激。
编辑:
到目前为止,以下是最接近的解决方案,感谢David Zemens。但我认为我缺少一些参考,因为我的编译器不理解ClearClipboard()函数的“DataObject”。虽然如下所示可以复制和粘贴带有格式的内容到Word中(尽管我必须注释掉最后一个函数以避免错误):
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