I'm working on an export of an HTML file to a Open XML wordfile. If in the HTML <h1>
is used, I want to add a Heading1 style to that part. But somehow when I open the document in Microsoft Word 2010, the Style isn't applied.
If I open the created document in Libre Office, there is some style applied.
I also defined some styles myself, and if I use one of those styles, everything went well in Word and Libre Office.
I opened the Open XML SDK 2.5 Productivity Tool for Microsoft Office and when I look in the example code it provides, it suggests:
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Kop1" };
Kop1 (instead of Heading1) is because my Word is in Dutch so my template is in Dutch, but this didn't solve the problem.
In this code sample I create the paragraph and add style and text to it:
using (wordDocument = WordprocessingDocument.Open(documentStream, true))
{
MainDocumentPart mainPart = wordDocument.MainDocumentPart;
WP.Body body = wordDocument.MainDocumentPart.Document.Body;
WP.Paragraph para = body.AppendChild(new WP.Paragraph());
StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
if (part != null)
{
WP.ParagraphProperties pPr = new WP.ParagraphProperties();
WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Heading1" };
pPr.Append(paragraphStyleId1);
para.Append(pPr);
}
WP.Run run = para.AppendChild(new WP.Run());
run.AppendChild(new WP.Text(value));
}
It has styles defined, because I work with a template, so the code will come inside the if-statement.
I also tried it with the style title (in Dutch titel), tried both but both didn't work...
I really don't understand what's wrong and why I can use my own created styles but can't use one of the predefined styles of Word, which I didn't remove in the template.
It somehow doesn't recognize the predefined styles?
EDIT/ADDITION
I got it working, but not the way I want, I have to click on the predefined styles in the template-document and then save the document. Somehow now after clicking them and saving these styles are added to the document, because if I now generate my final document using the template, I can use the styles.
But without clicking the styles in the document first, I can't use them becuase they aren't saved?
See Question&Answers more detail:
os