'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsSystem.LinqImportsGrapeCity.Documents.Word '' This sample shows how to insert the whole body of an existing DOCX into '' another document at a location where an arbitrary search string is found.'' One interesting point in this sample is that the code ensures that the'' insert position is valid, i.e. the source content (all paragraphs of'' the source document) can be inserted at the found point. If necessary,'' the target is split so that there is a content object of a valid type'' at the point where the source is to be inserted.'' In this sample, the target document is JsFrameworkExcerpt.'' The source document inserted is SampleParagraphs, and it is inserted'' on page 2 of the source document, immediately before the string'' "software design principles".PublicClassInsertAtFoundPublicFunctionCreateDocx() AsGcWordDocument'' The string to find. The source document will be inserted before this stringConstfindPattern = "software design principles" '' Target document where the source document will be inserted:Dim doc = NewGcWordDocument() doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx")) '' Source document that will be inserted into the target:Dim sourceDoc = NewGcWordDocument() sourceDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"))'' Note: the inserted document starts with a 'Heading 1', which adds a page break.'' We change it to 'Heading 2' to avoid this so that the result is more clear: sourceDoc.Body.Paragraphs.First.Style = sourceDoc.Styles(BuiltInStyleId.Heading2) '' Find the first occurrence of the search string:Dim findResult = doc.Body.Find(findPattern, NewFindOptions(doc) With {.IgnoreCase = True}).FirstOrDefault()If findResult IsNothingThenThrowNew Exception("Unexpected: search string not found.")EndIf '' Find a valid insertion point near the found string:Dim insertObject = CreateInsertPoint(findResult) '' Copy the source document to the target at insertion point sourceDoc.Body.CopyTo(insertObject.GetRange(), InsertLocation.Before) '' DoneReturn docEndFunction '' Walk up the parent chain And determine where we are - inside body, cell Or contentcontrol.'' Return the original object if we are inside body Or cell.'' Return contentcontrol if we are inside contentcontrol.PrivateSharedFunctionGetAnchorObject(testedObject AsContentObject) AsContentObjectDim originalObject = testedObjectWhileTrueIf testedObject.ParentContentIsNothingThenReturn originalObjectElseIfTypeOf testedObject IsCellThenReturn originalObjectElseIfTypeOf testedObject IsContentControlThenReturn testedObjectElse testedObject = testedObject.ParentContentEndIfEndWhileReturnNothingEndFunction '' This method assumes that testObject Is always an entity inside a paragraph.PrivateSharedFunctionGetParentParagraph(testObject AsContentObject) AsParagraphWhile testObject IsNotNothingIfTypeOf testObject IsParagraphThenReturnCType(testObject, Paragraph)EndIf testObject = testObject.ParentContentEndWhileThrowNew ArgumentException("testObject is not inside a paragraph.")EndFunction PrivateSharedFunctionCreateInsertPoint(fr AsFindResult) AsContentObjectDim anchorObject = GetAnchorObject(fr.Range.First())Dim foundParagraph = GetParentParagraph(anchorObject) If fr.StartIndex > 0AndAlsoTypeOf anchorObject IsTextThen anchorObject = CType(anchorObject, Text).Split(fr.StartIndex)EndIfIf foundParagraph Is anchorObject Then anchorObject = foundParagraph.Split(anchorObject, InsertLocation.End)Else anchorObject = foundParagraph.Split(anchorObject, InsertLocation.Before)EndIfReturn anchorObjectEndFunctionEndClass