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
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.
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
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.