Please see if you can help me resolve them.
My application creates documents (Microsoft.Interop.Word.Document) each one according to another template(.dot on the hard disc). Then I have to merge them and save as a single .doc (while the original formatting is saved). I do this in the following way:
Expand|Select|Wrap|Line Numbers
- Range rangeTrg = ((Document)(m_WordDocsArr[0])).Content;
- object brk = WdBreakType.wdSectionBreakNextPage;
- object rngEnd = WdCollapseDirection.wdCollapseEnd;
- rangeTrg.Collapse(ref rngEnd);
- for (int i = 1; i < m_WordDocsArr.Count; i++)
- {
- rangeTrg.InsertBreak(ref brk);
- Range rangeSrc = ((Document)(m_WordDocsArr[i])).Content; rangeTrg.FormattedText = rangeSrc.FormattedText;
- rangeTrg.Collapse(ref rngEnd);
- }
If there is one document in m_WordDocsArr, afterwords when I open the .doc by doubleclicking it, Word opens it in Print Layout, if there is more than one document in m_WordDocsArr, Word opens it in Normal layout.
Why?
Problem #2:
The original templates I use have headers and footers.
Somewhy (???) the merged doc has the first document's headers and footers.
I tryed to save the headers and footers of the original docs before merging and assign them afterwords to each section, but the following code throws the exception:
Expand|Select|Wrap|Line Numbers
- ArrayList footers = new ArrayList();
- ....
- footers.Add(((Document)(m_WordDocsArr[i])).Sections.First.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText);
- .....
- int ih = 0;
- foreach (Section sec in ((Document)(m_WordDocsArr[0])).Sections)
- {
- sec.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
- sec.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText = ((Range)footers[ih]).Duplicate; //exception at run time (could not copy ranges) ih++;
- }
Please help me !!!???
Thanks a lot.