Microsoft KB Archive/231574

From BetaArchive Wiki
Knowledge Base


How to send rich-text formatted e-mail from Visual Fox Pro with CDO (1.x)

Article ID: 231574

Article Last Modified on 7/13/2004



APPLIES TO

  • Microsoft Collaboration Data Objects 1.1
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Collaboration Data Objects 1.21



This article was previously published under Q231574

SUMMARY

This article describes how to use Microsoft's Collaboration Data Objects (1.1, 1.2, 1.21) library from Visual FoxPro to send an e-mail message with a rich text formatted (RTF) message body.

MORE INFORMATION

The following Visual FoxPro code sample sends an e-mail message with an RTF formatted message body.

Notes

This code sample assumes that CDO (1.1, 1.2, or 1.21) has been installed on the target system. If you're not certain that this library is already installed please see the following article in the Microsoft Knowledge Base:

171440 Where to Acquire the CDO (1.x) Libraries


This article also makes use of a function (writertf) provided in a DLL (Mapirtf.dll) delivered in a second article:

172038 DLL to Read and Write RTF with Active Messaging


NOTE: CDO (1.1) was formerly named Active Messaging.

The code below will not function until both the CDO (1.1, 1.2, 1.21) library and the Mapirtf.dll file are installed as noted in their respective readme files.

* Send RTF E-Mail from VFP
* -----------------------
* Put Mapirtf.dll in the current directory, or Windows\System directory
* (for Windows 95) or the Winnt\System32 directory (for Windows NT).

Declare INTEGER writertf IN mapirtf.dll;
       STRING ProfileName, STRING MessageID,;
       STRING StoreID,     STRING cText

Declare INTEGER readrtf IN mapirtf.dll;
       STRING ProfileName, STRING SrcMsgID,;
       STRING SrcStoreID,  STRING MsgRTF
       
objSession = CREATEOBJECT("mapi.session")
objSession.Logon

*Create a new message in the Outbox
objMessage = objSession.Outbox.Messages.Add

*Set the Subject and Add a Recipient
objMessage.Subject = "This is the message subject"
objRecip = objMessage.Recipients.Add
objRecip.name = "My_Recip_Here"  && <<--Your Recipients email name here
objRecip.Resolve

*Write the message to the store in order to acquire then
*retrieve a MessageID
objMessage.Update
MsgID = objMessage.ID

*Write the RTF, passing the saved MessageID, StoreID, and the
*RTF Stream to the function.
*
*Note - The RichEdit Control that ships with VF does not expose the
*       RTF Stream - just plain text. You will need to either use an
*       alternate RTF Control that exposes the RTF Stream, or read
*       the RTF from a file, etc. The following line of code loads
*       an RTF Stream from file into the vRTFStream variable.
*
*For testing purposes, try reading the contents of a file saved
*in an RTF format from MS-Word into the variable, alternately you
*will need to find an RTF control that exposes the RTF stream as
*a property of the control:
vRTFStream = FILETOSTR(GETFILE("RTF",;
                               "",;
                               "Read this File",;
                               0,;
                               "Select an RTF Source File"))

*The writertf() function is in the MAPIRTF.DLL from Q172038
intRetVal = writertf(objSession.Name,;
                     objMessage.ID,;
                     objMessage.StoreID,;
                     vRTFStream)
            
IF intRetVal <> 0 THEN
       MESSAGEBOX("Write RTF to Message >> Failed <<")
ELSE
       MESSAGEBOX("Write RTF to Message >> Succeeded <<")
ENDIF

*Because the object has changed, we must re-acquire our Message...
*First clear our current variable
RELEASE objMessage

*Next set a filter on the outbox for our Message ID
objMessageColl = objSession.Outbox.Messages
objMessageFilter = objMessageColl.Filter
objMessageFilter.Fields.Add(0x0FFF0102, MsgID)
objMessage = objMessageColl.GetFirst

*Send it
objMessage.Send

*Clean up then exit
objSession.Logoff
RELEASE objRecip, objMessage, objSession
*EOP: VF_CDO_RTF.PRG
                

REFERENCES

For additional information on how to effectively use CDO (1.x), see the Microsoft Knowledge Base articles and the Microsoft Developer Network (MSDN). Both are great resources, containing a substantial number of additional articles on how to make use of CDO. To find these articles, query on "CDO" and "1."

Keywords: kbhowto kbmsg KB231574