Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Outlook VBA Convert PlainText to HTML

Posted on 2006-06-16
17
Medium Priority
?
6,481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 750 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 750 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

598 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