Hi,
this is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
prova.sostituisci("salva")
End Sub
inside prova there is
Public Function sostituisci(ByVal azione As String) As Boolean
Dim doc As New Document("C:\TemplateWord.doc")
'Dim doc As New Document("C:\FI1230.doc")
Dim builder As New DocumentBuilder(doc)
If (azione = "conta") Then
MsgBox("La parola cercata compare " & doc.Range.Replace(New Regex(Me.PTag1), Me.PTag1) & " volte")
Exit Function
End If
If Me.PTag1 <> "" Then
doc.Range.Replace(New Regex("#" & Me.PTag1 & "#"), Me.PTag1New)
End If
If Me.PTag2 <> "" Then
doc.Range.Replace(New Regex("#" & Me.PTag2 & "#"), New ReplaceEvaluatorFindAndInsertObject(Me.PTag2New, Me.PUrl, "link"), False)
End If
If Me.PTag3 <> "" Then
If (Me.POggetto.Substring(Me.POggetto.Length() - 3, 3) = "jpg") Then
doc.Range.Replace(New Regex("#" & Me.PTag3 & "#"), New ReplaceEvaluatorFindAndInsertObject(Me.POggetto, "image"), False)
ElseIf (Me.POggetto.Substring(Me.POggetto.Length() - 3, 3) = "doc") Then
Dim srcDoc As New Document("C:\output.doc")
doc.Range.Replace(New Regex("#" & Me.PTag3 & "#"), New ReplaceEvaluatorFindAndInsertObject(doc, srcDoc, "document"), False)
End If
End If
If (azione = "salva") Then
salva(doc, "C:")
ElseIf (azione = "converti") Then
converti(doc, "C:")
End If
End Function
Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
'This is a Run node that contains either the beginning or the complete match.
Dim currentNode As Node = e.MatchNode
'The first (and may be the only) run can contain text before the match,
'in this case it is necessary to split the run.
If (e.MatchOffset > 0) Then
currentNode = SplitRun(CType(currentNode, Run), e.MatchOffset)
End If
'This array is used to store all nodes of the match for further removing.
Dim runs As New ArrayList()
'Find all runs that contain parts of the match string.
Dim remainingLength As Integer = e.Match.Value.Length
While ((remainingLength > 0) AndAlso (currentNode IsNot Nothing) AndAlso (currentNode.GetText().Length <= remainingLength))
runs.Add(currentNode)
remainingLength = remainingLength - currentNode.GetText().Length
'Select the next Run node.
'Have to loop because there could be other nodes such as BookmarkStart etc.
Do While ((currentNode IsNot Nothing) AndAlso (currentNode.NodeType <> NodeType.Run))
currentNode = currentNode.NextSibling
Loop
End While
'Split the last run that contains the match if there is any text left.
If ((currentNode IsNot Nothing) AndAlso (remainingLength > 0)) Then
SplitRun(CType(currentNode, Run), remainingLength)
runs.Add(currentNode)
End If
'Create Document Buidler aond insert the object
Dim builder As New DocumentBuilder(e.MatchNode.Document)
builder.MoveTo(CType(runs(runs.Count - 1), Run))
If mType = "link" Then 'if the object is an Hyperlink
'builder.MoveTo(CType(runs(runs.Count - 1), Run))
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink
If (mUrl.Substring(0, 4) <> "http") Then
builder.InsertHyperlink(mText, "http://" & mUrl, False)
Else
builder.InsertHyperlink(mText, mUrl, False)
End If
ElseIf mType = "image" Then 'if the object is an image
builder.InsertImage(mPath)
ElseIf mType = "document" Then 'if the object is a document to append
'Dim bookmark As Bookmark = mDoc.Range.Bookmarks("bookmark") '"insertionPlace"
builder.StartBookmark("bookmark")
builder.EndBookmark("bookmark")
InsertDocumentAtBookamrk("bookmark", mDoc, mSrcDoc)
End If
'Now remove all runs in the sequence.
For Each run As Run In runs
run.Remove()
Next
'Signal to the replace engine to do nothing because we have already done all what we wanted.
Return ReplaceAction.Skip
End Function
Private Shared Function SplitRun(ByVal run As Run, ByVal position As Integer) As Run
Dim afterRun As Run = CType(run.Clone(True), Run)
afterRun.Text = run.Text.Substring(position)
run.Text = run.Text.Substring(0, position)
run.ParentNode.InsertAfter(afterRun, run)
Return afterRun
End Function
Sub InsertDocumentAtBookamrk(ByVal bookmarkName As String, ByVal dstDoc As Document, ByVal srcDoc As Document)
'Create DocumentBuilder
Dim builder As DocumentBuilder = New DocumentBuilder(dstDoc)
'Move cursor to bookmark and insert paragraph break
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 Aspose.Words.Paragraph = CType(insertAfterNode, Aspose.Words.Paragraph)
'Content of last paragraph of srcDoc will be apended to this parafraph
Dim insertBeforeParagraph As Aspose.Words.Paragraph = builder.CurrentParagraph
'We will be inserting into the parent of the destination paragraph.
Dim dstStory As CompositeNode = insertAfterNode.ParentNode
'Remove empty paragraphs from the end of document
While (Not srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
srcDoc.LastSection.Body.LastParagraph.Remove()
End While
'Loop through all sections in the source document.
Dim srcSection As Aspose.Words.Section
For Each srcSection In srcDoc.Sections
'Loop through all block level nodes (paragraphs and tables) in the body of the section.
Dim srcNode As Node
For Each srcNode In srcSection.Body
'Do not insert node if it is a last empty paragarph in the section.
Try
Dim para As Aspose.Words.Paragraph = CType(srcNode, Aspose.Words.Paragraph)
If (Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes) Then
Exit For
End If
'If current paragraph is first paragraph of srcDoc
'then appent its content to insertAfterParagraph
If (para.Equals(srcDoc.FirstSection.Body.FirstParagraph)) Then
Dim node As Node
For Each node In para.ChildNodes
Dim dstNode As Node = dstDoc.ImportNode(node, True, ImportFormatMode.KeepSourceFormatting)
insertAfterParagraph.AppendChild(dstNode)
Next
'If subdocument contains only one paragraph
'then copy content of insertBeforeParagraph to insertAfterParagraph
'and remove insertBeforeParagraph
If (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastParagraph)) 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.Equals(srcDoc.LastSection.Body.LastParagraph)) Then
Dim previouseNode As Node
Dim node As Node
For Each node In para.ChildNodes
Dim dstNode As Node = dstDoc.ImportNode(node, True, ImportFormatMode.KeepSourceFormatting)
If (previouseNode Is Nothing) Then
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild)
Else
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 = dstDoc.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
'Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
End If
Catch e As Exception
MsgBox(e.Message)
End Try
Next
Next
End Sub
If i forgot anything you need I’ll add here