More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 22-Jan-2019 18:09

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: advanced    Code area: Outlook Expert Techniques Printer Friendly Version
Title: Add text to plain text message
Description: In versions of Outlook prior to 2002, it's not possible to use Outlook objects to create a plain text message or to add to a plain text message without changing the format. This Outlook VBA sample shows the basics of using CDO to work with an existing message, presumably in the Drafts folder. It requires a reference in your project to the Microsoft CDO 1.21 library.
Date: 21-Jul-2003  11:13
Code level: advanced
Code area: Outlook Expert Techniques
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub TestAppendText()
    Dim objItem As Object
    Dim thisMail As Outlook.MailItem
    'On Error Resume Next
    
    Set objItem = Application.ActiveExplorer.Selection(1)
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            Set thisMail = objItem
            Call AppendTextToMessage(thisMail, "Some text added at " & Now())
        End If
    End If
    
    Set objItem = Nothing
    Set thisMail = Nothing
End Sub

Sub AppendTextToMessage(ByVal objMail As Outlook.MailItem, ByVal strText As String)
    Dim objCDO As MAPI.Session
    Dim objMsg As MAPI.Message
    Dim objField As MAPI.Field
  
    Set objCDO = CreateObject("MAPI.Session")
    objCDO.Logon "", "", False, False
    
    If Not objMail.EntryID = "" Then
        Set objMsg = objCDO.GetMessage(objMail.EntryID, _
                                   objMail.Parent.StoreID)
        objMsg.Text = objMsg.Text & vbCrLf & strText
        objMsg.Update True, True
        Set objField = objMsg.Fields(CdoPR_RTF_COMPRESSED)
        If Not objField Is Nothing Then
            objField.Delete
            objMsg.Update True, True
        End If
        Set objField = Nothing
        Set objField = objMsg.Fields(CdoPR_RTF_SYNC_BODY_COUNT)
        If Not objField Is Nothing Then
            objField.Delete
            objMsg.Update True, True
        End If
     Else
        strMsg = "You must save the item before you add text. " & _
                 "Do you want to save the item now?"
        intAns = MsgBox(strMsg, vbYesNo + vbDefaultButton1, "Append Text to Message")
        If intAns = vbYes Then
            Call AppendTextToMessage(objMail, strText)
        Else
            Exit Sub
        End If
    End If
    
    Set objMsg = Nothing
    objCDO.Logoff
    Set objCDO = Nothing
End Sub
All 12comments
Page [ 1 2 Next >>  
  21-Jul-2003  18:20   


Outlook does some caching, so the appended text might not be immediately apparent when you open the item to send it. A possible enhancement, therefore, would be to send the message with CDO (or better yet, use Redemption instead of CDO to avoid security prompts).



Because of the caching, changing the text in an item that's open in an Inspector window may not work at all. A possible workaround in that case might be to create a copy of the message, save it, pass it to CDO, modify it, then delete the original.



Note the need to delete a couple of MAPI properties to get the text change to show -- a little MAPI voodoo that Outlook Spy (http://www.dimastr.com) is essential for experimenting with.



I'm saving the item (.Update) after each change for good measure, but that might be overkill.
  25-May-2006  15:25   
I tried using this code but received an error on the line "Set objField = objMsg.Fields(CdoPR_RTF_SYNC_BODY_COUNT)" of "mapi_e_not_found(800401f)". I have managed to establish that this has something to do with the code not being able to find the message or that it is missing. The code picks up the subject of the selected message in the "objMail" variable. Any thoughts?

Taking what this code should do a little further what I want to try and achieve is the ability to add a hyperlink (to a file or dir) into an email. I have figured out how to select the path for the file or dir but am now looking into how I can name the link and insert it in the email with the path of the file or dir. If this is not possible than I'd appreciate knowing that. Thanks.
  25-May-2006  16:07   
Chris, try uncommenting the On Error Resume Next statement.

What version of Outlook are you using?
  25-May-2006  17:13   
Sue, OK, that avoids that error but I cant see the text "Some text added at " being added, although after your suggestion I looked at the test email I was using and the string was in it! Not sure when that was placed there but I cant replicate it. Just so I am using it right. I have saved an email in drafts which has text in the subject and body. I select it (single click) and run the macro "TestAppendText" and it runs through and "objMail" picks up the subject value but noting changes in the email? Am using Outlook 2003 with SP3.
 
  25-May-2006  21:33   
Did you create the message in Drafts in plain text format?

If you are using Outlook 2003, why are you using this approach, which is intended as a workaround for Outlook 2000 and earlier?
  28-May-2006  23:20   
Yes, I have just tried that and no go. Perhaps I am barking up the wrong tree using this code going by what I am trying to achieve (Post 25May). Thanks for the feedback and I'll continue playing...
  31-May-2006  13:48   
Chris, still looking for an answer to my last question.
  19-Aug-2008  02:25   
do I have a chance to cpoy and paste the test in the message via outlook macro
  19-Aug-2008  09:15   
ali, if Word is the email editor, you can return a Word.Document object from Inspector.WordEditor and then use Word's methods for copy and pate. Otherwise, you would be limited to executing the controls on the toolbar or menu; see http://wwww.outlookcode.com/d/tips/commandbarfun.htm
  21-Aug-2008  04:41   
no I just wanna to use outlook and would like establish a macro which is works very well by excel. this macro is used for copiing ans pasting of marked text areas. it means I need a macro, which allows to me to copy a marked area for using the copied text for another application.
Page [ 1 2 Next >>