win32com.client.GetActiveObject("CorelDRAW.Application.21")
Attaches to an instance that is already open and never starts a new process. It raises if CDR isn't running, which makes it the right call for "open CDR by hand firs
Signature
Notes
Python example (runnable)
pywin32. The example assumes app / doc / page / layer are already connected.
See Connection & Initimport win32com.client
# attach to an already running CorelDRAW, never start a new process
try:
app = win32com.client.GetActiveObject("CorelDRAW.Application.21")
print("Attached to the running CorelDRAW")
except Exception:
# raises if CorelDRAW is not running; catch it and say so plainly
print("CorelDRAW is not running; open it manually, then run this script")
It attaches, it does not launch
The whole difference from Dispatch is one sentence: Dispatch will start the
process for you, GetActiveObject will not. If CorelDRAW is not running, it raises and
does nothing else.
pythoncom.com_error: (-2147221021, 'Operation unavailable', None, None)
-2147221021 / 0x800401E3 is MK_E_UNAVAILABLE. It is a
recognisable code: it means "no instance to attach to", and there is no point looking anywhere else
for the cause.
"Does not launch" is not a limitation, it is the entire value. For plugins, macros, and small tools you hand to colleagues, the worst possible behaviour is a script that quietly starts its own CorelDRAW — the user's desktop suddenly grows a window, or worse, the script edits a new process while the user believes their own document is being changed.
It searches the ROT, not the process list
This is the most commonly misunderstood part. GetActiveObject queries the Running Object
Table — a registry of objects that processes have voluntarily published under "I am running, here is my
object". It does not enumerate processes and it does not look at Task Manager.
So "the process is running" and "I can find it" are two different facts. In all of these cases the
process is right there and GetActiveObject still returns MK_E_UNAVAILABLE:
- Different Windows session. A script running as a service sits in session 0; the user's CorelDRAW sits in session 1. They cannot see each other. A disconnected RDP session gets the same isolation.
- Instance belonging to another user. The ROT is per-session and per-user; user A cannot see user B's objects.
- Different integrity level. A low-integrity process (a script dropped by some security policy, for example) cannot attach to a high-integrity object — UIPI blocks it.
- An instance started for automation. Some launch paths never register the object
in the ROT. Those instances can only be reached with
Dispatch.
Debug in that order: same session, same user, same integrity level. Once all three line up, then suspect the code.
The version in the ProgID decides whether you find it
Call GetActiveObject("CorelDRAW.Application.21") while 2023 is running and even though
CurVer points at 24, you get nothing — that ProgID resolves to a different CLSID, and the
ROT holds the CLSID of the running instance.
A more durable approach is to try without a version first (so CurVer
resolves to the current default CLSID), then fall back to trying candidate versions one by one. Wide
to narrow outlives a hard-coded version number.
pywin32 exposes two entry points; do not mix them
win32com.client.GetActiveObject(progid)— returns a ready-to-call dispatch object.pythoncom.GetActiveObject(progid)— returns aPyIUnknownthat still needswin32com.client.Dispatch(unk)around it. Use it directly and you get "no such attribute" on every call.
When you see both styles in one file, it was assembled from two different sources.
A complete version, including the failure path
The real question here is a design decision, not syntax: if nothing is running, should the script start CorelDRAW itself? For anything you hand to an end user the answer is usually no — say so clearly and let them open it.
import pythoncom
import win32com.client
def attach():
# Attach to an already-running instance. Never launch one silently.
pythoncom.CoInitialize()
for progid in ("CorelDRAW.Application",
"CorelDRAW.Application.24",
"CorelDRAW.Application.23"):
try:
app = win32com.client.GetActiveObject(progid)
print("attached:", progid, "version", app.Version)
return app
except pythoncom.com_error as e:
if e.args[0] == -2147221021: # MK_E_UNAVAILABLE
continue
raise
raise SystemExit("No running CorelDRAW found - open it first, then run this script")
app = attach()
print("active document:", app.ActiveDocument.Name if app.ActiveDocument else "none")
Note the app.ActiveDocument guard. An attached instance can be running with no document
open. In that case ActiveDocument is None and reading .Name
straight off it raises AttributeError — a plain Python error with nothing to do with COM.
Three rules of use
- Never
Quit(). You did not start that instance. Quitting it closes documents the user may be editing with unsaved work. To clean up, close only the documents you created. - Do not use it to test whether CorelDRAW is installed. Not-running and not-installed
produce the same error code. Check installation via the registry
CurVer; use this call only to find out whether it is running. - Do not use it as a heartbeat. Some scripts call
GetActiveObjectin a loop to detect the user closing CorelDRAW. That hammers the ROT for no reason. Catch the exception on an actual call instead.
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
- win32com.client.Dispatch("CorelDRAW.Application.21")Connect by version number; 21 is CorelDRAW 2019. If CDR is already run
- 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.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