Solved

Outlook VBA Convert PlainText to HTML

Posted on 2006-06-16
17
5,753 Views
Last Modified: 2012-08-13
Hi,

I am having trouble adapting the following VBA code in Outlook 2002.  Basically I want to say if it's a plain text email then convert it to HTML.  The following works - eg non-HTML emails become HTML but if I change the last part to:

 If Item.BodyFormat = olFormatPlain Then
   Item.BodyFormat = olFormatHTML

it doesn't work:

Any ideas?  Thanks very much.

Ed


Dim WithEvents olkInboxItems As Outlook.Items

Private Sub Application_Quit()
    Set olkInboxItems = Nothing
End Sub

Private Sub Application_Startup()
    Set olkInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If Item.BodyFormat <> olFormatHTML Then
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub
0
Comment
Question by:emozley
  • 6
  • 6
  • 5
17 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
Hi,

Could you elaborate on "it doesn't work"?  Do you mean the format the the body stays the same or you see an error message?

Have you tried, simply...

Item.HTMLBody = Item.Body

?

BFN,

fp.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi emozley,

That looks like something I wrote.  You can convert HTML to plain text easily enough since an Outlook message contains both an HTML and a plain text representation of an HTML message.  But a plain text message has no HTML representation.  

Cheers!
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
I shouldn't have clicked submit so quick.  You can convert a plain text message to HTML, but it won't have any formatting.
0
 

Author Comment

by:emozley
Comment Utility
Sorry I was a little vague there!  By it doesn't work I mean the format of the message doesn't change.  There is no error messag or anything.

I don't mind if there is no formatting - it just needs to be in HTML as our spam filter adds an HTML formatted disclaimer on to the end of the email.  That means if someone gets an email in plaintext and replies without changing the format to HTML the recipient will see all the HTML formatting in the reply which looks rather unprofessional.

My latest attempt is the following however if I send an email to myself in plaintext it is still plaintext when I open it.

Dim WithEvents olkInboxItems As Outlook.Items

Private Sub Application_Quit()
    Set olkInboxItems = Nothing
End Sub

Private Sub Application_Startup()
    Set olkInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If Item.BodyFormat = olFormatPlain Then
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
This works find for me.

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If Item.BodyFormat <> olFormatHTML Then
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub
0
 

Author Comment

by:emozley
Comment Utility
I know this works - if if it's not HTML then turn it into HTML

 If Item.BodyFormat <> olFormatHTML Then
            Item.BodyFormat = olFormatHTML

but I want to say 'if it's a plan text message then turn it into HTML'

 If Item.BodyFormat = olFormatPlain Then
            Item.BodyFormat = olFormatHTML

But nothing happens - the message is still in plaintext.

Does that work on yours?


0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Yes, that works for me.  Try this.  It will convert plain text or unspecified format messages to HTML.

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If (Item.BodyFormat = olFormatPlain) Or (Item.BodyFormat = olFormatUnspecified) Then
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub
0
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
Hi,

Can you convert from plain text to RTF, then from RTF to HTML?

(I know it's a pain...)

BFN,

fp.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 250 total points
Comment Utility
> Can you convert from plain text to RTF, then from RTF to HTML?
Yes.  The code below will do this.  Why the intermediate step though?  

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If (Item.BodyFormat = olFormatPlain) Or (Item.BodyFormat = olFormatUnspecified) Then
            Item.BodyFormat = olFormatRichText
            Item.Save
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub
0
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
I seem to recall that in certain circumstances when you have an e-mail in plain text, the HTML option is not shown in MS-Outlook; you have to manually convert to RTF first, so I figured that the reason the code was not doing anything is because the facility is not available in VBA either.

With the intermediate step, Ed, does it solve your problem?

BFN,

fp.
0
 

Author Comment

by:emozley
Comment Utility
Hi,

It's very strange because if I use this...

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If (Item.BodyFormat = olFormatPlain) Then
            Item.BodyFormat = olFormatRichText
            Item.Save
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub

...then nothing happens.  Plain emails stay plain, html emails stay as html and rich text emails stay as rich text.  However if I use:

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        If (Item.BodyFormat = olFormatPlain) Or (Item.BodyFormat = olFormatUnspecified) Then
            Item.BodyFormat = olFormatRichText
            Item.Save
            Item.BodyFormat = olFormatHTML
            Item.Save
        End If
    End If
End Sub

...then all emails get converted to HTML - eg both plain AND rich text emails get converted.  It's almost as if the format of all emails is showing up as FormatUnspecified.

Is it possible to make it so that when an email arrives I can make a dialogue box or message of some sort open up and display the value of Item.BodyFormat?

Thanks for all your help with this so far.

E.
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 250 total points
Comment Utility
:)

So my suggestion worked!... lucky guess :)

Rather than a MsgBox, why don't we log the results to a file so that it is not intrusive to your environment?

1.  Start MS-Outlook.
2.  Click "Tools" / "Macro" / "Visual Basic Editor" menu item, or use [ALT]+[F11] key combination.
3.  If not already expanded, expand the "Microsoft Office Outlook Objects" and highlight (select) "ThisOutlookSession".
4.  Copy the code below and paste it into the right-hand pane of the "Microsoft Visual Basic" Editor window:

' Start of code...

Option Explicit

Private WithEvents olkInboxItems                        As Outlook.Items
Private Sub Application_Startup()
 
  Set olkInboxItems = Session.GetDefaultFolder(olFolderInbox).Items

End Sub
Private Sub Application_Quit()

  Set olkInboxItems = Nothing
 
End Sub
Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)

  Dim objFile                                           As Object
  Dim objFSO                                            As Object
 
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.OpenTextFile("c:\Outlook_Inbox.txt", 8, True)
 
  objFile.Writeline Now & " " & vbTab & Item.Subject & vbTab & IIf(Item.Class = olMail Or Item.Class = olNote, Item.BodyFormat, "n/a")
 
  objFile.Close
 
  Set objFile = Nothing
  Set objFSO = Nothing
 
End Sub

' ...End of code

5.  Click the diskette icon on the toolbar to save the changes (or use [CTRL]+[S] key combination).
6.  Close the "Microsoft Visual Basic - VbaProject.OTM - [ThisOutlookSession (Code)]" window.
7.  Click "Tools" / "Macro" / "Security..." menu item.
8.  Set the Security Level to Medium.  
9.  Close MS-Outlook.
10. Re-start MS-Outlook.
11. When prompted to [Enable Macros] or [Disable Macros] on the "Security Warning" message box, click the [Enable Macros] button.


After each mail message is delivered to your "Inbox" the current date/time, Subject & BodyFormat (if applicable) will be logged to the file named "Outlook_Inbox.txt" on the root folder of your c: drive.

Use MS-Notepad, MS-Wordpad, MS-Word, or any other ASCII text editor to view the contents.

BFN,

fp.


0
 

Author Comment

by:emozley
Comment Utility
Strange - I sent myself a plain email, then an html email then a rich text.  This is the file that was produced:

20/06/2006 09:22:20       Test Plain Email      0
20/06/2006 09:22:34       Test HTML email      0
20/06/2006 09:22:48       Test Rich Text Email      0

It looks the BodyFormat is always showing as 0 regardless of the actual format.

I am using Outlook 2002 with Exchange 2003 if that helps.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
A body format of 0 is FormatUnspecified.  When you send to yourself are the messages staying within your Exchange environment, or are they going outside and coming back in?  Or are you sending from a different account (i.e. not from your Exchange account)?
0
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
Glad the code was of some use.

Yes, may be worth (opening & then) using a Yahoo! Mail or Hotmail (or similar) account & sending yourself a message to see the outcome in the "log" file.

BFN,

fp.
0
 

Author Comment

by:emozley
Comment Utility
I've just sent myself two emails from my hotmail account - the first one plaintext and the second one HTML:

20/06/2006 14:04:14       Test      0
20/06/2006 14:05:09       Test      0

This is very strange and i'm not sure if there is anything else that can be done.  I think I will have to make all emails HTML regardless of their format.  This is a bit of a pain as rich text does have some good features like being able to print the email without opening it.

Thanks very much - points being awarded now!

E.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
You're very welcome.

It may be worth creating a new question in the "Exchange Server" Topic Area, in case this is a setting you can control...
[ http://www.experts-exchange.com/Networking/Email_Groupware/Exchange_Server/ ]

BFN.

fp.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now