"Call Rejected By Callee"
Asked Answered
P

1

6

We're in the process of moving a slew of our applications from Windows XP to Windows 7 and have run into an old problem with Word Automation.

We had an issue in one of our applications where we would get "Call rejected by Callee" when trying to connect to Word, unless it was already open. We worked around it in Delphi 2000 / Windows XP with the following code:

WordApp.Connect;
WordApp.Visible := True;
WordApp.Documents.Add(Template, EmptyParam, EmptyParam, EmptyParam);
WordApp.ChangeFileOpenDirectory(jdir);
WordApp.Visible := False;
WordDoc.ConnectTo(WordApp.ActiveDocument);

This no longer does the trick under Windows 7 - and recompiling under XE2 doesn't seem to help.

I've seen a related question here which pertains to Visual Studio - anyone know how to apply that to Delphi (XE2 would be fine at this stage)

Dan

Pontone answered 28/6, 2012 at 16:38 Comment(4)
What is WordApp? Is it a TWordApplication component, or a generic OleVariant using late binding?Chelyuskin
Its a TWordApplication. WordDoc is a TWordDocument.Pontone
Did you try changing the TWordApplication.ConnectKind to ckRunningOrNew? This is supposed to connect to a running instance if there is one, and open a new one automatically if there isn't.Chelyuskin
ConnectKind is RunningOrNew. Just tried with NewInstance and no joy there. If Word is already open then it is fine...Pontone
U
9

"Call rejected by callee" errors happen when the instance that you are connected/connecting to is currently in an interactive mode: an open dialog for example. Or, in Excel, a cell being edited, or even being in a state where a cell being edited was interrupted by the user switching away from the application - when (s)he returns it may look that the edit was completed, but the interactive mode is not ended until a different cell is selected.

Because of this I don't understand why you were getting this error when connecting unless another instance was already open. If there is no instance open (and visible), Word cannot be in interactive mode and you shouldn't have been getting the error. Is it possible your remedy merely circumvented the real problem?

No matter what though, you are in a situation where you are trying to connect to an instance that is in interactive mode. Either beforehand, or caused by your code. As you switched from XP to Windows 7, UAC does come to mind as a possible culprit.

I'd do away with the work-around, and see where that takes you.

For Word automation I always make sure that:

  • I connect to a dedicated instance by using a ConnectKind of ckNewInstance and
  • make sure I do not make my dedicated instance visible or
  • make sure I only make it visible after I am all done and can turn the instance over to the user.

If you have no option but to automate against a visible (and thus non-dedicated) Word instance, you will simply have to deal with the possibility of this error coming up. When it does, alert the user to what is happening and make sure you offer a retry.

Update The thread on the Embarcadero forums mentioned in the comments by @Hendra includes a link to some very useful MSDN documentation: Fixing 'Application is Busy' and 'Call was Rejected By Callee' Errors

Unitarianism answered 28/6, 2012 at 21:12 Comment(8)
Have a look also at (forums.embarcadero.com/thread.jspa?messageID=437814) for using OleMessageFilter.Underlaid
Marjan - that was my understanding as well. This one has bugged us for a while. It seems to be a timing issue - if I step through then the issue doesn't happen, but adding a sleep() doesn't help. The link in the original question suggests that it's a threading problem...Pontone
Hendra - can you put that as an answer as it's looking promising and I've a few more commentsPontone
@Hendra: Good one, thanks for the link. The link in that thread is really helpful. Have added it to my answer, but I think you should add your comment as an answer to this question, perhaps with a small summary as "link only" answers are discouraged on SO. I'd certainly upvote it.Unitarianism
Link is broken. Anyone archived?Diverticulum
@XelNaga, try the wayback machine aka the internet archive at archive.org and enter the original link into its search box. Click on the last crawled date to get the most recent archived copy of the page.Unitarianism
@MarjanVenema Wayback Machine has not archived that URL.Diverticulum
@XelNaga Then why does it tell me it has archived it 10 times and shows me the page when I click on the last of those archive dates? Use the original URL from my post, not the one that MS redirects you to.Unitarianism

© 2022 - 2024 — McMap. All rights reserved.