Hi,
I’m currently evaluating the Aspose WORD control to be used in our web application to generate documents from templates. I’m using the Range.Replace method to find tags in the form of and replace them with the actual data. This works fine. However, we also have the need to replace some tags with an Aspose.Word.Table object. I have not been able to find a straight forward way to do this, since for inserting a table at a certain place it is necessary to have a reference node. The only way I’ve been able to come up with is using the Range.Replace overload that accepts a delegate function, since via the MatchNode in de event args in that delegate I can get a reference to the necessary nodes to insert the table.
- Am I doing things the hard way while there’s an easier solution to this?
- It works, however, an exception is generated (Object reference not set to an instance of an object) if one table needs to be inserted twice or more. I’ve been debugging a while and found that the delegates is called 4 times if the template contains two instances of the same tag, 6 for three etc. While looking at the plain Document.Range.Text it is clearly visible that a second table is being inserted in one of the cells of an inserted table (MatchNode.Type=“Run” and MatchNode.Range.Text= the text that’s in the cell where a second instance of the table is being inserted. I suspect this problem occurs due to the fact that the document object layout changes because of the insert of a number of nodes that make up the table. I’ve also found a work around for the problem; just replacing one instance at a time, however I’m afraid this has some performance issues. Below is the code I’m using to do the replace (bold is the workaround). Am I doing something wrong here that triggers the exception?
Private Function WORDTableReplace(ByRef rastuVars() As structWORDVar, ByVal vobjDoc As Aspose.Word.Document) As Boolean
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'//Method: Globals.clsDocument.WORDTableReplace
'//
'//Date: 06/12/2005 / 8:59
'//Author: Rick Langevoort
'//Purpose:
'//
'//Rev:
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
**Dim intTable, intTables As Integer**
Dim stuVar As structWORDVar, objRange As Aspose.Word.Range = vobjDoc.Range
For Each stuVar In rastuVars
With stuVar
If StrComp(.Table, "tabel", CompareMethod.Text) = 0 Then
m_stuCurrentWORDVar = stuVar
**intTables = objRange.Replace(.VarCode, .VarCode, False, False)
m_blnMultipleTables = intTables > 1**
**For intTable = 0 To intTables - 1
m_blnReplaced = False**
objRange.Replace(New System.Text.RegularExpressions.Regex(Replace(.VarCode, ".", "\.") & "", RegularExpressions.RegexOptions.IgnoreCase), New Aspose.Word.ReplaceEvaluator(AddressOf WORDTableReplaceHandler), True)
**Next intTable**
End If
End With
Next stuVar
Return True
End Function
Public Function WORDTableReplaceHandler(ByVal sender As Object, ByVal e As Aspose.Word.ReplaceEvaluatorArgs) As Aspose.Word.ReplaceAction
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'//Method: Globals.clsDocument.WORDTableReplace
'//
'//Date: 06/12/2005 / 8:59
'//Author: Rick Langevoort
'//Purpose: Deze functie handeld de events af die worden gegenereerd door de replace functie voor de tabellen.
'//
'//Rev:
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
**If m_blnReplaced And m_blnMultipleTables Then Return Aspose.Word.ReplaceAction.Stop**
With m_stuCurrentWORDVar
**m_blnReplaced = True**
If StrComp(.Waarde, "-") <> 0 Then
Dim intRow, intCol As Integer
Dim objTable As New Aspose.Word.Table(e.MatchNode.Document), objRow As Aspose.Word.Row, objCell As Aspose.Word.Cell, objParent As Aspose.Word.Node = e.MatchNode, objSibbling As Aspose.Word.Node = e.MatchNode
Dim objDocBuilder As New Aspose.Word.DocumentBuilder(e.MatchNode.Document), dblPageWidth As Double = objDocBuilder.PageSetup.PageWidth - objDocBuilder.PageSetup.LeftMargin - objDocBuilder.PageSetup.RightMargin
Dim astrRows() As String = Split(.Waarde, vbCrLf), astrCols() As String
While objParent.NodeType <> Aspose.Word.NodeType.Body And objParent.NodeType <> Aspose.Word.NodeType.Cell
objSibbling = objParent
objParent = objParent.ParentNode
End While
With objTable
For intRow = 0 To UBound(astrRows)
astrCols = Split(astrRows(intRow), "|")
objRow = New Aspose.Word.Row(e.MatchNode.Document)
.Rows.Add(objRow)
With objRow
With .RowFormat
.HeadingFormat = intRow = 0 '//De eerste rij is de titelrij......
With .Borders
.LineStyle = Aspose.Word.LineStyle.Single
.Color = System.Drawing.Color.FromArgb(0, 0, 128)
End With
End With
For intCol = 0 To UBound(astrCols)
objCell = New Aspose.Word.Cell(e.MatchNode.Document)
.Cells.Add(objCell)
With objCell
.EnsureMinimum()
If astrCols(intCol).Length > 0 Then .FirstParagraph.Runs.Add(New Aspose.Word.Run(e.MatchNode.Document, astrCols(intCol)))
With .CellFormat
.LeftPadding = 5
.RightPadding = 5
.VerticalAlignment = Aspose.Word.CellVerticalAlignment.Center
.Width = dblPageWidth / (UBound(astrCols) + 1)
End With
If intRow = 0 Then
.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(0, 0, 128)
With .FirstParagraph.Runs(0).Font
.Bold = True
.Color = System.Drawing.Color.White
End With
End If
End With
Next intCol
End With
Next intRow
End With
CType(objParent, Aspose.Word.CompositeNode).InsertBefore(objTable, objSibbling)
e.MatchNode.Range.Delete()
Return Aspose.Word.ReplaceAction.Skip
Else
e.Replacement = "-"
Return Aspose.Word.ReplaceAction.Replace
End If
End With
End Function