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

  1. Open the document in Word, resolve/delete the broken comments, and re-save
  2. Inspect word/document.xml for commentReference ids lacking matching commentRangeStart/End
  3. Strip comment markup from the XML before processing if comments are not needed
  4. 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

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


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)