Reputation: 963
I'm developing a Word Addin where a user can select some predefined templates (docx) documents that are loaded from SharePoint. Going though a wizard the users sets the content controls in the document. So far a very good experience.
However, I have an Issue when loading a docx file with headers.
I'm using this function to load the docx file: (full code below)
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
That works, but sometimes the headers of the source document are not present. Or other times they are on all pages, while the first page should be different.
Question: Should inserting a document with headers and footers work and am I doing anything wrong?
private applyTemplate(template: Template): void {
if (!Office.context.requirements.isSetSupported("WordApi", 1.2)) {
this.errorMessage = 'Deze versie van Word wordt niet ondersteund';
this.showError = true;
return;
}
let calls: [
ng.IPromise<TemplateFile>
] = [
this.appService.getTemplateDocument(template.templateId)
];
this.q.all(calls)
.then((results: any[]) => {
let templateDoc: TemplateFile = results[0].data;
Word.run((context) => {
let body = context.document.body;
let sections = context.document.sections;
context.load(sections, 'body/style');
body.clear();
return context.sync()
.then(() => {
sections.items[0].getHeader(Word.HeaderFooterType.primary).clear();
sections.items[0].getFooter(Word.HeaderFooterType.primary).clear();
return context.sync()
.then(() => {
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
this.appService.setTemplateSelected(template);
return context.sync()
.then(() => {
this.go('/customers');
this.scope.$apply();
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
});
}, ((result: ng.IHttpPromiseCallbackArg<ErrorMessage>) => {
this.errorMessage = result.data.exceptionMessage;
this.showError = true;
this.scope.$apply();
}));
}
***Edit I see this is coming in a new version: https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/application.md
What's the difference with what I'm doing?
Upvotes: 0
Views: 786
Reputation: 5036
that's a by design behavior, when you insert a file using the insertFileFromBase64 method we do not replace neither the header/footers nor the customXMLParts of the document (there might be already headers and footers there, same for XMLParts).
So if you need to update the headers and footers you will have to do it after you do the insertion of the file. (with the api you can insert the 3 types of headers that are supported in word for each section, first page, even, odd pages)
Hope this helps. Thanks! Juan.
Upvotes: 1