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 95

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.
Technology Partners: 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!

LVL 95

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 95

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 95

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
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…
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…

710 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