FileName Property (IMessages)

Topic Last Modified: 2009-07-23

Returns the name of the file containing the message on the file system. This property is read-only.

Applies To

IMessages Interface

Type Library

Microsoft CDO for Exchange 2000 Library

DLL Implemented In

CDOEX.DLL

Syntax

Property FileName(var As Variant) As String

HRESULT get_FileName(VARIANT var, BSTR* Filename);

Parameters

  • var
    The message you want to determine the file name of.
  • Filename
    The name of the file containing the message on the file system.

Remarks

You can identify the message for which you want the file name using the first argument to the property. You can either specify the ordinal index of the Message object or pass the IMessage object reference itself.

One common use of the FileName property is when you want to modify the contents of the messages stored in a particular directory and save the changes back into the file from which each Message object was derived. When you retrieve a Messages collection, each Message object in the collection is created using the serialized message stream stored in a file with the .eml extension. For these messages, unlike messages stored in the Microsoft® Exchange store, you cannot use the IDataSource interface on the Message object to bind to the file, call IDataSource.Save, and thereby save the modified message contents back to the file. In such cases, you must use a Microsoft ActiveX® Data Objects (ADO) Stream object or some other means, such as a Scripting.FileSystemObject, to save the new message contents back to the file system. You can use the FileName property to get the file name, and then overwrite the file with the new contents of the message. You can use the IMessage.GetStream method to return the contents of the Message object in serialized format within an ADO Stream object. The _Stream interface defines the _Stream.SaveToFile method that can be used to save the stream to disk. The following example demonstrates how to do this.

Examples

This example appends text to the end of each text/plain body part of each message in the SMTP drop directory, and then saves the modified message back into the file from which the Message object was derived.


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



#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();
}