Using this forum we have written a "InsertDocument" function that imports nodes from one document into another document for the purpose of merging files. I am trying to assign a style to the paragraphs after they are inserted into the destination document. The style is succesfully getting assigned however, the font size is getting overrided. For example:
1. Source.rtf is inserted into Destination document.
2. Each paragraph is assigned a style of "MyStyle" which is defined in the destination document as 8pt Gray text.
3. When opening the destination document in word the text that was inserted now has a style of "MyStyle + 10pt"
How can I prevent the +10pt from being added to the style that I am assigning to these inserted paragraphs? Here is the code:
Public Sub InsertDocument(ByVal oDestinationNode As Node, ByVal oDocToInsert As Document, Optional ByVal StyleName As String = Nothing)
'Validate Input
If oDestinationNode Is Nothing Then Exit Sub
'Determine the proper Destination & Parent Nodes by going up through the tree
Dim oParentNode As CompositeNode = oDestinationNode.ParentNode
Do While True
'The Destination Node Must have a Parent Node
If oParentNode Is Nothing Then Throw New Exception("Document cannot be inserted after the specified node.")
'Keep going UP the tree until we find a Story, Cell or Shape
If TypeOf oParentNode Is Story OrElse TypeOf oParentNode Is Cell OrElse TypeOf oParentNode Is Drawing.Shape Then Exit Do
'Move up the node tree 1 level
oDestinationNode = oParentNode
oParentNode = oDestinationNode.ParentNode
Loop
'Get the Index of the destination node so we can use the Insert method
Dim iDestinationIndex As Integer = oDestinationNode.ParentNode.ChildNodes.IndexOf(oDestinationNode)
'Get the Document so we can easily Import Nodes
Dim oDestinationDoc As Document = oDestinationNode.Document
Dim oInsertedSection As Section
'Get the Style so we can easily assign
Dim oStyle As Style
If Not StyleName Is Nothing Then oStyle = oDestinationDoc.Styles(StyleName)
'Go through all the Sections of the document being inserted
For Each oSection As Section In oDocToInsert.Sections
'Import the section into the document
oInsertedSection = CType(oDestinationDoc.ImportNode(oSection, True, ImportFormatMode.KeepSourceFormatting), Section)
'Can't insert the section because all the section breaks will mess the document
'up. We must go through all the child nodes and insert them individually.
For Each oInsertedNode As Node In oInsertedSection.Body.ChildNodes
'Do not insert node if it is a last empty paragarph in the section.
If TypeOf oInsertedNode Is Paragraph AndAlso oInsertedNode Is oSection.Body.LastChild AndAlso oInsertedNode.ToTxt.Equals(String.Empty) Then Exit For
'Copy the node (and children)
Dim oNewNode As Node = oInsertedNode.Clone(True)
'Insert the Node and increment index counter
oParentNode.ChildNodes.Insert(iDestinationIndex, oNewNode)
iDestinationIndex += 1
'Assign the Style if it is a paragraph node
If Not oStyle Is Nothing AndAlso oStyle.Type = StyleType.Paragraph AndAlso oNewNode.NodeType = NodeType.Paragraph Then CType(oNewNode, Paragraph).ParagraphFormat.Style = oStyle
Next
Next
End Sub