"Cannot remove Because there is no parent" exception after deleting empty rows from table using VB.NET

Hi,
I have a word document (.doc) that has lot of bookmarks on it. On the first half of the document (Pls see attached - the line below ‘Advance Account PY Regular Employer’) if there is no value in the bookmark, I need to delete the empty rows. Same thing with the rows below ‘#0097 TRSL PY Regular Employer’.
This is the code I am using to try to delete the rows.

Dim bAllDeleted As Boolean = False
While bAllDeleted = False
bAllDeleted = True
If tables(0).Rows.Count <> 0 Then
For Each Row As Tables.Row In tables(0).Rows
While String.IsNullOrEmpty(Row.ToTxt.Trim())
Row.Remove()
bAllDeleted = False
End While
Next
End If
End While
doc.Save("C:\Rads\JournalEntryReviewProcess\" + "JETemplate27.doc", SaveFormat.Doc)

But after deleting one blank row, I get an error: “Cannot remove Because there is no parent”. What do I need to add to fix the issue, and also delete ALL the blank rows in the table (just the table 1).

Hi,

Please accept my apology for late response. For your scenario, Please see the code bellow. Hope this helps you. Let us know, If you have any more queries.

Dim doc As New Document("D:\\JETemplate.doc")
doc.UpdateFields()
Dim tables As Node() = doc.GetChildNodes(NodeType.Table, True).ToArray()
Dim table As Table = DirectCast(tables(0), Table)
Dim rowToRemove As New ArrayList()
For Each row As Row In table.Rows
Dim emptyrow As [Boolean] = True
For Each cell As Cell In row
If cell.Range.Text.Trim().IndexOf("FORMTEXT") = -1 Then
emptyrow = False
Exit For
End If
Next
If emptyrow = True Then
rowToRemove.Add(row)
End If
Next
For Each row As Row In rowToRemove
If row IsNot Nothing Then
row.Remove()
End If
Next
doc.Save("D:\\Asposeout.doc")

Hi,
I tried your code but no empty rows were deleted. below is the exact code that I am using, but modified little bit for my VB.NET environment: I ran the code but no empty rows were deleted. Please help, this is a very time senstive issue.

doc.UpdateFields()
Dim tables As Node() = doc.GetChildNodes(NodeType.Table, True).ToArray()
Dim table As Tables.Table = DirectCast(tables(0), Tables.Table)
Dim rowToRemove As New ArrayList()
For Each row As Tables.Row In table.Rows
Dim emptyrow As [Boolean] = True
For Each cell As Tables.Cell In row
If cell.Range.Text.Trim().IndexOf("FORMTEXT") = -1 Then
emptyrow = False
Exit For
End If
Next
If emptyrow = True Then
rowToRemove.Add(row)
End If
Next
For Each row As Tables.Row In rowToRemove
If row IsNot Nothing Then
row.Remove()
End If
Next
doc.Save("C:\Rads\JournalEntryReviewProcess\" + "Asposeout.doc")

Hi

Thanks for your inquiry. First of all, please note that DocumentExplorer is a very useful tool which easily enables us to see the entire document structure. You can find DocumentExplorer in the folder where you installed Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe. Below is the DOM structure of your document as viewed with DocumentExplorer:

Document explorer does not recognize the table in your provided Word document as shown in attached screen shot.

To delete empty rows, please follow up the code snippet.

Dim doc As New Document("c:/temp/JETemplateTest.docx")
doc.UpdateFields()
Dim tables As Node() = doc.GetChildNodes(NodeType.Table, True).ToArray()
Dim table As Table = DirectCast(tables(0), Table)
Dim rowToRemove As New ArrayList()
For Each row As Row In table.Rows
Dim emptyrow As [Boolean] = True
For Each cell As Cell In row
If cell.ToTxt.Trim() <> "" Then
emptyrow = False
'Exit For
End If
Next
If emptyrow = True Then
rowToRemove.Add(row)
End If
Next
For Each row As Row In rowToRemove
If row IsNot Nothing Then
row.Remove()
End If
Next
doc.Save("D:\\Asposeout.doc")

I have also attached input / output document files. In case of any ambiguity, please let me know.

Imran,
I tried your code and it worked great. It removed all the empty rows in the table. Thanks very much. I really appreciate your help in this matter.
Thanks again.

Hi

Thank you very much for your feedback. It is great to hear that your issue has been resolved. You are always welcome and please feel free to ask if you have any query in future.
We always welcome constructive response from our customers.

I have one ore question in follow up to deleting the rows in the table dynamically.
Please see the attached document. In the process of deleting the empty rows, the line is missing from the table (because of the style sheet options). This is shown by the red arrow in the document.
Is there a way in Aspose to draw a line through code?
Thanks.

Hi

Thank you for inquiry. could you please attach input Word document to reproduce this problem on my side.

Attached is the word document. Please help me resolve the issue. It is very time critical.
I want to insert a line so that it wouldnt look awkward - only when rows are deleted from the table.

Hi

Thank you for inquiry. First off, please try to use the latest version at your end and see if it resolves your issue. Moreover, i did not observe this problem on my end. I have attached output Word document.

I hope this will help.

Imran,
I have the problem on my end. Please see the first attachment (with a screenshot) I sent you yesterday. When the rows are deleted (using the code that you sent me few weeks back), the line is also being deleted.
In general, is there a way to insert a line through code in Aspose.Words?

I am wondering if someone is going to help me with the issue. If the question is not clear, then I can explain it you again. I need the Aspose code to insert a line below the last row of the table somehwere in the code below.

' Delete Blank Rows from the Debit and Credit descriptions
doc.UpdateFields()
Dim tables As Node() = doc.GetChildNodes(NodeType.Table, True).ToArray()
Dim table As Tables.Table = DirectCast(tables(0), Tables.Table)
Dim rowToRemove As New ArrayList()
For Each row As Tables.Row In table.Rows
Dim emptyrow As [Boolean] = True
For Each cell As Tables.Cell In row
If cell.ToTxt.Trim() <> "" Then
emptyrow = False
End If
Next
If emptyrow = True Then
rowToRemove.Add(row)
End If
Next
For Each row As Tables.Row In rowToRemove
If row IsNot Nothing Then
row.Remove()
End If
Next

Hi

Thank you for inquiry and sorry for delay. Could you please create a simple console application which reproduces the issue on your side and attach it here? We will take a closer look into what’s happening and provide you with some further feedback.

Moreover, I will suggest you to visit table border documentation here.