Link to home
Start Free TrialLog in
Avatar of formi
formiFlag for Switzerland

asked on

Outlook automation with Logon

Hi,
Delphi XE7, Outlook 2013 with 2 profiles

I have the following code:
  OutlookApp := CreateOleObject('Outlook.Application');
  Mapi := OutlookApp.GetNameSpace('MAPI');
  if MapiLogOn(0,PAnsiChar(AnsiString(Profile)),'',MAPI_NEW_SESSION,0,@hSession) = 0 then
  begin
    ....
  end;

Open in new window

I have 2 problems:
1. if outlook is already executed I get an error (by CreateOleObject) "starting the server failed"
2. if outlook is not started outlook starts and displays a message to choose the profile (by CreateOleObject('Outlook.Application') and all works fine, but I won't log on and use outlook invisible and automatic. How can I do that?
Thanks, Peter
Avatar of John
John
Flag of Canada image

I do not know Delphi, but:

1. Outlook already started:   You can only have one active profile.  So that explains point 1.

2. Outlook not started:  You have to define the profile you  want in the command line to start Outlook.

I do this (point 2) to run two different Outlook sessions on the same Desktop session. But only one can run at one time.
Avatar of Sinisa Vuk
1.
You should connect to existing one if exists:
  try
    OutlookApp := GetActiveOleObject('Outlook.Application');
  except
    OutlookApp := CreateOleObject('Outlook.Application');
  end;
...

Open in new window


Great examples was on (non existing anymore) page:
http://www.djpate.freeserve.co.uk/AutoOutl.htm
...but found archive of page here...

2.
Check my answer to an similar question...
Avatar of formi

ASKER

@John: I need help in Delphi-programming
@Sinisa: I have read that that's not valid for office-programs. There, if you call CreateOleObject and the program is already started it connects to the existing one. GetActiveOleObjects always throws an exception and it want be assigned.
Yes. I do not know Delphi. But, you need to use the Outlook command structure to open Outlook

Outlook /Profile <profilename>  

And then you can only open one at a time.
Also, and so we are clear, your script cannot run if Outlook is Open. Outlook must be closed for the script to run. Why? Outlook cannot open unless it opens an account (outlook profile) and you cannot run another Outlook profile while one is running.

Most of what you are up against here is Outlook and not Delphi.
Avatar of formi

ASKER

John, have a look at what I wrote @Sinisa. If you create an new OleObject and Outllook already runs it doesn't create a second one but it connects to it.
Outlook will run but it will use the profile already loaded - just a second occurrence.
ASKER CERTIFIED SOLUTION
Avatar of formi
formi
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formi

ASKER

Found myself that it was a problem of Delphi edition. Second question is not answered.