iOfficeAI/OfficeCLI · error · InvalidOperationException

DispId({name}) hr=0x{hr:X8}

Error message

DispId({name}) hr=0x{hr:X8}

What it means

Thrown by WordPdfBackend.DispId when IDispatch::GetIDsOfNames returns a non-zero HRESULT for a method/property name. This backend drives Office via raw COM IDispatch (manual vtable + GetIDsOfNames/Invoke), so a failed DispId lookup means the COM object does not expose the requested name. The message embeds the name and the raw HRESULT for diagnosis. The class is [SupportedOSPlatform("windows")] — it is Windows-only Word automation.

Source

Thrown at src/officecli/Core/WordPdfBackend.cs:158

                if (s == 1) return;
                throw new InvalidOperationException();
            }
            throw new TimeoutException();
        }
        finally { Rel(info); }
    }

    static int DispId(IntPtr d, string name)
    {
        var np = Marshal.StringToHGlobalUni(name);
        var arr = Marshal.AllocHGlobal(IntPtr.Size);
        var did = Marshal.AllocHGlobal(4);
        try
        {
            Marshal.WriteIntPtr(arr, np);
            var iid = Guid.Empty;
            int hr = VT<F_GetIDsOfNames>(d, 5)(d, ref iid, arr, 1, 0, did);
            if (hr != 0) throw new InvalidOperationException($"DispId({name}) hr=0x{hr:X8}");
            return Marshal.ReadInt32(did);
        }
        finally { Marshal.FreeHGlobal(did); Marshal.FreeHGlobal(arr); Marshal.FreeHGlobal(np); }
    }

    static void Wv(IntPtr v, object? o)
    {
        Marshal.WriteInt64(v, 0); Marshal.WriteInt64(v, 8, 0); Marshal.WriteInt64(v, 16, 0);
        switch (o)
        {
            case null: Marshal.WriteInt16(v, (short)VT_EMPTY); break;
            case int i: Marshal.WriteInt16(v, (short)VT_I4); Marshal.WriteInt32(v, 8, i); break;
            case bool b: Marshal.WriteInt16(v, (short)VT_BOOL); Marshal.WriteInt16(v, 8, (short)(b ? -1 : 0)); break;
            case string s: Marshal.WriteInt16(v, (short)VT_BSTR); Marshal.WriteIntPtr(v, 8, SysAllocString(s)); break;
            case IntPtr p:
                Marshal.WriteInt16(v, (short)VT_DISPATCH); Marshal.WriteIntPtr(v, 8, p);
                if (p != IntPtr.Zero) Marshal.AddRef(p);
                break;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Decode the HRESULT: common values are DISP_E_UNKNOWNNAME (0x80020006) = name not found, or TYPE_E_ELEMENTNOTFOUND.
  2. Confirm Microsoft Word is installed and the correct ProgID/object is activated.
  3. Verify the member name against the Word object model for the installed version.
  4. Run this backend only on Windows ([SupportedOSPlatform("windows")]).
Defensive patterns

Strategy: try-catch

Validate before calling

// Guard the platform and Office presence before using the COM PDF backend
if (!OperatingSystem.IsWindows()) throw new PlatformNotSupportedException("WordPdfBackend requires Windows");
// Ensure Word is installed (TypeFromProgID) before activating

Try / catch

try { var did = DispId(dispatch, "ExportAsFixedFormat"); /* ... */ }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("DispId"))
{ /* ex.Message has name + HRESULT 0x80020006 => member not found on this Word version */ }

Prevention

When it happens

Trigger: Calling DispId against an IDispatch object that lacks the named member: a Word version that renamed/removed the property; querying the wrong interface/object; a member name typo; Office not installed or a different app object than expected.

Common situations: Running on a machine without Microsoft Word installed; a Word version with a different object model; passing the wrong dispatch object; a typo in the member name in the interop layer.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/49b910bd477ff018. Report an issue: GitHub.