Solved

Outlook VBA Convert PlainText to HTML

Posted on 2006-06-16
17
5,821 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 ]
ID: 16918944
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
ID: 16920872
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
ID: 16920879
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
ID: 16921424
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
ID: 16921493
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
ID: 16921877
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
ID: 16922339
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 ]
ID: 16924697
Hi,

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

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

BFN,

fp.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 250 total points
ID: 16924760
> 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 ]
ID: 16925999
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
ID: 16933649
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
ID: 16938708
:)

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
ID: 16940477
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
ID: 16940899
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 ]
ID: 16940910
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
ID: 16941970
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 ]
ID: 16942013
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA: Add rows to listbox based on criteria 7 73
VBA to copy paste columns form one file to other 20 85
using web browser with BING 40 110
VB6 - Convert HH:MM into Decimal 8 51
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

21 Experts available now in Live!

Get 1:1 Help Now