Hi i have search on the forum and found the function :
InsertDocumentAtBookamrk(ByVal bookmarkName As String, ByVal dstDoc As Document, ByVal srcDoc As Document)
So i have tried this :
Dim croquis_doc As Document = New Document("c:\test.doc")
InsertDocumentAtBookamrk("My_bookmark", croquis_doc, doc)
where “croquis_doc” is the document i want to insert inside “doc”.
But i have an exception : "Impossible to make a cast from an "aspose.words.tables’ object to “aspose.words.paragraph”
Thank you.
Max
Hi
Thanks for your inquiry. I think you experienced the same problem as described in the following thread:
https://forum.aspose.com/t/100342
There you can find updated code of InsertDocuemntAtBookmark method. Code is in C#, please let me know if you need help with translating it to VB, I will be glad to help you.
Best regards.
Hi i have tried to translate your c# code in vb via an online translator but there is 1 line with errors if you can please take a look thank you for your support.
The line is :
Public Sub InsertDocumentAtBookamrk(ByVal bookmarkName As String, ByVal MainDoc As Document, ByVal MargedDoc As Document)
'Create DocumentBuilder
Dim builder As DocumentBuilder = New DocumentBuilder(MainDoc)
'Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName, False, True)
'builder.MoveToBookmark(bookmarkName);
builder.Writeln()
'Content of srcdoc will be inserted after this node
Dim insertAfterNode As Node = builder.CurrentParagraph.PreviousSibling
'Content of first paragraph of srcDoc will be apended to this parafraph
Dim insertAfterParagraph As Paragraph = CType(insertAfterNode, Paragraph)
'Content of last paragraph of srcDoc will be apended to this parafraph
Dim insertBeforeParagraph As Paragraph = builder.CurrentParagraph
'We will be inserting into the parent of the destination paragraph.
Dim dstStory As CompositeNode = insertAfterNode.ParentNode
'Get list if current paragraph is list item
Dim list As List = Nothing
Dim listFontColor As Color = Color.Black
If insertAfterParagraph.IsListItem Then
list = insertAfterParagraph.ListFormat.List
listFontColor = insertAfterParagraph.ListFormat.ListLevel.Font.Color
End If
'Loop through all sections in the source document.
For Each srcSection As Section In MargedDoc.Sections
'Loop through all block level nodes (paragraphs and tables) in the body of the section.
For Each srcNode As Node In srcSection.Body
'Do not insert node if it is a last empty paragarph in the section.
Dim para As Paragraph = CType(srcNode, Paragraph)
'If current paragraph is first paragraph of srcDoc
'then appent its content to insertAfterParagraph
If ((Not (para) Is Nothing) AndAlso para.Equals(MargedDoc.FirstSection.Body.FirstParagraph)) Then
Dim dstParagraph As Paragraph = CType(MainDoc.ImportNode(para, True, ImportFormatMode.KeepSourceFormatting), Paragraph)
While dstParagraph.HasChildNodes
'Node dstNode = MainDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
insertAfterParagraph.AppendChild(dstParagraph.FirstChild)
End While
If (Not insertAfterParagraph.IsListItem AndAlso dstParagraph.IsListItem) Then
If (list Is Nothing) Then
list = dstParagraph.ListFormat.List
End If
insertAfterParagraph.ListFormat.List = list
listFontColor = para.ListFormat.ListLevel.Font.Color
End If
'If subdocument contains only one paragraph
'then copy content of insertBeforeParagraph to insertAfterParagraph
'and remove insertBeforeParagraph
If MargedDoc.FirstSection.Body.FirstParagraph.Equals(MargedDoc.LastSection.Body.LastParagraph) Then
While insertBeforeParagraph.HasChildNodes
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild)
End While
insertBeforeParagraph.Remove()
End If
End If
'If current paragraph is last paragraph of srcDoc
'then appent its content to insertBeforeParagraph
If ((Not (para) Is Nothing) AndAlso para.Equals(MargedDoc.LastSection.Body.LastParagraph)) Then
Dim tofix As Node
Dim previouseNode As Node = Nothing
For Each node As Node In para.ChildNodes
Dim dstNode As Node = MainDoc.ImportNode(node, True, ImportFormatMode.KeepSourceFormatting)
If (previouseNode Is Nothing) Then
'insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
tofix = insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)
Else
'insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
tofix = insertBeforeParagraph.InsertAfter(dstNode, previouseNode)
End If
previouseNode = dstNode
' If last paragraph to be inserted is not numbering, then remove Numbering
If (Not para.IsListItem AndAlso CType(dstNode.ParentNode, Paragraph).IsListItem) Then
CType(tofix.ParentNode, Paragraph).ListFormat.RemoveNumbers()
End If
' Apply numbering according to the collected List
insertBeforeParagraph.ListFormat.List = list
Next
' insertBeforeParagraph.ListFormat.List = list;
Else
'This creates a clone of the node, suitable for insertion into the destination document.
Dim newNode As Node = MainDoc.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
'Set list if it is needed
If (newNode.NodeType = NodeType.Paragraph) Then
If (CType(newNode, Paragraph).IsListItem AndAlso (Not (list) Is Nothing)) Then
CType(newNode, Paragraph).ListFormat.List = list
End If
End If
'Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
End If
Next
Next
'Clear formating of list
If (Not (list) Is Nothing) Then
For Each level As ListLevel In list.ListLevels
level.Font.Color = listFontColor
level.Font.Bold = False
level.NumberPosition = 0
Next
End If
End Sub
Hi
Thanks for your request. Please try using this code:
'''
''' Inserts the document at bookamrk.
'''
''' Name of the bookmark.
''' The main doc.
''' The marged doc.
Public Sub InsertDocumentAtBookamrk(ByVal bookmarkName As String, ByVal MainDoc As Document, ByVal MargedDoc As Document)
' Create DocumentBuilder
Dim builder As DocumentBuilder = New DocumentBuilder(MainDoc)
' Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName, False, True)
builder.Writeln()
' Content of srcdoc will be inserted after this node
Dim insertAfterNode As Node = builder.CurrentParagraph.PreviousSibling
' Content of first paragraph of srcDoc will be apended to this parafraph
Dim insertAfterParagraph As Paragraph = CType(insertAfterNode, Paragraph)
' Content of last paragraph of srcDoc will be apended to this parafraph
Dim insertBeforeParagraph As Paragraph = builder.CurrentParagraph
' We will be inserting into the parent of the destination paragraph.
Dim dstStory As CompositeNode = insertAfterNode.ParentNode
Dim importer As NodeImporter = New NodeImporter(MargedDoc, MainDoc, ImportFormatMode.KeepSourceFormatting)
' Loop through all sections in the source document.
For Each srcSection As Section In MargedDoc.Sections
' Loop through all block level nodes (paragraphs and tables) in the body of the section.
For Each srcNode As Node In srcSection.Body
' Do not insert node if it is a last empty paragarph in the section.
Dim para As Paragraph = TryCast(srcNode, Paragraph)
' If current paragraph is first paragraph of srcDoc
' then appent its content to insertAfterParagraph
If (para IsNot Nothing AndAlso (para.Equals(MargedDoc.FirstSection.Body.FirstChild))) Then
Dim dstParagraph As Paragraph = CType(importer.ImportNode(para, True), Paragraph)
While (dstParagraph.HasChildNodes)
insertAfterParagraph.AppendChild(dstParagraph.FirstChild)
End While
' If subdocument contains only one paragraph
' then copy content of insertBeforeParagraph to insertAfterParagraph
' and remove insertBeforeParagraph
If (MargedDoc.FirstSection.Body.FirstParagraph.Equals(MargedDoc.LastSection.Body.FirstChild)) Then
While ((insertBeforeParagraph.HasChildNodes))
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild)
End While
insertBeforeParagraph.Remove()
End If
'If current paragraph is last paragraph of srcDoc
'then appent its content to insertBeforeParagraph
ElseIf (para IsNot Nothing AndAlso (para.Equals(MargedDoc.LastSection.Body.LastChild))) Then
Dim tofix As Node
Dim previouseNode As Node = Nothing
For Each node As Node In para.ChildNodes
Dim dstNode As Node = importer.ImportNode(node, True)
If ((previouseNode Is Nothing)) Then
tofix = insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)
Else
tofix = insertBeforeParagraph.InsertAfter(dstNode, previouseNode)
End If
previouseNode = dstNode
Next
Else
' This creates a clone of the node, suitable for insertion into the destination document.
Dim newNode As Node = importer.ImportNode(srcNode, True)
' Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
End If
Next
Next
End Sub
Hope this helps. Please let me know in case of any issues, I will be glad to help you.
Best regards.
Seems working fine thank you very much
Max