Outlook automation with Logon

Posted on 2016-10-26
Last Modified: 2016-11-21
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

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
Question by:formi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 96

Expert Comment

by:Experienced Member
ID: 41860269
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.
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 41860329
You should connect to existing one if exists:
    OutlookApp := GetActiveOleObject('Outlook.Application');
    OutlookApp := CreateOleObject('Outlook.Application');

Open in new window

Great examples was on (non existing anymore) page:
...but found archive of page here...

Check my answer to an similar question...

Author Comment

ID: 41864100
@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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 96

Expert Comment

by:Experienced Member
ID: 41864113
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.
LVL 96

Expert Comment

by:Experienced Member
ID: 41864199
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.

Author Comment

ID: 41864376
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.
LVL 96

Expert Comment

by:Experienced Member
ID: 41864394
Outlook will run but it will use the profile already loaded - just a second occurrence.

Accepted Solution

formi earned 0 total points
ID: 41871898
As I don't get an answer I try to explain what I want to do:

I want to synchronise the contact-folder with the entries of a database. I want do that independent if outlook is started or not (when it already runs I can use this instance, when not I create a new one).

problems as described in my first post:
1. outlook already started: GetActiveOleObject returns an exception and does not connect to the existing instance of outlook
2. if not is started: with CreateOleObject it displays the window for choosing the profile but I want to log in by programcode (the user should not have to take some actions).

How can I solve these problems. Thanks.

Edit: First Problem solved!! Compiling with Delphi XE7 --> exception occurs, with Delphi 10.1 Berlin: works as expected!

Author Closing Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Read this checklist to learn more about the 15 things you should never include in an email signature.
CodeTwo Sync for iCloud ( automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question