Problems working with Word in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Julia1
    New Member
    • Sep 2008
    • 1

    Problems working with Word in C#

    Actually I have encountered several problems working with Word.
    Please see if you can help me resolve them.

    My application creates documents (Microsoft.Inte rop.Word.Docume nt) 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:

    Code:
    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);
    }
    Problem #1:
    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:

    Code:
    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++;
    }
    So it is not so trivial for me to merge the documents with their original formatting?????
    Please help me !!!???
    Thanks a lot.
    Last edited by kenobewan; Sep 26 '08, 01:44 PM. Reason: Use code tags
Working...