MiniMax-AI/skills · critical · InvalidOperationException
Document has no MainDocumentPart.
Error message
Document has no MainDocumentPart.
What it means
`doc.MainDocumentPart ?? throw new InvalidOperationException("Document has no MainDocumentPart.")` in the reference `AddFullComment` helper. OpenXML's `WordprocessingDocument.MainDocumentPart` is the part that holds `word/document.xml` (the body text). It is null when the package was created without a main part or opened from a malformed/empty package. The reference code refuses to operate because every comment operation reads and writes the document body through this part.
Source
Thrown at skills/minimax-docx/references/openxml_encyclopedia_part3.md:1364
## 5. Comments (4-File System)
### 5.1 Full 4-File Comment System Setup
Comments require four XML files plus markers in `document.xml`.
```csharp
// This method creates a complete comment with all 4 files properly initialized
public static int AddFullComment(
WordprocessingDocument doc,
string text,
string author,
string initials,
string rangeText,
int? existingCommentId = null)
{
var mainPart = doc.MainDocumentPart
?? throw new InvalidOperationException("Document has no MainDocumentPart.");
int commentId = existingCommentId ?? GetNextCommentId(doc);
// Generate paraId (8-char hex) and durableId (8-digit hex)
string paraId = Guid.NewGuid().ToString("N")[..8].ToUpperInvariant();
string durableId = new Random().Next(0x10000000, 0xFFFFFFFF).ToString("X8");
var body = mainPart.Document!.Body!;
// ─────────────────────────────────────────────────────────────
// FILE 1: word/comments.xml — Main comment content
// ─────────────────────────────────────────────────────────────
var commentsPart = mainPart.WordprocessingCommentsPart
?? mainPart.AddNewPart<WordprocessingCommentsPart>();
if (commentsPart.Comments == null)
commentsPart.Comments = new Comments();
View on GitHub (pinned to 60aaae52bb)
Solutions
- When creating new documents, initialize the part: `var main = doc.AddMainDocumentPart(); main.Document = new Document(new Body());` before calling comment methods.
- When opening existing files, verify the source is a valid Word document (not a `.dotx` template or empty file) with `doc.MainDocumentPart != null` before use.
- Use `WordprocessingDocument.Open(path, true)` on a real `.docx`; if it lacks a main part, recreate it from a known-good template.
Example fix
// before — throws on a fresh doc using var doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document); AddFullComment(doc, "hi", "me", "MJ"); // after — seed the main part first using var doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document); var main = doc.AddMainDocumentPart(); main.Document = new Document(new Body()); AddFullComment(doc, "hi", "me", "MJ");
Defensive patterns
Strategy: validation
Validate before calling
void EnsureMainPart(WordprocessingDocument doc)
{
if (doc.MainDocumentPart is null)
{
var main = doc.AddMainDocumentPart();
main.Document = new Document(new Body());
}
}
// before any comment operation
EnsureMainPart(doc); Type guard
bool HasMainPart(WordprocessingDocument doc) => doc.MainDocumentPart is not null;
Try / catch
try
{
AddFullComment(doc, text, author, initials, rangeText);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("MainDocumentPart"))
{
// seed the part and retry once
var main = doc.AddMainDocumentPart();
main.Document = new Document(new Body());
AddFullComment(doc, text, author, initials, rangeText);
} Prevention
- Always call `AddMainDocumentPart()` and seed a `Document(new Body())` immediately after `WordprocessingDocument.Create`.
- Open from a known-good `.docx` template instead of creating empty packages when you need a full document.
- Centralize document creation in one factory method that guarantees a non-null main part.
- Assert `doc.MainDocumentPart != null` in debug builds before any manipulation.
When it happens
Trigger: Calling `AddFullComment` on a `WordprocessingDocument` created with `WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)` but no `AddMainDocumentPart()` was ever called, or on a `.docx` that is actually a template/empty shell missing `word/document.xml`.
Common situations: Creating a brand-new document and forgetting to initialize the main part, opening a `.docx` that is really a macro-enabled template or a corrupt package, or chaining operations on a document that was closed/disposed.
Related errors
- Document has no main part.
- Document has no comments part.
- Relationship {oldRelId} does not point to an ImagePart.
- Image format '{ext}' is not supported by OpenXML.
- Missing word/document.xml
AI-assisted analysis of MiniMax-AI/skills@60aaae52bb (2026-08-13).
Data as JSON: /api/errors/f4da7436808a71a6.
Report an issue: GitHub.