Retrieve comments from a word processing document
Word comments are stored in a comments part related from the main document part. The body XML contains comment references; the comment text lives in word/comments.xml.
Open the package for read-only access when retrieving comments. If the main document part has no comments relationship, the document has no comment entries to return.
Read comments
#![allow(unused)] fn main() { pub fn get_comments(path: &Path) -> Result<Vec<String>, Box<dyn std::error::Error>> { let document = WordprocessingDocument::new_from_file_with_settings(path, lazy_settings())?; let main_part = document.main_document_part()?; let Some(comments_part) = main_part.wordprocessing_comments_part(&document) else { return Ok(Vec::new()); }; let xml = comments_part.data_as_str(&document)?.unwrap_or_default(); Ok(extract_text_values(xml)) } }
The helper opens the main document part, follows the comments relationship when present, and extracts text from the comments XML.
Comment markup
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:comment w:id="0" w:author="Ada">
<w:p><w:r><w:t>Review this paragraph</w:t></w:r></w:p>
</w:comment>
</w:comments>
Use the comment id to connect body references with comment entries.
The comments element is the root of the comments part and contains zero or more comment children. A comment can contain block-level WordprocessingML content such as paragraphs. If a comment id is not referenced by a matching commentReference in document content, a consuming application may ignore it; if more than one comment has the same id, only one may be loaded.
In ooxmlsdk, generated schema types include Comments, Comment, and CommentReference.