Solved

Outlook automation with Logon

Posted on 2016-10-26
9
106 Views
Last Modified: 2016-11-21
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
0
Comment
Question by:formi
[X]
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
9 Comments
 
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.
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 41860329
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...
0
 

Author Comment

by:formi
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.
0
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.
0
 
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.
0
 

Author Comment

by:formi
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.
0
 
LVL 96

Expert Comment

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

Accepted Solution

by:
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!
0
 

Author Closing Comment

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

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 (http://www.codetwo.com/sync-for-icloud?sts=6554) 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