Using pywin32, what is the difference between Dispatch and DispatchEx?

14,557

Solution 1

It depends on what you want. If Excel is already open, using dispatch will create a new tab in the open Excel instance. If Excel is already open, using dispatchEx will open a new instance of Excel.

Solution 2

DispatchEx is not documented, and Dispatch is, which already implies the practical answer: Just use Dispatch, unless you have some very good reason to believe you have a special case.

However, if you want to know the differences under the covers:

From the pywin32 source, you can see that DispatchEx tries to return a wrapped-up IDispatchEx interface rather than IDispatch. (Not too surprising, given the names.)

You can look up IDispatchEx at MSDN, and you'll see that it's

an extension of the IDispatch interface, supports features appropriate for dynamic languages such as scripting languages.

The idea is that, if the thing you're automating is a dynamic object (like the kind of object you have in Python or Javascript), rather than a static object (like the kind of object you have in C++ or Java), Visual Basic code can access its dynamic nature—enumerate, add, and remove members at runtime, etc.

And of course pywin32 can do almost everything VB can, you just need to be a bit more explicit sometimes. In this case, you need to create a DispatchEx, and call its DeleteMemberByName etc. methods.

Solution 3

If you're using COM to automate, say, Excel there will come a time when you want .Dispatch to fire up a new instance of the application, not to interfere with the one already running on the desktop.

Use DispatchEx instead of Dispatch

Share:
14,557
pandita
Author by

pandita

Updated on September 15, 2022

Comments

  • pandita
    pandita over 1 year

    When opening e.g. a spreadsheet with pywin32, I found two options to do so:

    excel1 = win32com.client.DispatchEx('Excel.Application')
    wb = excel1.Workbooks.Open('myxls.xls')
    

    or I could do

    excel2 = win32com.client.Dispatch('Excel.Application')
    wb = excel2.Workbooks.Open('myxls.xls')
    

    and I'm wondering if this makes any difference. The docstrings don't help me much either:

    >>> w32.Dispatch.__doc__
    'Creates a Dispatch based COM object.\n '
    
    >>> w32.DispatchEx.__doc__
    'Creates a Dispatch based COM object on a specific machine.\n  '
    

    In this site they suggest that DispatchEx might be for remote access.

    Does it make any difference which method I use when I'm simply trying to automate spreadsheets on my own PC?

  • Brian Schlenker
    Brian Schlenker about 10 years
    Something I just ran into that can go wrong with Dispatch. If you are writing a GUI (or anything threaded) and want to do some background work in a thread that requires a com object instance, you must first call pythoncom.CoInitialize() or you'll get a bunch of com_errors. Now, if you try to do something in the foreground using an object created by Dispatch, while the background task is using an object created by Dispatch (they will refer to the same application), calls will be rejected. DispatchEx fixes that by using separate instances per thread.
  • Diego Vélez
    Diego Vélez about 9 years
    I'm using Dispatch and it opens a new Excel instance anyway. Why? I want to connect to the instance already open. How should I do?
  • Joe
    Joe about 3 years
    I found that there, too. But looking at the source code you can see that it uses CoCreateInstanceEx(). And the comment on that is Create a new instance of an OLE automation server possibly on a remote machine. Maybe the new instance is a side effect of the new automation server?