My task is to place a comment via Office Javascript API in a Word document (.docx) compatible to Word 2007 or later.
I found, that there is no direct way to do this via Microsoft API.
Since I am able to pass OOXML to the Word document, I thought that I can use this to place a comment.
I did some research on the Word document structure and found, that comments are stored in a separate XML file called "comments.xml" and then referenced via an ID in the "document.xml" (I attached a corresponding sample).
Is there a way to edit this comments.xml
via API in order to place comments in the Word document or is this not possible?
Sample "document.xml":
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
mc:Ignorable="w14 wp14">
<w:body>
<w:p w:rsidR="00A9590C" w:rsidRDefault="0058668B">
<w:r>
<w:t>I am text.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="0058668B" w:rsidRDefault="0058668B">
<w:commentRangeStart w:id="0"/>
<w:r>
<w:t>I am text with comment.</w:t>
</w:r>
<w:commentRangeEnd w:id="0"/>
<w:r>
<w:rPr>
<w:rStyle w:val="Kommentarzeichen"/>
</w:rPr>
<w:commentReference w:id="0"/>
</w:r>
</w:p>
<w:sectPr w:rsidR="0058668B">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708"
w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
Sample "comments.xml":
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:comments
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
mc:Ignorable="w14 wp14">
<w:comment w:id="0" w:author="rz" w:date="2015-05-23T10:30:00Z" w:initials="r">
<w:p w:rsidR="0058668B" w:rsidRDefault="0058668B">
<w:pPr>
<w:pStyle w:val="Kommentartext"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="Kommentarzeichen"/>
</w:rPr>
<w:annotationRef/>
</w:r>
<w:r>
<w:t>Test</w:t>
</w:r>
<w:bookmarkStart w:id="1" w:name="_GoBack"/>
<w:bookmarkEnd w:id="1"/>
</w:p>
</w:comment>
</w:comments>
This is the most information I have been able to find while researching this with you:
https://msdn.microsoft.com/en-us/magazine/jj991976.aspx
Note: One good way to learn about how to manipulate OOXML from an app is to add the content that you want to work with using the UI (for example, inserting SmartArt by clicking Insert | Illustrations | SmartArt), getting the OOXML for the content using getSelectedDataAsync and then reading the results.
I would do this, and then send those results as ooxml using setSelectedDataAsync, and that will answer your question. Is office smart enough to create those references itself, or is it not? (if not, there is nothing you can do about it through the api)
OLD COMMENT (Where I found your first premise to be true. Please ignore, or read for laughs)
It looks like you can use the setSelectedDataAsync function to pass in comment values and apply them to the current selected content in the word document. Here are the two most relevant snippets:
Office.context.document.setSelectedDataAsync(data [, options], callback(asyncResult));
and
Office.CustomXMLNodeType.NodeComment "comment" The node is a comment.
From the implementation of coerciontype used in one of microsoft's examples and the fact that coerciontype is an enumeration just like customexmlnodetype... It makes sense to me that this would work.
function writeMatrix() {
Office.context.document.setSelectedDataAsync("test comment"], {CustomXMLNodeType: Office.Office.CustomXMLNodeType.NodeComment}
function (asyncResult) {
var error = asyncResult.error;
if (asyncResult.status === Office.AsyncResultStatus.Failed){
write(error.name + ": " + error.message);
}
});
}
Looking back through the documentation I now find coercionType as an optional parameter under object, and not all enumerations. That is so stupid!!
Here is the information I referenced:
Check this out: https://msdn.microsoft.com/en-us/library/office/fp142145.aspx
And this: https://msdn.microsoft.com/EN-US/library/office/fp142154.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With