Reputation: 11
I am creating an office add-in for Word and i am trying to create custom styles for users to style their texts with. I came accross this example of a styled text and went to try it myself.
Here is the gist code: https://gist.github.com/thomas-idgis/286cd1526b2fbf5bf7359b0dd54464ac
The text that it creates when clicked on run should be styled but it isn't styled, which is the problem cause i want it to be styled as it should be styled from the OOXML code and i can't figure out why it's wrong.
I couldn't get it working, is the example wrong or is my code the problem?
// Create a proxy object for the document body.
var body = context.document.body;
// Queue a command to insert OOXML in to the beginning of the body.
body.insertOoxml(
`<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="TestParagraphStyle"/>
</w:pPr>
<w:r>
<w:t xml:space="preserve">This text should be styled</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/styles.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">
<pkg:xmlData>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
<w:style w:type="paragraph" w:styleId="TestParagraphStyle">
<w:name w:val="Test Paragraph Style"/>
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:ind w:firstLine="1440"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
<w:color w:val="FFF200"/>
<w:sz w:val="40"/>
</w:rPr>
</w:style>
</w:styles>
</pkg:xmlData>
</pkg:part>
</pkg:package>
`,
Word.InsertLocation.start
);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('OOXML added to the beginning of the document body.');
});
Upvotes: 0
Views: 565
Reputation: 5046
We dont have an API to create new styles in Word.js, thats a gap. However there are a couple of ways to achieve this.
On this answer I want to show the first method :)
Here are the basic steps.
what happens on that snippet? the important instruction is this one:
context.document.body.insertFileFromBase64(mybase64, "end").getRange().clear();
that line of code is actually inserting the file and then clearing the inserted content by getting the range from the insertion and erasing it. BUT the styles are persisted in the document! Now you can use them :)
Evidently:
Hope this helps.
Upvotes: 1