I've inserted some text into a Word document using the
wordApplication.Selection.InsertXML() method. Then I've added headers and
footers as follows (this.document is the active document):
HeaderFooter header =
this.document.Sections.First.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
HeaderFooter footer =
this.document.Sections.First.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
// Clear any text in there.
header.Range.Delete(ref missing, ref missing);
footer.Range.Delete(ref missing, ref missing);
// Insert header and footer.
header.Range.InsertXML(this.resourceManager.GetString("HeaderXml",
CultureInfo.CurrentCulture), ref dummy);
footer.Range.InsertXML(this.resourceManager.GetString("FooterXml",
CultureInfo.CurrentCulture), ref dummy);
// Ensure different header and footer on first page.
this.document.Content.PageSetup.DifferentFirstPageHeaderFooter = -1;
// Ensure header is visible.
this.document.ActiveWindow.View.SeekView = WdSeekView.wdSeekFirstPageHeader;
this.document.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
Now, the HeaderXML and footerXML strings simply contain a <w:tbl> element.
In some circumstances, after I've inserted the main text into the document,
Word adds a section break of its own (continuous).
Whilst this still displays everything ok, Word does not save properly. If I
save the document in XML format (no transforms, etc), the header and footer
is not saved out. However, if I save the same document in Word Document
(DOC) format, the header and footer is saved out.
Also, if Word does not add a section break of its own when I insert the text
into the main document, the header and footer is saved in both XML and DOC
format.
It seems that for some reason the code I have for inserting headers and
footers causes them not to be saved out if and only if, Word inserts its own
section breaks when I insert text.
Can someone please help as I have to sort this ASAP?
Thanks.
Haydn
Haydn - 12 Sep 2005 15:34 GMT
Found the problem. Looks like I had embedded paragraphs. The original XML
in the document was like:
<w:p>
<myelement>...</myelement>
</w:p>
Then after the transform, it would look like:
<w:p>
<w:p>
...
</w:p>
</w:p>
Guess the only way Word could handle this was to insert a section break.
All is well once more.
> I've inserted some text into a Word document using the
> wordApplication.Selection.InsertXML() method. Then I've added headers and
[quoted text clipped - 44 lines]
>
> Haydn