Thanks for the quick response on prior question. I have one more issue that I am running into with combining files together. I have a case where I want to import a file into a specified bookmark location. I am using the code below but what happens is that the imported file is being placed on seperate page and I get an additional blank page on the end. What am I doing wrong?
protected void InsertFile(string FileLocation, string bookmarkName)
{
// Get current working document
Document doc = (Document)(WordDocList[CurrentIndexWordDocAndWordApp]);
// Insert the target file into the desired document
InsertDocument(doc.Range.Bookmarks[bookmarkName].
BookmarkStart, new Document(FileLocation));
}
private void InsertDocument(Node node, Document doc)
{
int index;
Document dstDoc = node.Document;
Section insertedSection;
if(node.GetAncestor(typeof(Cell))!=null ||
node.GetAncestor(typeof(Shape))!=null)
{
while(node.NodeType != NodeType.Paragraph)
{
node = node.ParentNode;
}
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
index = node.ParentNode.ChildNodes.IndexOf(node);
foreach(Section section in doc.Sections)
{
foreach(Node sourceNode in section.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be
// inserted into Cell or Shape
if(sourceNode is Paragraph || sourceNode is Table)
{
// Do not insert node if it is a last
// empty paragarph in the section.
if(sourceNode is Paragraph &&
sourceNode == section.Body.LastChild &&
!sourceNode.HasInnerText)
{
break;
}
Node insertedNode = dstDoc.ImportNode(sourceNode, true,
ImportFormatMode.KeepSourceFormatting);
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
}
else
{
// Insertion point is tracked to Section.Body level:
// - insert appended document on section by section basis.
Section dstSection;
Body body = (Body)node.GetAncestor(typeof(Body));
if(body.LastChild!=node)
{
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveTo(node);
builder.InsertBreak(BreakType.SectionBreakContinuous);
dstSection = builder.CurrentParagraph.ParentSection;
}
else
{
dstSection = (Section)node.GetAncestor(typeof(Section));
}
index = dstDoc.Sections.IndexOf(dstSection);
int index0 = index;
foreach(Section section in doc.Sections)
{
insertedSection = (Section)dstDoc.ImportNode(section,
true, ImportFormatMode.KeepSourceFormatting);
// Uncomment this line if you want to get rid of
// headers/footers in the inserted section
//insertedSection.ClearHeadersFooters();
dstDoc.Sections.Insert(index++, insertedSection);
}
dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;
}
}