iOfficeAI/OfficeCLI · error · InvalidOperationException
swap not supported for this document type
Error message
swap not supported for this document type
What it means
Thrown by the `swap` command's handler switch when the opened document's handler is none of PowerPointHandler, WordHandler, or ExcelHandler. The default arm raises InvalidOperationException. In practice the three Office handlers cover all supported extensions, so this is reachable mainly if DocumentHandlerFactory.Open returns an unexpected handler type (e.g. a custom/future handler) for a file that passed the extension check elsewhere.
Source
Thrown at src/officecli/CommandBuilder.Add.cs:513
{
var file = result.GetValue(swapFileArg)!;
var path1 = MsysPathHint.Restore(result.GetValue(swapPath1Arg)!)!;
var path2 = MsysPathHint.Restore(result.GetValue(swapPath2Arg)!)!;
if (TryResident(file.FullName, req =>
{
req.Command = "swap";
req.Args["path"] = path1;
req.Args["to"] = path2;
}, json) is {} rc) return rc;
using var handler = DocumentHandlerFactory.Open(file.FullName, editable: true);
var (p1, p2) = handler switch
{
OfficeCli.Handlers.PowerPointHandler ppt => ppt.Swap(path1, path2),
OfficeCli.Handlers.WordHandler word => word.Swap(path1, path2),
OfficeCli.Handlers.ExcelHandler excel => excel.Swap(path1, path2),
_ => throw new InvalidOperationException("swap not supported for this document type")
};
var message = $"Swapped {p1} <-> {p2}";
if (json) Console.WriteLine(OutputFormatter.WrapEnvelopeText(message));
else Console.WriteLine(message);
NotifyWatch(handler, file.FullName, path1);
return 0;
}, json); });
return swapCommand;
}
}
View on GitHub (pinned to 1ced45e900)
Solutions
- Confirm the file is genuinely .docx/.pptx/.xlsx and not renamed; check the extension matches the content.
- Ensure DocumentHandlerFactory.Open returns one of the three supported handlers for that file.
- If you added a custom handler, extend the swap switch to cover it before calling swap on that type.
Defensive patterns
Strategy: type-guard
Validate before calling
// Only call swap on the three supported handler types.
using var h = DocumentHandlerFactory.Open(file, editable: true);
if (h is not (PowerPointHandler or WordHandler or ExcelHandler))
throw new NotSupportedException($"swap not supported for {h.GetType().Name}"); Type guard
// Type guard: handler supports swap.
static bool HandlerSupportsSwap(DocumentHandler h) =>
h is PowerPointHandler or WordHandler or ExcelHandler; Prevention
- Check the handler type (or file extension) before calling swap.
- Keep swap's switch and the factory's supported extensions in sync when adding new handlers.
When it happens
Trigger: `officecli swap file.<ext> /p1 /p2` where the file opens as a handler not in the three-way switch. Realistically: a misconfigured factory, a corrupted file mis-detected as a different format, or a new handler type added without updating swap's switch.
Common situations: A future handler type is registered but swap was not updated; the file extension was spoofed/renamed so the factory picks an unexpected handler; an internal test fixture returns a stub handler; a plugin-influenced open path yields a non-standard handler.
Related errors
- swap not supported for this document type
- unsupported_format
- 'swap' command requires 'path' and 'path2' (or 'to') fields.
- Chart at {parentPath} is not a standard chart (extended cx c
- Move not supported for: {elementRef}. Supported: row[N], col
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/2e115ddda27d57d9.
Report an issue: GitHub.