Hi,
I have a function that it extract content between nodes. The problem is when in this part of text include a numbering because it lose the format.
This is method to extract content between nodes:
Shared Function ExtractContentBetweenNodes(ByRef startNode As Words.Node, ByRef endNode As Words.Node) As Words.Document
Dim sStr As String
Dim oDocSource, oDocDst As Words.Document
Dim firstSect, currNode, dstNode, sect As Words.Node
Dim bEnd As Boolean = False
Try
'Comprobar si los nodos de inicio y fin son hijos del body del documento
If startNode.ParentNode.NodeType <> Words.NodeType.Body Or endNode.ParentNode.NodeType <> Words.NodeType.Body Then
sStr = "Para extraer el contenido de un marcador el inicio y fin de los nodos debes de ser hijos del cuerpo del documento."
Throw New Exception(sStr)
End If
'Clonar el documento original.
'Esto es necesario para preservar los estilos del documento original
oDocSource = startNode.Document oDocDst = oDocSource.Clone
oDocDst.RemoveAllChildren()
'Ahora debemos de copiar los nodos padre del nodo inicial al documento destino
firstSect = oDocDst.ImportNode(startNode.GetAncestor(Words.NodeType.Section), True, Words.ImportFormatMode.KeepSourceFormatting)
oDocDst.AppendChild(firstSect)
'Borrar el contenido de la seccion, exceptuando cabeceras y pies de página
oDocDst.LastSection.Body.RemoveAllChildren()
'Copiar el contenido
currNode = startNode
While Not bEnd
'miramos si hemos llegado al fin del contenido a extraer
If currNode.Equals(endNode) Then bEnd = True
'importar el nodo
dstNode = oDocDst.ImportNode(currNode, True)
oDocDst.LastSection.Body.AppendChild(dstNode)
'mover al siguiente nodo
If currNode.NextSibling IsNot Nothing Then
currNode = currNode.NextSibling
Else
'mover a la siguiente sección
sect = currNode.GetAncestor(Words.NodeType.Section)
If sect.NextSibling IsNot Nothing Then
dstNode = oDocDst.ImportNode(sect.NextSibling, True, Words.ImportFormatMode.KeepSourceFormatting)
oDocDst.AppendChild(dstNode)
oDocDst.LastSection.Body.RemoveAllChildren()
currNode = CType(sect.NextSibling, Words.Section).Body.FirstChild
Else
Exit While
End If
End If
End While
Catch ex As Exception
Throw ex
Finally
oDocSource = Nothing
firstSect = Nothing
currNode = Nothing
dstNode = Nothing
sect = Nothing
End Try
'To test the result
oDocDst.Save("d:\test\out.odt")
Return oDocDst
End Function
I attach the documet with this problem.
Thanks for all.
Best regards,