G Martin
G Martin

Reputation: 11

How do I obtain a list of PST Files in stores from Outlook?

I'm reading in emails from PST file and would like the user to be able to specify the file to be read from within the Delphi App and set it as the default.

Setting a PST file into the Sessions store seems to work OK. However, I have not been able to list the other PST files in the store for the user to select one from within the application.

The code was created by looking a C# example on the internet and adapting it to Delphi. It does not work enumerating through the list of stored items the way I assume to do it from the C# code, giving an invalid argument error when enumerating with an integer index for the store. I haven't found what to do to fix that. Any help available will be appreciated.

procedure TFEmailImportControl.PSTLocate(pstPath : String  = '');
  var
  Outlook, oNameSpace, pstStores, pstNameSpace, CurrStore : OleVariant;
  pstSession, Stores, aStore : OleVariant;
  

  i  : Integer;

  pstLocationsFile, tempStr : String;
  strLPST : TStringList;

begin
  pstLocationsFile := 'PSTLocations.txt';
//  pstPath := 'E:\MyPSTFile.pst'; //  eg where a new pst file is to go.

  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;

  oNameSpace := Outlook.GetNamespace('MAPI');
  oNameSpace.Logon('', '', False, False);   // not sure if this is necessary

  StrLPST := TStringList.Create;


  try
    strLPST.Add('-- PST Files -- ');

    pstSession := Outlook.Session;
    stores :=  pstSession.Stores;


    // add a new Store if specified
    if (Trim(pstPath) <> '' ) and (Pos('.pst', pstpath) > 0) then
      pstSession.AddStore( pstPath );   // (This works OK)

    for I := 1 to Stores.Count do
    begin
      aStore := Stores[i];    //breaks down here ***   comes up with invalid argument... How to fix it?


      tempStr := AStore.FilePath;           // temp variable for debug only
      strLPST.Add( tempStr );
    end;


    // Save the list to a text file
    ChDir(MainBaseFolder);             // Set Main folder from the application
    strLPST.SaveToFile(PSTLocationsFile);

 finally
   strLPST.Free;
 end;


end;

Upvotes: 1

Views: 148

Answers (0)

Related Questions