Inserting Documents

May 14, 2012 at 3:01 AM

I need to merge several documents.  I'm using InsertDocument, and for the most part, everything is working great. However, there are a few minor problems and I'm not sure what the best approach it to resolve these issues.

When I merge documents, I have certain documents that have their fonts and font sizes changed.  I'm also having a problem with the resultant file being corrupted, however if I open it in Word 2010 and ignore the warning, the file is repaired.

Is there something I can do to the files that I'll be merging to reduce or eliminate these problems?  Or is this a way to use the line number in document.xml that is given in the error message to determine what is causing the file to be corrupt?

Developer
May 14, 2012 at 6:55 AM

Which version are you using? Can you try to compile the code yourself from newest source?

May 14, 2012 at 2:13 PM

Sorry, should have mentioned that.  I'm using the latest source.

Developer
May 14, 2012 at 10:59 PM

Can you provide some test code along that we can mimic this behaviour?

May 15, 2012 at 1:13 AM

I suspect the problem lies within one of the Word docs, but here's the relevant parts of the code.  The error is easily reproduced with the right Word Docs.  However, those are proprietary.  I could send them to you privately, but can't post them on a board.

 

public partial class Form1 : Form    {       

public Form1()       

{           

InitializeComponent();           

// load a checklist box with files that the user may select to be included

checkedListBox1.Items.Add("File A", true);           

checkedListBox1.Items.Add("File B, true);           

checkedListBox1.Items.Add("File C", true);           

checkedListBox1.Items.Add("File D", true);           

}

 

private void button1_Click(object sender, EventArgs e)       

{

DocX masterDoc = null;           

String masterFileName = "";

//merge all checked documents


            foreach (object o in checkedListBox1.CheckedItems)

{               

DocX thisDoc = DocX.Load(Path.Combine(Directory.GetCurrentDirectory(), o.ToString() + ".docx"));               

if (masterDoc == null)               

{

masterDoc = thisDoc;                   

masterFileName = o.ToString() + ".docx";               

}               

else               

{                   

masterDoc.InsertParagraph().InsertPageBreakAfterSelf();                   

masterDoc.InsertDocument(thisDoc);               

}           

}           

if (!masterFileName.Equals(""))           

{               

masterDoc.Save();               

Process.Start(Path.Combine(Directory.GetCurrentDirectory(), masterFileName));           

}       

}
}