Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Outlook automation with Logon

Posted on 2016-10-26
Medium Priority
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 97

Expert Comment

by:John Hurst
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.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

LVL 97

Expert Comment

by:John Hurst
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 97

Expert Comment

by:John Hurst
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 97

Expert Comment

by:John Hurst
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Read this checklist to learn more about the 15 things you should never include in an email signature.
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
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.

722 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