Can't connect to Outlook via TOutlookApplication component

Stef Merlijn
Stef Merlijn used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
imkotteesSenior Messaging Engineer

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

Author

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 architect
Top Expert 2012

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

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Stef MerlijnDeveloper

Author

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 architect
Top Expert 2012

Commented:
do you have line
Outlook := CoOutlookApplication.Create;

Open in new window

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

Author

Commented:
No I don't. What does it do?
Stef MerlijnDeveloper

Author

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 MerlijnDeveloper

Author

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 architect
Top Expert 2012

Commented:
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 MerlijnDeveloper

Author

Commented:
Outlook.Connect;  -> undeclared identifier 'Connect'
Stef MerlijnDeveloper

Author

Commented:
Can it be related that multiple instances of Outlook aren't allowed?
Software architect
Top Expert 2012
Commented:
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.
Stef MerlijnDeveloper

Author

Commented:
The same error occurs when Outlook is running.
So it doesn't matter which way the connection is established.
Stef MerlijnDeveloper

Author

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 MerlijnDeveloper

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial