Display HTML email in Internet Explorer (1 click work around for2007)

K

Ken

The VBA code below can be used to open HTML emails in Internet
Explorer. It is essentially the same as opening the Email, Click
Other Actions, View in Browser. I got a start from the code posted on
http://www.howto-outlook.com/howto/openinbrowser.htm This was a quick
and dirty because I needed something quick. We use a web based
calendar and we needed the buttons in emails for accepting meetings to
work. This accomplished that. I have done very little testing on
it. Any improvements are welcome. Hope it helps.


1. Click Alt F11 to open the Visual Basic Editor

2. Expand Project1
3. Expand Microsoft Office Outlook
4. Double Click ThisOutlookSession

5. Paste the following code in the window on the right

--------------------------------------------------------Start
Code-------------------------------------------------------------
Sub OpenInBrowser()

Dim BrowserLocation As String
'Dim Response As Integer

'Set the location of the executable of the browser you want to use
BrowserLocation = "C:\Program Files\Internet Explorer
\iexplore.exe"

'Get the user's TempFolder to store the item in
Dim FSO As Object, TmpFolder As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set FileName = 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

Dim MySelectedItem As MailItem
Dim sHTML As String
'Retrieve the selected item
Set MySelectedItem = myolselection.Item(1)


sHTML = MySelectedItem.HTMLBody


'save the selected item as htm to the temp folder
Dim sFolderName As String
Dim SaveFile As Object

strname = "www_howto-outlook_com"
FileName = FileName & "\" & strname & ".htm"
sFolderName = FSO.GetTempName
If FSO.fileexists(FileName) Then
Set SaveFile = FSO.OpenTextFile(FileName, 2, False)
Else
Set SaveFile = FSO.OpenTextFile(FileName, 2, True)
End If

SaveFile.Write sHTML
SaveFile.Close



'open the saved item in the browser
Shell BrowserLocation & " " & FileName, vbNormalFocus

'Cleanup
Set FSO = Nothing
Set FileName = Nothing
Set MyOlNamespace = Nothing
Set myolselection = Nothing
Set MySelectedItem = Nothing

End Sub
 
R

Roady [MVP]

Thanks for crediting my solution but why copy the contents of my webpage
into your post?
This is not proper etiquette.
Why didn't you comment your code changes and motivation for it?

Don't get me wrong; you can freely use and modify the code provided on my
webpage but when you are going to repost it publicly, there are better and
more proper ways to do so. Not properly separating the reference and your
own contribution is considered plagiarism.
 
K

Ken

Don't get me wrong; you can freely use and modify the code provided on my
webpage but when you are going to repost it publicly, there are better and
more proper ways to do so. Not properly separating the reference and your
own contribution is considered plagiarism.
I certainly didn't mean to plagiarize (thus the reference to your
original work). Without your original work I wouldn't have figured
out the solution to my problem. I spent a few hours last night
searching and your website was the only thing even close. Telling
users to open the mail, click other actions, and then select view in
browser was a non-starter. Your code opened the email in IE but
apparently used Word Formatting because the buttons were still not
active.
Do you have a reference for how to properly quote original
code?? I'm searching the etiquette posts but haven't seen anything.
The slightly cleaned up code(hopefully, correctly annotated) is
below. I apologize.


Sub OpenInBrowser()
'This procedure was written by Robert Spaarnaaij
'and may be found at http://www.howto-outlook.com/openinbrowser.htm
'Several lines have been added to save the html from the email.
'The original used file saveas

Dim BrowserLocation As String
'Dim Response As Integer

'Set the location of the executable of the browser you want to use
BrowserLocation = "C:\Program Files\Internet Explorer
\iexplore.exe"

'Get the user's TempFolder to store the item in
Dim FSO As Object, TmpFolder As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set FileName = 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

Dim MySelectedItem As MailItem 'added declaration to make MySelected
Item a MailItem object

'Retrieve the selected item
Set MySelectedItem = MyOlselection.item(1)


Dim sHTML As String 'Added string variable to hold the HTMLBody
'apparently the original code saved the file in Word Format

sHTML = MySelectedItem.HTMLBody 'Added to place the HTML from the
email into the temp file


'save the selected item as htm to the temp folder
strname = "www_howto-outlook_com"
FileName = FileName & "\" & strname & ".htm"
'The following line was in the original code - it was replaced
with code for saving
'the HTML
MySelectedItem.SaveAs FileName, olHTML

Dim SaveFile As Object 'added for use in saving the HTML file

'Added next 7 lines to write and save the HTML file
If FSO.fileexists(FileName) Then
Set SaveFile = FSO.OpenTextFile(FileName, 2, False)
Else
Set SaveFile = FSO.OpenTextFile(FileName, 2, True)
End If

SaveFile.Write sHTML
SaveFile.Close

'open the saved item in the browser
Shell BrowserLocation & " " & FileName, vbNormalFocus

'Cleanup
Set FSO = Nothing
Set FileName = Nothing
Set MyOlNamespace = Nothing
Set MyOlselection = Nothing
Set MySelectedItem = Nothing
Set SaveFile = Nothing 'Added

End Sub
 
R

Roady [MVP]

Hi Ken,

No, it's good this way, no problem. I saw your reference intention so I
kinda figured already you didn't do it on purpose. I'm in a research
environment where not properly referencing is enough to lose your titles so
I can be overly jumpy on that sometimes ;-)

Could you send me an example of what exactly you are trying to fix? It's
kind of vague to me what is not working for you in plain vanilla Outlook
2007 and what you are trying to solve with my macro and to what extend it
requires modification. Maybe we can work together on some code modifications
that can benefit the masses. You'll find a contact link on my website.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top