Reputation: 7207
I am using Office 2007 and Windows 7. With macro below, I extract text from email and open document in Chrome. But, what I want is not to extract whole text, but just selected text and the email header (To, CC, Subject). Is this possible ?
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
Edit:
Here's my macro which extracts email header (From, To, Subject ...) and selected text in email - but selected text is raw text, without 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
Upvotes: 0
Views: 4662
Reputation: 12403
Open Outlook and the Visual Basic Editor. Click F2 to display the Object Browser. Scroll down the left hand list (Classes) to find and select MailItem. The right hand list will display all the properties and methods of a MailItem. Many, such as BCC, Body and CC, will be obvious others you will have to look up.
Click on CC. Underneath the two lists you will see: "Property CC As String".
You have not asked for Recipients but scroll down and select Recipients. Now you will see "Property Recipients as Recipients" with the second "Recipients" underlined and coloured green. You can click the second Recipients but I doubt you will find it helpful until you are more familiar with these properties. Look up "Recipients" in Help and you will get an example of how to add a recipient. To access the existing recipients you treat Recipients as an array (its actually a collection).
CC, To and Subject are all strings so are easy to access.
HtmlBody is a string so is easy to access (as you have) but it is an Html string with tags, attributes, element values and so on. Extracting the particular text you seek may be a little challenging. If you explain what you are after, I may be able to offer further help.
Upvotes: 1