Link to home
Create AccountLog in
Avatar of Stef Merlijn
Stef MerlijnFlag 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?
Avatar of Kotteeswaran Rajendran
Kotteeswaran Rajendran
Flag of Malaysia image

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

Open in new window

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

Open in new window

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

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

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

?
Outlook.Connect;  -> undeclared identifier 'Connect'
Can it be related that multiple instances of Outlook aren't allowed?
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The same error occurs when Outlook is running.
So it doesn't matter which way the connection is established.
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?
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.