Avatar of Stef Merlijn
Stef Merlijn
Flag for Netherlands asked on

Can't connect to Outlook via TOutlookApplication component

Hi,

One of our customers has a problem with synchronizing scheduler events from Outlook to our application.
Customer PC details: Windows 10, Outlook 2010.
Our application uses component "TOutlookApplication" to connect to Outlook.
When connecting to Outlook the user is asked to close Outlook before starting sync.
procedure TForm1.btStartImportClick(Sender: TObject);
var Calendar: MAPIFolder;
      NmSpace: NameSpace;
begin
  try
    appOutlook.ConnectKind := ckRunningOrNew;
    appOutlook.Connect;
    try
      NmSpace := appOutlook.GetNamespace('MAPI');
      NmSpace.Logon('', '', False, False);
      Calendar := NmSpace.GetDefaultFolder(olFolderCalendar);
      ProgressBar1.Max := Calendar.Items.Count;
    except
      On E: EOleException do  // uses ComObj
      begin
        E.Message := FullOleExceptionMessage(E);
        ShowMessage(E.Message);
        Exit;
      end;
    end;
  finally
    appOutlook.Disconnect;
  end;
end;

Open in new window

The error which is given:
Cannot connect to MS Outlook server is unavailable
Error code: -2147221231 80040111
Facility: ITF code: 4

Open in new window


Does anybody have an idea how this can be solved?
DelphiOutlook

Avatar of undefined
Last Comment
Stef Merlijn

8/22/2022 - Mon
Kotteeswaran Rajendran

Hi, Make sure that your username and password are correct. try username like Domain\Username
Stef Merlijn

ASKER
imkottees: There is no username or password given in my code, so I don't quite understand.
NmSpace.Logon('', '', False, False);

Open in new window

Also user doesn't have to login when he starts Outlook normally.
Sinisa Vuk

Try to comment (or delete) lines:
 //appOutlook.ConnectKind := ckRunningOrNew;
 //appOutlook.Connect;

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Stef Merlijn

ASKER
@Sinisa Vuk: Without those settings I get following EOleSysError:
"Uitvoeren vanaf de server is mislukt, ClassID: {0006F03A-0000-0000-C000-000000000046}."
This is when Outlook is NOT already running.

If I start Outlook before sync, than on my system the connection is established.
Sinisa Vuk

do you have line
Outlook := CoOutlookApplication.Create;

Open in new window

somewhere...? How do you bind to Outlook?
Stef Merlijn

ASKER
No I don't. What does it do?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Stef Merlijn

ASKER
I used to use this but it gave problems on some systems:
Try
    appOutlook.ConnectKind := ckRunningOrNew;
    appOutlook.Connect;
    try
      NmSpace := appOutlook.GetNamespace('MAPI');
      NmSpace.Logon('', '', False, False);
      Calendar := NmSpace.GetDefaultFolder(olFolderCalendar);
      ProgressBar1.Max := Calendar.Items.Count;
    except
Finally
  appOutlook.Disconnect;
end;

Open in new window

Stef Merlijn

ASKER
When I use following code I can only use it when Outlook is closed, otherwise I get the same connectionerror.
    Outlook := CoOutlookApplication.Create;
    NmSpace := Outlook.GetNamespace('MAPI');
    NmSpace.Logon('', '', False, False);
    Calendar := NmSpace.GetDefaultFolder(olFolderCalendar);
    ProgressBar1.Max := Calendar.Items.Count;

Open in new window

Sinisa Vuk

Ahhh, you put TOutlookApplication component to form, right? So, that's why you don't have a create.
Hmm, if you put:
    Outlook := CoOutlookApplication.Create;
    Outlook.Connect;
    NmSpace := Outlook.GetNamespace('MAPI');
    NmSpace.Logon('', '', False, False);
...

Open in new window

?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Stef Merlijn

ASKER
Outlook.Connect;  -> undeclared identifier 'Connect'
Stef Merlijn

ASKER
Can it be related that multiple instances of Outlook aren't allowed?
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Stef Merlijn

ASKER
The same error occurs when Outlook is running.
So it doesn't matter which way the connection is established.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Stef Merlijn

ASKER
As I see it, the connection is made correctly, so the error has to come from a different problem.
Does anybody have an idea how to track this down and solve it?
Stef Merlijn

ASKER
Thanks for making sure connection was established correctly.
It turned out that a PDF plugin in Outlook kept some instance of Outlook running in the background even when it was closed. After de-installing the plugin the problem was solved.