Free Support Forum - aspose.com

How to remove a row of any given text

Hi,

I am trying to remove a row in a table that contains a zero value. My template is created in a way that it will produce a special string, in this case, "|8|" when hitting a field with $0.00 This is intended so that the document can remove the row where the cell contains such special character by using the below code so the users do not see that line at all.

' Search |8| and delete the row for excessive empty or zero value row (Old code using MS OXPPIA COM object)

reOpenWord.Application.Selection.Find.ClearFormatting()

With reOpenWord.Application.Selection.Find

.Text = "|8|"

.Forward = True

.Wrap = WdFindWrap.wdFindContinue

.Format = False

.MatchCase = False

.MatchWholeWord = True

.MatchByte = False

.MatchWildcards = False

.MatchAllWordForms = False

End With

While reOpenWord.Application.Selection.Find.Execute()

reOpenWord.Application.Selection.Rows.Delete()

End While

'--------------

I have been looking all day and cannot find anything in Aspose.Word to do that. Have I overlooked something or is there any other way that I can perform the same task?

Please attach a sample document. Then I will provide a code sample which will do the described processing of the document.

Here is the sample files for my problem.

http://legaldev.fscportal.com/AsposeProblemSample.zip

The zip contains 3 files:

AsposeTableTemplate.doc - the template for the mail merge.

AsposeTableSampleDataSource.txt - a tab separated values file containing 1 record to merge.

AsposeTableSampleMergedDoc.doc - the result document of the merge.

Please help.

Normally, the code for it would be very simple:

ArrayList rowsToRemove = new ArrayList();

foreach (Row row in doc.GetChildNodes(NodeType.Row))

{

if (row.GetText().IndexOf("|8|") >= 0)

RowsToRemove.Add(row);

}

foreach (Row row in rowsToRemove)

{

row.Remove();

}

In you case the problem is that |8| mark is also occurring inside IF fields which are also included in row.GetText(). And IF field code text should not be accounted. So the code becomes slightly more complex:

ArrayList rowsToRemove = new ArrayList();

foreach (Row row in doc.GetChildNodes(NodeType.Row, true))

{

string rowText = row.GetText();

int markPos = rowText.IndexOf("|8|");

if (markPos >= 0)

{

string endText = rowText.Substring(markPos);

int fieldStartPos = endText.IndexOf(ControlChar.FieldStartChar);

int fieldSepPos = endText.IndexOf(ControlChar.FieldSeparatorChar);

if ((fieldSepPos == -1) || ((fieldStartPos >= 0) && (fieldStartPos < fieldSepPos)))

rowsToRemove.Add(row);

}

}

foreach (Row row in rowsToRemove)

{

row.Remove();

}

You can use the same approach if you need to highlight rows or cells containing specific data.

Best regards,