win32com.client.Dispatch("CorelDRAW.Application.21")
Connect by version number; 21 is CorelDRAW 2019. If CDR is already running this attaches to that process, otherwise it starts a new one.
Signature
Notes
Python example (runnable)
pywin32. The example assumes app / doc / page / layer are already connected.
See Connection & Initimport win32com.client # COM client module
VER = 21 # 21=2019 22=2020 23=2021 24=2022/2023 25=2024
app = win32com.client.Dispatch(f"CorelDRAW.Application.{VER}")
# app : root object; every other call hangs off it
print("Connected to CorelDRAW, version: ", app.Version)
What Dispatch actually does
Dispatch does not "start CorelDRAW". It takes a ProgID, asks the Windows COM
registry for it, and gets back an object pointer through IDispatch. Once that is
clear, most of the odd behaviour people hit becomes explainable:
win32com.client.Dispatchreturns a dynamic proxy, not a typed object. A misspelled method or property name does not fail when you write it — it fails when you call it.- CorelDRAW registers as a local server and is effectively a single-instance application.
If CorelDRAW is already running,
Dispatch("CorelDRAW.Application.21")normally attaches to the running instance rather than starting a second one. If nothing is running, it launches. - So it carries both meanings: connect and launch. To be explicit, use
GetActiveObject(attach only, never launch) orEnsureDispatch(goes through the type library).
The ProgID argument
The form is CorelDRAW.Application.<version>. The number is CorelDRAW's
internal program version, not the release year — the two differ by a fixed offset. The commonly
cited mapping:
- X4 → 14, X5 → 15, X6 → 16, X7 → 17, X8 → 18
- 2017 → 19, 2018 → 20, 2019 → 21, 2020 → 22, 2021 → 23, 2022 → 24
Corel changed how versions are numbered from 2023 onward, and the tables floating around online frequently disagree. Do not guess — read the real value from the registry. Two keys matter:
HKEY_CLASSES_ROOT\CorelDRAW.Application\CurVer— the default value is the ProgID of the newest registered version, for exampleCorelDRAW.Application.24.HKEY_CLASSES_ROOT\CorelDRAW.Application.21— if this key exists, V21 is installed and registered.
If you omit the version, Dispatch("CorelDRAW.Application") resolves through
CurVer. When several versions are installed and you must pin one, always write the
full version — this is the single most common reason a script behaves differently on someone
else's machine.
Return value
You get a dynamic dispatch object; every later call goes through
IDispatch::Invoke. Three consequences: autocomplete is largely absent; whether a
property reads or writes depends on whether it exposes propget / propput;
and collection indices start at 1, not 0. That last one catches the most people.
Choosing between Dispatch, GetActiveObject and EnsureDispatch
- Dispatch — the general-purpose call. Attaches if an instance exists, launches if not. What most scripts want.
- GetActiveObject — attach only, never launches. Called from
pythoncom; raises immediately if nothing is running. Right for add-in style scripts that should operate on whatever the user already has open rather than quietly spawning a second process. - EnsureDispatch / gencache — reads the type library and generates a Python
wrapper module, which gives real method signatures and catches typos earlier. The cost is a
slow first run and a cache tied to the version: after upgrading you may need to clear
gen_pyand regenerate.
Common errors and what they mean
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)— wrong ProgID, or that version is not installed.-2147221005is0x800401F3. ReadCurVerunderCorelDRAW.Applicationand compare.(-2147221164, 'Class not registered', None, None)— the classic bitness mismatch: 32-bit Python cannot see a 64-bit CorelDRAW registration, or the reverse. That is0x80040154. Check withpython -c "import sys;print(sys.maxsize > 2**32)"—Truemeans 64-bit.- No error, but "nothing happened" — after a successful
Dispatchthe window may still be hidden. Setapp.Visible = Trueand look again. - It crashes when called from a worker thread — COM must be initialised per thread. Call
pythoncom.CoInitialize()on entry andpythoncom.CoUninitialize()on exit.
Complete example
With initialisation, error handling and teardown. Note that it deliberately does
not call app.Quit() — if you attached to an instance the user is
working in, quitting closes their unsaved documents. The rule for that is at the end.
import pythoncom
import win32com.client
def connect(progid="CorelDRAW.Application.21"):
pythoncom.CoInitialize() # optional on the main thread, required in workers
try:
app = win32com.client.Dispatch(progid)
except pythoncom.com_error as e:
hr, msg = e.args[0], e.args[1]
if hr == -2147221005:
raise SystemExit("No such ProgID: check the version, or read CurVer")
if hr == -2147221164:
raise SystemExit("Class not registered: Python/CorelDRAW bitness mismatch")
raise
app.Visible = True # without this you drive an invisible window
print("connected:", app.Version)
return app
app = connect()
doc = app.ActiveDocument or app.CreateDocument()
print("document:", doc.Name, doc.Pages.Count, "pages")
Should you call Quit?
One question decides it: who started the instance.
- Your script launched it (CorelDRAW was closed beforehand) — yes, call
app.Quit(), otherwise you leave orphan processes behind and a debugging session will pile up a dozen of them. - You attached to the user's open instance — do not quit. Close only the
document you created:
doc.Close(). This is the first thing people complain about when someone else's script runs on their machine.
Platform and version notes
All of this only holds on Windows. The macOS build of CorelDRAW has no COM and no equivalent
scripting interface, so no win32com approach applies there — the only option is
"export a file, process it outside".
About this section: Connection & Init
Connection & Init — connecting to CorelDRAW over COM.
- This section has 8 entries; this is one of them.
- See the full section index: Connection & Init
Other entries in this section
- pythoncom.CoInitialize()COM initialization; it goes on the first line of the script. Any threa
- app.Visible = TrueShows the CDR window. Setting it False is faster for batch runs, but k
- win32com.client.Dispatch("CorelDRAW.Application")No version number, so it binds to whatever version is registered as de
- win32com.client.GetActiveObject("CorelDRAW.Application.21")Attaches to an instance that is already open and never starts a new pr
- win32com.client.gencache.EnsureDispatch(progid)Once the constant cache exists you can write cdrTextShape instead of 6
- app.VersionReturns the version as a string. Use it to confirm which version you a
- app.Quit()Closes CorelDRAW entirely. Don't call it at the end of a batch job — i