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
  • 4
  • 4
LVL 92

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 26

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.
LVL 92

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.
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 92

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 92

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

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.

Question has a verified solution.

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

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

861 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now