Send OL Mail Item Plain Text Only-VBA in OL2K

J

John Gregory

Is there an OL2K VB property or method to create and send a mail ite
by "plain text"? (Not Rich Text or HTML) The code below sends Ric
Text even though I have manually set the option in OL2K for Plai
Text only in Tools,Options, Mail Format. I want all mail to go ou
as plain text

Sub Create_OL_Mail_PlainTextOnly(
Dim olApp As Outlook.Applicatio
Dim olMailItm As Outlook.MailIte
Set olApp = New Outlook.Applicatio
' Create new OL mail item
Set olMailItm = olApp.CreateItem(olMailItem
With olMailIt
.To = "(e-mail address removed);[email protected]
.Subject = "Msg created in VBA
.Body = "Msg created by VBA.
.Display 'Display or Sen
.Sav
.Close Fals
End Wit
'olApp.Qui
Set olApp = Nothin
Set olMailItm = Nothin
End Su

Thank
John Gregor
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

No, the only way to accomplish that with Outlook 2000 is to use CDO 1.21 to create and send the message, not Outlook objects. Setting the Body property in Outlook 2000 always generates an RTF message.
 
J

John Gregory

I found the answer in a Microsoft Knowledge Base (MKB
http://support.microsoft.com/?kbid=222248

The short answer is in OL2K, you can not send a mail item with VBA a
Plain Text. When using VBA in OL2K to send a mail item, the only tw
options are (1) HTML and (2) Rich Text. The MKB Collaboration Dat
Objects CDO work around is a bit much for me for now

But I will look further to see if I can use VBA code to access th
menus on the new mail after it is displayed and saved in the "Drafts
folder item for Format and "Plain Text". Can someone help me d
that

This Plain Text problem is fixed in OL2002 and OL2003.

(Start Quote
SYMPTOMS: You use the Outlook object model and set the Body o
HTMLBody property of a MailItem, and the mail format of the messag
changes to Rich Text or HTML.

RESOLUTION: This behavior is by design and not documented correctly i
the Outlook Visual Basic Reference Help file (Vbaoutl9.chm)

If you want to populate the body of the message and still send th
message in Plain Text format, you can use the Collaboration Dat
Objects (CDO) object model. The CDO object model sends Plain Tex
messages by default

[Skip... ] Both Outlook 2002 and Microsoft Office Outlook 2003 includ
a new property named BodyFormat. In later versions of Outlook [Afte
OL2000], you can use the BodyFormat property to programmaticall
switch between mail formats [Including Plain Text].

(End Quote
Joh
(e-mail address removed)
 
M

Michael Bauer

Hi John,

why do you spend so much time on workarounds while the solution is well
known already? Using CDO is very simple. Please try this sample. From
within OL, i.e. if OL is running, you don´t need to specify the profile.

Public Sub Test()
With CreateMessage
.Recipients.Add "e-mail-address"
.Recipients.Resolve
.Subject = "test text/plain"
.Send
End With
End Sub

Private Function CreateMessage() As MAPI.Message
On Error Resume Next
Dim oMapiSess As MAPI.Session
Dim oFld As MAPI.Folder
Dim colMsg As MAPI.Messages

Set oMapiSess = LogOn
Set oFld = oMapiSess.Outbox
Set colMsg = oFld.Messages
Set CreateMessage = colMsg.Add
End Function

Private Function LogOn(Optional sProfile As String) As MAPI.Session
On Error Resume Next
Dim oSess As MAPI.Session
Set oSess = CreateObject("MAPI.Session")
Select Case Len(sProfile)
Case 0
oSess.LogOn , , False, False, , True
Case Else
oSess.LogOn sProfile, , False, True, , True
End Select
Set LogOn = oSess
End Function
 
J

John Gregory

Copy my previous macro into an XL module and run it. Then you wil
see it does work. That macro ends up with an OL2K mail item in plai
text, not RTF and not HTML. Step one displays the OL mail item i
RTF. Step two executes that same mail item's menus (control ID 5536
for Format and Plain text thereby converting RTF to Plain text.
have tested it many times, and it has not failed yet. Granted, m
test may be limited but it does work in my tests in my projects

The only minor inconvenience is the user has to click the OK butto
when an alert pops up just before converting from RTF to Plain Text
My question was for a solution to work around the alert. I will b
surprised if a solution to the alert exists, but am asking, just t
be sure

I just now saw Michael's CDO code and will try it in OL as suggested
And, for my use, I will later have to run the macro from an XL modul
and I assume that will work OK also. XL contains my list of addresses
my subjects and the body. I have never tried CDO before and until
just saw Michael's post, I did not know where to acquire it
Apparently, I already have it. I now have hope that I will be able t
use CDO to use VBA to send OL mail items in Plain Text, without th
work arounds

I hope I do not appear ungrateful. I really am very grateful and than
both you and Sue for sharing your expertise with me

John D. Gregor
(e-mail address removed)
 
J

John Gregory

Michael

I installed Microsoft CDO 1.21 Library from the Office 200
installation disk and the code Michael generously shared with m
works fine, just like it should. My problems are illustrated below i
the lines of code (LOC) commented out with the leading apostrophe an
also in the following paragraphs about alert boxes and their O
buttons

With CreateMessag
'.To = "(e-mail address removed)" 'Method not supported 438
.Recipients.Add "(e-mail address removed)
'My emails get kicked back if I do not use resolve
'Err Msg: No transport provider was availabl
.Recipients.Resolve 'Alert & OK Button
.Subject = "test text/plain Serial Num 0009
'.Display 'Method not supported 438
.Send 'Alert & Ok Button, 5 Sec. Wait
End Wit

My first and the major problem with this CDO code is the Displa
method is not supported. Only the Send method is supported. I need t
use the "Display" method so I can review, edit, and approve the emai
item before I manually click the Send menu on the displayed emai
item. I run this code from XL2K

A second problem with this CDO code is the two alert boxes and tw
"OK" buttons. My only reason for trying CDO was to avoid the on
alert box

A second problem with this CDO code is it does not support the ".To
method, which means I have to use the ".Recipients.Resolve" metho
which causes another alert box and OK button.

The two alert boxes with OK buttons are
(A) The method "Recipients.Resolve" causes an alert box with an O
Button
(B) The method ".Send" causes an alert message & and a 5 secon
wait before you can click the OK button

My previous code, written without using CDO, "Displayed" an OL emai
item in Plain Text, and required only one alert box with an OK butto
to click without having to wait five seconds. So what am I missing
Surely there are some facts which are not in my limited persona
knowledge base of XL, OL and CDO, which would result in my drawing
different conclusion if I knew such facts.

Last but most important, thank you for sharing your knowledge of XL
OL and CDO, which will help me for years to come. This code you gav
me saved me literally hours of learning time compared to my having t
find it without your help. I am progressing through a learnin
curve

Joh
 
M

Michael Bauer

Ok, the easiest would be to use Redemption (www.dimastr.com).

Without Redemption (or programming against ExtendedMapi with C++ or
Delphi) there´s only one workaround for you as long as you want to
display the message and click "Send" manually, because calling the Send
method via code is blocked.

Workaround:

- Create your message with the OOM as you did before, except the Body
property.

- Save the message and dereference the object variable.

- Open the message with CDO by its EntryID (Session.GetMessage)

- Fill in the Body and save again.

- Open the message again with the OOM (GetItemFromID)

- Display the message.
 

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