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?
Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imkotteesSenior Messaging EngineerCommented:
Hi, Make sure that your username and password are correct. try username like Domain\Username
Stef MerlijnDeveloperAuthor Commented:
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 VukSoftware architectCommented:
Try to comment (or delete) lines:
 //appOutlook.ConnectKind := ckRunningOrNew;
 //appOutlook.Connect;

Open in new window

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Stef MerlijnDeveloperAuthor Commented:
@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 VukSoftware architectCommented:
do you have line
Outlook := CoOutlookApplication.Create;

Open in new window

somewhere...? How do you bind to Outlook?
Stef MerlijnDeveloperAuthor Commented:
No I don't. What does it do?
Stef MerlijnDeveloperAuthor Commented:
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 MerlijnDeveloperAuthor Commented:
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 VukSoftware architectCommented:
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

?
Stef MerlijnDeveloperAuthor Commented:
Outlook.Connect;  -> undeclared identifier 'Connect'
Stef MerlijnDeveloperAuthor Commented:
Can it be related that multiple instances of Outlook aren't allowed?
Sinisa VukSoftware architectCommented:
I don't have Outlook .... but as this article noticed - as opposite to Word, Outlook can be started only once. Try example "Without using the type library" from upper link.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stef MerlijnDeveloperAuthor Commented:
The same error occurs when Outlook is running.
So it doesn't matter which way the connection is established.
Stef MerlijnDeveloperAuthor Commented:
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 MerlijnDeveloperAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.