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

Community
  • 1
  • 1
Dan Kelly
  • 2,634
  • 5
  • 41
  • 61
  • What is `WordApp`? Is it a TWordApplication component, or a generic OleVariant using late binding? – Ken White Jun 28 '12 at 17:50
  • Its a TWordApplication. WordDoc is a TWordDocument. – Dan Kelly Jun 28 '12 at 19:14
  • 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. – Ken White Jun 28 '12 at 19:50
  • ConnectKind is RunningOrNew. Just tried with NewInstance and no joy there. *If* Word is already open then it is fine... – Dan Kelly Jun 29 '12 at 08:11

1 Answers1

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

Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
  • 6
    Have a look also at (https://forums.embarcadero.com/thread.jspa?messageID=437814) for using OleMessageFilter. – Hendra Jun 29 '12 at 05:30
  • 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... – Dan Kelly Jun 29 '12 at 08:21
  • Hendra - can you put that as an answer as it's looking promising and I've a few more comments – Dan Kelly Jun 29 '12 at 08:50
  • 2
    @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. – Marjan Venema Jun 29 '12 at 09:42
  • Link is broken. Anyone archived? – Xel Naga Apr 09 '22 at 09:16
  • @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. – Marjan Venema Apr 09 '22 at 15:00
  • @MarjanVenema Wayback Machine has not archived that URL. – Xel Naga Apr 09 '22 at 23:00
  • @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. – Marjan Venema Apr 10 '22 at 09:42