Home/ Reference/ Connection & Init

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

win32com.client.GetActiveObject("CorelDRAW.Application.21")

Notes

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 first, then run the script".

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
import 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.

Error
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:

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

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.

Python
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

About this section: Connection & Init

Connection & Init — connecting to CorelDRAW over COM.

Other entries in this section