Vba 获取outlook中的电子邮件标题和选定文本
我正在使用Office2007和Windows7。使用下面的宏,我从电子邮件中提取文本并在Chrome中打开文档。但是,我想要的不是提取整个文本,而是选择的文本和电子邮件标题(收件人、抄送、主题)。这可能吗Vba 获取outlook中的电子邮件标题和选定文本,vba,email,outlook,outlook-2007,Vba,Email,Outlook,Outlook 2007,我正在使用Office2007和Windows7。使用下面的宏,我从电子邮件中提取文本并在Chrome中打开文档。但是,我想要的不是提取整个文本,而是选择的文本和电子邮件标题(收件人、抄送、主题)。这可能吗 Sub OpenInBrowser() Dim BrowserLocation As String Dim AlwaysConvert As Boolean Dim EvaluateHTML As Boolean '=============Set you
Sub OpenInBrowser()
Dim BrowserLocation As String
Dim AlwaysConvert As Boolean
Dim EvaluateHTML As Boolean
'=============Set your variables in the section below==========================
'The default settings are optimized for viewing newsletters and receiving
'messages with HTML forms or animated gif-files embedded in the message.
'Set the location of the executable of the browser you want to use.
BrowserLocation = "C:\Program Files\Google\Chrome\Application\chrome.exe"
'When set to True, we will let Outlook convert the message to HTML.
'The message will be opened in the configured browser just as it
'appears in Outlook.
'Standard value: False
AlwaysConvert = False
'When set to True, we will look for embedded resources in the HTML message and
'determine whether Outlook should convert the message or whether we can strip
'the HTML directly. When set to False, we will always strip the HTML and ignore
'embedded resources.
'For this setting to take effect, AlwaysConvert must be set to False.
'Standard value: True
EvaluateHTML = True
'=======Don't modify the code below unless you know what you are doing=========
'Get the user's TempFolder to store the item in
Dim FSO As Object, TmpFolder As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set TempFolder = FSO.GetSpecialFolder(2)
'Get all selected items
Dim MyOlNamespace As Outlook.NameSpace
Set MyOlNamespace = Application.GetNamespace("MAPI")
Set MyOlSelection = Application.ActiveExplorer.Selection
'Make sure at least one item is selected
If MyOlSelection.Count = 0 Then
Response = MsgBox("Please select an item first", vbExclamation, MyApplName)
Exit Sub
End If
'Make sure only one item is selected
If MyOlSelection.Count > 1 Then
Response = MsgBox("Please select only one item", vbExclamation, MyApplName)
Exit Sub
End If
'Retrieve the selected item
Set MyselectedItem = MyOlSelection.Item(1)
'construct the filename
Dim FileName As String
strname = "www_howto-outlook_com"
FileName = TempFolder & "\" & strname & ".htm"
'If the message is in HTML format we directly capture the HTML from the message
'to construct our htm-file. This will allow us to capture as many HTML elements
'as possible. If it is a different format, or if the HTML mail includes embedded
'resources we let Outlook convert it to HTML.
Dim OutlookConvert As Boolean
OutlookConvert = True
If MyselectedItem.BodyFormat = olFormatHTML And AlwaysConvert = False Then
Dim rawHTML As String
rawHTML = MyselectedItem.HTMLBody
If EvaluateHTML = False Then
OutlookConvert = False
Else
'Check if there are embedded resources in the message.
'If it does, we let Outlook convert the message.
If InStr(UCase(rawHTML), UCase("src=""cid:")) = 0 Then
OutlookConvert = False
End If
End If
End If
'Write the temp-file
If OutlookConvert = False Then
'create the htm-file in the temp folder and write the HTML code to it
Set objFile = FSO.CreateTextFile(FileName, True)
objFile.Write "" & rawHTML
objFile.Close
Set objFile = Nothing
Else
'let Outlook convert the message and save the selected item
'as htm to the temp folder
MyselectedItem.SaveAs FileName, olHTML
End If
'open the saved item in the browser
Shell BrowserLocation & " " & FileName, vbNormalFocus
'Cleanup
Set FSO = Nothing
Set MyOlNamespace = Nothing
Set MyOlSelection = Nothing
Set MyselectedItem = Nothing
End Sub
编辑:
这是我的宏,它提取电子邮件标题(从,到,主题…)和电子邮件中的选定文本-但选定文本是原始文本,没有HTML
Sub OpenInBrowser()
Dim BrowserLocation As String
Dim AlwaysConvert As Boolean
Dim EvaluateHTML As Boolean
'=============Set your variables in the section below==========================
'The default settings are optimized for viewing newsletters and receiving
'messages with HTML forms or animated gif-files embedded in the message.
'Set the location of the executable of the browser you want to use.
'Standard value: "C:\Program Files\Internet Explorer\iexplore.exe"
BrowserLocation = "C:\Program Files\Google\Chrome\Application\chrome.exe"
'When set to True, we will let Outlook convert the message to HTML.
'The message will be opened in the configured browser just as it
'appears in Outlook.
'Standard value: False
AlwaysConvert = False
'When set to True, we will look for embedded resources in the HTML message and
'determine whether Outlook should convert the message or whether we can strip
'the HTML directly. When set to False, we will always strip the HTML and ignore
'embedded resources.
'For this setting to take effect, AlwaysConvert must be set to False.
'Standard value: True
EvaluateHTML = True
'=======Don't modify the code below unless you know what you are doing=========
'Get the user's TempFolder to store the item in
Dim FSO As Object, TmpFolder As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set TempFolder = FSO.GetSpecialFolder(2)
'Get all selected items
Dim MyOlNamespace As Outlook.NameSpace
Set MyOlNamespace = Application.GetNamespace("MAPI")
Set MyOlSelection = Application.ActiveExplorer.Selection
'Make sure at least one item is selected
If MyOlSelection.Count = 0 Then
Response = MsgBox("Please select an item first", vbExclamation, MyApplName)
Exit Sub
End If
'Make sure only one item is selected
If MyOlSelection.Count > 1 Then
Response = MsgBox("Please select only one item", vbExclamation, MyApplName)
Exit Sub
End If
'Retrieve the selected item
Set MyselectedItem = MyOlSelection.Item(1)
'construct the filename
Dim FileName As String
strname = "header_printing"
FileName = TempFolder & "\" & strname & ".htm"
'If the message is in HTML format we directly capture the HTML from the message
'to construct our htm-file. This will allow us to capture as many HTML elements
'as possible. If it is a different format, or if the HTML mail includes embedded
'resources we let Outlook convert it to HTML.
Dim OutlookConvert As Boolean
OutlookConvert = True
Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Dim rng As String
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem
If insp.EditorType = olEditorWord Then ' outlook 2013
Set hed = msg.GetInspector.WordEditor
Set word = hed.Application
rng = word.Selection.Text
End If
End If
If MyselectedItem.BodyFormat = olFormatHTML And AlwaysConvert = False Then
Dim rawHTML As String
Dim textBody
If rng = "" Or Len(rng) < 3 Then 'sometimes one letter is selected by it self
textBody = MyselectedItem.HTMLBody
Else
textBody = rng
End If
'Email header - to, cc, bcc, subject and selected text from body
rawHTML = "<b><font size=4>" & MyselectedItem.Subject & "</b><br/>" & _
MyselectedItem.SenderName & " [" & MyselectedItem.SenderEmailAddress & "]" & "</font><br/>" & _
"<b>Sent: </b>" & MyselectedItem.SentOn & "<br/>" & _
"<b>From: </b>" & MyselectedItem.SenderName & " [" & MyselectedItem.SenderEmailAddress & "]<br/>" & _
"<b>To: </b>" & MyselectedItem.To & "<br/>" & _
"<b>Subject: </b>" & MyselectedItem.Subject & "<br/>" & _
"<hr>" & _
textBody
If EvaluateHTML = False Then
OutlookConvert = False
Else
'Check if there are embedded resources in the message.
'If it does, we let Outlook convert the message.
If InStr(UCase(rawHTML), UCase("src=""cid:")) = 0 Then
OutlookConvert = False
End If
End If
End If
'Write the temp-file
If OutlookConvert = False Then
'create the htm-file in the temp folder and write the HTML code to it
Set objFile = FSO.CreateTextFile(FileName, True)
objFile.Write "" & rawHTML
objFile.Close
Set objFile = Nothing
Else
'let Outlook convert the message and save the selected item
'as htm to the temp folder
MyselectedItem.SaveAs FileName, olHTML
End If
'open the saved item in the browser
Shell BrowserLocation & " " & FileName, vbNormalFocus
'Cleanup
Set FSO = Nothing
Set MyOlNamespace = Nothing
Set MyOlSelection = Nothing
Set MyselectedItem = Nothing
子OpenInBrowser()
Dim BrowserLocation作为字符串
Dim始终转换为布尔值
Dim EvaluateHTML作为布尔值
'================在下面的部分中设置变量==========================
'默认设置针对查看新闻稿和接收信息进行了优化
'邮件中嵌入HTML表单或动画gif文件。
'设置要使用的浏览器的可执行文件的位置。
'标准值:“C:\Program Files\Internet Explorer\iexplore.exe”
BrowserLocation=“C:\Program Files\Google\Chrome\Application\Chrome.exe”
'设置为True时,我们将让Outlook将邮件转换为HTML。
'消息将在配置的浏览器中打开,就像它
'将显示在Outlook中。
'标准值:假
AlwaysConvert=False
'设置为True时,我们将在HTML消息和
'确定Outlook是否应转换邮件,或者我们是否可以删除邮件
'直接使用HTML。当设置为False时,我们将始终剥离HTML并忽略它
"嵌入式资源。
'要使此设置生效,AlwaysConvert必须设置为False。
'标准值:真
EvaluateHTML=True
'=========除非您知道自己在做什么,否则不要修改下面的代码=========
'获取用于存储项目的用户临时文件夹
尺寸FSO作为对象,TmpFolder作为对象
设置FSO=CreateObject(“scripting.filesystemobject”)
Set TempFolder=FSO.GetSpecialFolder(2)
'获取所有选定项目
将MyOlNamespace设置为Outlook.NameSpace
设置MyOlNamespace=Application.GetNamespace(“MAPI”)
设置MyOlSelection=Application.ActiveExplorer.Selection
'确保至少选择了一项
如果MyOlSelection.Count=0,则
Response=MsgBox(“请先选择一个项目”,VBEQUOTION,MyApplName)
出口接头
如果结束
'确保只选择了一项
如果MyOlSelection.Count>1,则
Response=MsgBox(“请仅选择一项”,VBEQUOTION,MyApplName)
出口接头
如果结束
'检索所选项目
设置MyselectedItem=myelselection.Item(1)
'构造文件名
将文件名设置为字符串
strname=“页眉打印”
FileName=TempFolder&“\”&strname&“.htm”
'如果消息是HTML格式,我们直接从消息中捕获HTML
'来构造我们的htm文件。这将允许我们捕获尽可能多的HTML元素
“尽可能。如果是另一种格式,或者HTML邮件包含嵌入的
“我们让Outlook将其转换为HTML。
Dim OutlookConvert As Boolean
OutlookConvert=True
将消息作为Outlook.mailtim
高级督察
作为字符串的Dim rng
设置insp=Application.ActiveInspector
如果insp.CurrentItem.Class=olMail,则
设置msg=insp.CurrentItem
如果insp.EditorType=olEditorWord,则“outlook 2013
设置hed=msg.GetInspector.WordEditor
Set word=hed.Application
rng=word.Selection.Text
如果结束
如果结束
如果MyselectedItem.BodyFormat=olFormatHTML且AlwaysConvert=False,则
将HTML设置为字符串
暗文本体
如果rng=“”或Len(rng)<3,则“有时一个字母由它自己选择
textBody=MyselectedItem.HTMLBody
其他的
textBody=rng
如果结束
'电子邮件标题-收件人、抄送、密件抄送、主题和正文中的选定文本
rawHTML=”“&MyselectedItem.Subject&“
”和_
MyselectedItem.SenderName&“[”&MyselectedItem.SenderEmailAddress&“]”&“
”和_
“已发送:”&MyselectedItem.SentOn&“
”和_
“发件人:”&MyselectedItem.SenderName&“[”&MyselectedItem.SenderEmailAddress&“]
”和_
“收件人:”&MyselectedItem.To&“
”和_
“主题:”&MyselectedItem.Subject&“
”和_
“
”和_
正文
如果EvaluateHTML=False,则
OutlookConvert=False
其他的
'检查消息中是否有嵌入的资源。
'如果确实如此,我们将让Outlook转换邮件。
如果InStr(UCase(rawHTML),UCase(“src=”“cid:”)=0,则
OutlookConvert=False
如果结束
如果结束
如果结束
'写入临时文件
如果OutlookConvert=False,则
'在临时文件夹中创建htm文件,并将HTML代码写入其中
设置objFile=FSO.CreateTextFile(文件名,True)
objFile.Write”“&rawHTML
objFile.Close
设置objFile=Nothing
其他的
'让Outlook转换邮件并保存所选项目
'作为htm添加到临时文件夹
MyselectedItem.SaveAs文件名,olHTML
如果结束
'在浏览器中打开保存的项目
Shell BrowserLocation&“”&文件名,vbNormalFocus
"清理",
设置FSO=无
设置MyOlNamespace=Nothing
设置MyOlSelection=Nothing
设置MyselectedItem=Nothing
结束子菜单打开Outlook和Visual Basic编辑器。单击F2以显示对象浏览器。向下滚动左侧列表(类)以查找并选择MailItem。右侧列表将显示MailItem的所有属性和方法。许多,如密件抄送,正文和抄送,将是显而易见的其他你将不得不查找 点击CC。在这两个列表下面,您将看到:“属性CC作为字符串” 您没有要求收件人,但向下滚动并选择收件人。现在您将看到“属性收件人作为收件人”,第二个“收件人”带下划线,颜色为绿色。您可以单击第二个收件人,但我怀疑在您更熟悉这些属性之前,它是否会有所帮助。在“帮助”中查找“收件人”,您将获得如何添加收件人的示例。要访问现有收件人,请将收件人视为一个数组(实际上是一个集合) 抄送、发送和受ar约束