Link to home
Start Free TrialLog in
Avatar of emozley
emozley

asked on

Outlook VBA Convert PlainText to HTML

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
Avatar of [ fanpages ]
[ fanpages ]

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.
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!
I shouldn't have clicked submit so quick.  You can convert a plain text message to HTML, but it won't have any formatting.
Avatar of emozley

ASKER

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
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
Avatar of emozley

ASKER

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?


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
Hi,

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

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

BFN,

fp.
SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of emozley

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of emozley

ASKER

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.
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)?
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.
Avatar of emozley

ASKER

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.
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...
[ https://www.experts-exchange.com/Networking/Email_Groupware/Exchange_Server/ ]

BFN.

fp.