PRB: ReadFromFile() Fails for Attachment of Type CdoOle

ID: Q196080

The information in this article applies to:

SYMPTOMS

If a bitmap attachment has the type CdoOLE, the attachment can be written to a file on disk with the WriteToFile method without an error occurring. However, when you try to use the ReadFromFile method of an attachment object to read a bitmap file from the disk into a message where CdoOLE is the attachment type, you will get one of the following error messages, depending on the version of CDO:

   Run-time error '-2147221233 (8004010f)'
   [Collaboration Data Objects - [MAPI_E_NOT_FOUND (8004010F)]]

- or -

   Run-time error '-2147467259(80004005)'
   [Collaboration Data Objects - [E_FAIL(8000405)]]

The code provided in the "More Information" section below illustrates that the error will occur even if you have previously written the file from an Attachment object with the WriteToFile method.

CAUSE

Bitmap files are not in OLE docfile format. If the Type property of the Attachment object is CdoOLE, the file specified in the ReadFromFile method must be in OLE docfile format. The term "OLE docfile" indicates that the file is written by an application such as Microsoft Word version 6.0 or later that writes files using the OLE IStorage and IStream interfaces.

RESOLUTION

Specify CdoFileData (1) for the Type property of the Attachment or use Microsoft Outlook to insert the bitmap.

STATUS

This behavior is by design. CDO documenation states that the CdoOle file must be an OLE Doc file, not just a OLE file.

MORE INFORMATION

Steps to Reproduce Behavior

1. Open the Microsoft Paint accessory and draw something. Save it as

   "Test.bmp" in a folder, for example "C:\Temp".

2. In a new message in Outlook, select Object from the Insert menu. Select
   "Create from file" to insert your picture "Test.bmp" into the 
   newly-created message.

3. Send the newly-created message to yourself (or someone else).

4. Make sure the message received from the above steps is the first message

   in your Inbox. (This is because the code below will only test the 
   first message in the Inbox.)

5. Create a Visual Basic project and put a button on the form.

6. Add Microsoft CDO to the Project References.

7. Paste the code below into the project.

     Option Explicit

     Dim objSession As MAPI.Session
     Dim myMsg As MAPI.Message
     Dim newMsg As MAPI.Message
     Dim myAttach As MAPI.Attachment
     Dim newAttach As MAPI.Attachment

     Private Sub Command1_Click()

       Set objSession = CreateObject("MAPI.Session")
       objSession.Logon
       MsgBox "CDO version = " & objSession.Version

       Set myMsg = objSession.Inbox.Messages.GetFirst
       'MsgBox "myMsg.Subject is: " & myMsg.Subject

       Set myAttach = myMsg.Attachments.Item(1)
       'MsgBox "Attach Name is : " & myAttach.Name
       'MsgBox "Attach Type is : " & myAttach.Type
       'MsgBox "Attach Class is : " & myAttach.Class
       'MsgBox "Attach Method is : " & myAttach.Fields(CdoPR_ATTACH_METHOD)

       myAttach.WriteToFile "C:\temp\attach.bmp"

       Set newMsg = objSession.Outbox.Messages.Add
       newMsg.Subject = "Re: " & myMsg.Subject
       newMsg.Text = "Re: " & myMsg.Text

       Set newAttach = newMsg.Attachments.Add
       newAttach.Name = "attach.bmp"
       newAttach.Type = myAttach.Type
       newAttach.Position = myAttach.Position

       newAttach.ReadFromFile "C:\temp\attach.bmp"

       newMsg.Update
       objSession.Logoff
     End Sub

8. Run the code.

9. The following line will generate one of the messages described in the

   Symptoms section above.

       newAttach.ReadFromFile "C:\temp\attach.bmp"

To prevent the error message, you may change the following line of code:

    newAttach.Type = myAttach.Type

to:

    newAttach.Type = CdoFileData

Additional query words: kbDSupport kbCDO110 kbCDO120 kbCDO121 kbMsg
Keywords          : kbCDO110 kbCDO120 kbCDO121 kbMsg kbVBp kbGrpMsg 
Version           : WINDOWS:1.1,1.2,1.21
Issue type        : kbprb

Last Reviewed: April 15, 1999