获取电子邮件标题,仅在Outlook中选择文本

时间:2013-12-31 10:41:58

标签: vba email outlook outlook-vba outlook-2007

我正在使用Office 2007和Windows 7.在下面的宏中,我从电子邮件中提取文本并在Chrome中打开文档。但是,我想要的不是提取整个文本,而只是选择文本和电子邮件标题(To,CC,Subject)。这可能吗?

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

修改

这是我的宏,它提取电子邮件标题(From,To,Subject ...)和电子邮件中的选定文本 - 但所选文本是原始文本,没有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

End Sub

1 个答案:

答案 0 :(得分:1)

打开Outlook和Visual Basic编辑器。单击F2以显示对象浏览器。向下滚动左侧列表(Classes)以查找并选择MailItem。右侧列表将显示MailItem的所有属性和方法。很多人,比如BCC,Body和CC,很明显,你必须要查找其他人。

点击CC。在这两个列表下方,您将看到:“属性 CC 为字符串”。

您尚未要求收件人,但向下滚动并选择收件人。现在,您将看到“Property 收件人作为收件人”,第二个“收件人”带有下划线并显示为绿色。您可以单击第二个收件人,但我怀疑在您更熟悉这些属性之前,您会发现它有用。在“帮助”中查找“收件人”,您将获得如何添加收件人的示例。要访问现有收件人,请将收件人视为数组(实际上是一个集合)。

CC,To和Subject都是字符串,因此很容易访问。

HtmlBody是一个字符串,因此很容易访问(就像你一样),但它是一个带有标签,属性,元素值等的Html字符串。提取您寻找的特定文本可能有点挑战性。如果你解释了你的想法,我可能会提供进一步的帮助。