PHPOffice/PHPWord · error · InvalidArgumentException
Comment with id isn't referenced in document
Error message
Comment with id %s isn't referenced in document
What it means
AbstractPart::getCommentReference looks up a comment id in the collected commentRefs map. This exception means the document references a comment (commentReference id) whose range start/end was never recorded, so the reference cannot be resolved.
Solutions
- Open the document in Word, resolve/delete the broken comments, and re-save
- Inspect word/document.xml for commentReference ids lacking matching commentRangeStart/End
- Strip comment markup from the XML before processing if comments are not needed
- Catch the InvalidArgumentException and skip comment handling
Example fix
// before
$commentRefs = $part->getCommentReference($id); // throws if unresolved
// after
try {
$commentRefs = $part->getCommentReference($id);
} catch (\InvalidArgumentException $e) {
$commentRefs = ['start' => null, 'end' => null]; // tolerate dangling refs
} Defensive patterns
Strategy: try-catch
Validate before calling
$xml = $zip->getFromName('word/document.xml'); preg_match_all('/commentReference w:id="(\d+)"/', $xml, $refs); preg_match_all('/commentRangeStart w:id="(\d+)"/', $xml, $starts); $dangling = array_diff($refs[1], $starts[1]); // non-empty means broken comment refs Try / catch
try { $phpWord = \PhpOffice\PhpWord\IOFactory::load($docFile); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'isn\'t referenced')) { // resave document or strip comments } throw $e; } Prevention
- Clean up comments in Word before pipeline processing
- Detect dangling comment references during document intake
- If comments are irrelevant, remove comment markup before parsing
When it happens
Trigger: A document.xml contains <w:commentReference w:id="X"/> (or getCommentReference is called during read) but no commentRangeStart/End with id X was parsed — e.g. a dangling comment reference after comment ranges were stripped or the comment part is missing.
Common situations: Documents edited by tools that delete comment ranges but leave references; manually trimmed XML; docx produced by generators with inconsistent comment markup.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Type must be "start" or "end"
- The part " " doesn't exist
- Invalid value, on of ' . implode(', ', $position) . '…
- Invalid value, on of ' . implode(', ', $restartNumbers) . '…
- Invalid value, dirty or clean possible
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/285086281a07a3cb.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Reader/Word2007/AbstractPart.php:181
$this->commentRefs[$id] = [
'start' => null,
'end' => null,
];
}
$this->commentRefs[$id][$type] = $element;
return $this;
}
/**
* Get comment reference.
*
* @return array<string, null|AbstractElement>
*/
protected function getCommentReference(string $id): array
{
if (!array_key_exists($id, $this->commentRefs)) {
throw new InvalidArgumentException(sprintf('Comment with id %s isn\'t referenced in document', $id));
}
return $this->commentRefs[$id];
}
/**
* Read w:p.
*
* @param AbstractContainer $parent
* @param string $docPart
*
* @todo Get font style for preserve text
*/
protected function readParagraph(XMLReader $xmlReader, DOMElement $domNode, $parent, $docPart = 'document'): void
{
// Paragraph style
$paragraphStyle = $xmlReader->elementExists('w:pPr', $domNode) ? $this->readParagraphStyle($xmlReader, $domNode) : null;
View on GitHub (pinned to aef95c0415)