Platform SDK: CDO for Windows 2000

FileName Property

The FileName property returns the name of the file on the file system that contains the message.

[Visual Basic]
Property FileName(var as Variant) as String
read-only
[C++]
HRESULT get_FileName(VARIANT var, BSTR* Filename)
[IDL]
HRESULT [propget] FileName([in] VARIANT var, [out,retval] BSTR* Filename);

Parameters

var
The ordinal index or IMessage interface of the object for which you are requesting the file name.
Filename
On return, the full path to the file from which the specified Message object was created. The Filename parameter is the logical result when you are getting the property in languages such as Microsoft® Visual Basic®.

Remarks

The FileName property is commonly used to save modified message contents back to the file system. When you retrieve a Messages collection, each Message object in the collection is created using the serialized message content stored in a file with the .eml extension. However, you cannot use the IDataSource.Save method on the Message object save changes back to the file system. To save changes back to a file, use a Microsoft® ActiveX® Data Objects (ADO) Stream object or a Scripting.FileSystemObject object. To save the changes back into the original file, use the FileName property to first get the original file name and then use the IMessage.GetStream method to return the serialized contents of the Message object within an ADO Stream object. Finally, use the _Stream.SaveToFile method to save the serialized message stream into the file.

Example

This example code appends some text to the bottom of each text/plain body part of each message in the Simple Mail Transfer Protocol (SMTP) drop directory and then saves the modified messages back into the file from which the Message object was first loaded.

[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Dim iDropDir as New CDO.DropDirectory
Dim iMsgs as CDO.IMessages
Dim iMsg as CDO.Message
Dim iStream as ADODB.Stream
Dim fileName as String
Dim TxtBody as String

Set iMsgs = iDropDir .GetMessages
For Each iMsg in iMsgs
  TxtBody = ""
  fileName = ""
  fileName = iMsgs.FileName(iMsg)
  TxtBody = iMsg.TextBody
  TxtBody = TxtBody & vbCrLf & vbCrLf & "Here is some appended text"
  iMsg.TextBody = TxtBody
  Set iStream = iMsg.GetStream
  iStream.SaveToFile fileName, adSaveCreateOverWrite
Next iMsg
[C++,IDL]
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace raw_interfaces_only
#import <cdosys.dll> no_namespace raw_interfaces_only
#include <iostream.h>

void main(int argc, char* argv[])
{
  CoInitialize(NULL);

  IDropDirectory* pDropDir = NULL;
  IMessages* pMsgs = NULL;
  IUnknown* pUnk = NULL;
  IEnumVARIANT* pEnum = NULL;

  CoCreateInstance(
       __uuidof(DropDirectory),
       NULL,
       CLSCTX_SERVER,
       __uuidof(IDropDirectory),
      (void**)&pDropDir));

  pDropDir->GetMessages(L"",&pMsgs);
  long count = 0;
  pMsgs->get_Count(&count);
  cout << count << endl;

  pMsgs->get__NewEnum(&pUnk);
  pUnk->QueryInterface(__uuidof(IEnumVARIANT),(void**)&pEnum));

  ULONG cFetched = 0;
  VARIANT var;
  VariantInit(&var);
  while (1) {
    cFetched = 0;
      pEnum->Next(1,&var,&cFetched);
      cout << "fetched: " << cFetched << endl;
      if(cFetched == 0)
        break;
      IMessage* pMsg = NULL;
      var.pdispVal->QueryInterface(__uuidof(IMessage),(void**)&pMsg);
      var.pdispVal->Release();
      VariantClear(&var);

      // get file name
      BSTR szFilename;
      VARIANT varIntf;
      VariantInit(&varIntf);
      V_VT(&varIntf) = VT_DISPATCH;
      varIntf.pdispVal = pMsg;
      varIntf.pdispVal->AddRef();
      pMsgs->get_Filename(varIntf,&szFilename);

      // get text body of message
      BSTR szTxtBody;
      pMsg->get_TextBody(&szTxtBody);
      _bstr_t tempBody(szTxtBody);
      tempBody += "\r\n\r\nThis is some appended text";
      pMsg->put_TextBody(BSTR(tempBody);
      SysFreeString(szTxtBody);

      _Stream* pStrm = NULL;
      pMsg->GetStream(&pStrm);
      // write changes back to file
      pStrm->SaveToFile(szFilename,adSaveCreateOverWrite);
      pStrm->Release();
      SysFreeString(szFilename);
      pMsg->Release();
    }
  CoUninitialize();
}