none
How to remove empty row from tables in Word 2007 with C#?

    Question

  • I am working on an Add-In for Word 2007 using Visual Studio 2008 and C#. I get some information from another source, create a new Word document, and paste this information in the newly created document. Now I want to format the pasted conted and namely to remove/delete empty rows from tables, that were pasted.

    I was looking for a type like DataRow for Excel, but found nothing. Any suggestions how to get the rows from the tables and check wheater they are empty or not?

    Any help would be appreciated.

    Thanks

    Saturday, May 08, 2010 3:20 PM

Answers

  • In VBA, you would use to remove empty rows from the first table in the active document:
     
    Dim del As Boolean
    Dim i As Long, j As Long
    With ActiveDocument.Tables(1)
        For i = .Rows.Count To 1 Step -1
            del = False
            With ..Rows(i)
                For j = 1 To ..Cells.Count
                    If Len(.Cells(j).Range.Text) = 2 Then
                        del = True
                    Else
                        del = False
                        Exit For
                    End If
                Next j
                If del = True Then
                    ..Delete
                End If
            End With
        Next i
    End With

    --
    Hope this helps.
     
    Please reply to the newsgroup unless you wish to avail yourself of my
    services on a paid consulting basis.
     
    Doug Robbins - Word MVP, Posted via the NNTP Bridge

    I am working on an Add-In for Word 2007 using Visual Studio 2008 and C#. I get some information from another source, create a new Word document, and paste this information in the newly created document. Now I want to format the pasted conted and namely to remove/delete empty rows from tables, that were pasted.

    I was looking for a type like DataRow for Excel, but found nothing. Any suggestions how to get the rows from the tables and check wheater they are empty or not?

    Any help would be appreciated.

    Thanks


    Doug Robbins - Word MVP
    • Marked as answer by keksy Sunday, May 09, 2010 8:56 PM
    Saturday, May 08, 2010 10:09 PM

All replies

  • In VBA, you would use to remove empty rows from the first table in the active document:
     
    Dim del As Boolean
    Dim i As Long, j As Long
    With ActiveDocument.Tables(1)
        For i = .Rows.Count To 1 Step -1
            del = False
            With ..Rows(i)
                For j = 1 To ..Cells.Count
                    If Len(.Cells(j).Range.Text) = 2 Then
                        del = True
                    Else
                        del = False
                        Exit For
                    End If
                Next j
                If del = True Then
                    ..Delete
                End If
            End With
        Next i
    End With

    --
    Hope this helps.
     
    Please reply to the newsgroup unless you wish to avail yourself of my
    services on a paid consulting basis.
     
    Doug Robbins - Word MVP, Posted via the NNTP Bridge

    I am working on an Add-In for Word 2007 using Visual Studio 2008 and C#. I get some information from another source, create a new Word document, and paste this information in the newly created document. Now I want to format the pasted conted and namely to remove/delete empty rows from tables, that were pasted.

    I was looking for a type like DataRow for Excel, but found nothing. Any suggestions how to get the rows from the tables and check wheater they are empty or not?

    Any help would be appreciated.

    Thanks


    Doug Robbins - Word MVP
    • Marked as answer by keksy Sunday, May 09, 2010 8:56 PM
    Saturday, May 08, 2010 10:09 PM
  • Unfortunately I can't get the cells within a row in C# as in your example. There is nothing like Application.ActiveDocument.Rows[j].Cells

    Any suggestions how to iterate through the cells of a row in a Word table?

    Thanks

    Sunday, May 09, 2010 5:01 PM
  • My mistake - I can iterate through the cells within a row. I'll paste my code here tomorrow.

    Thanks

    Sunday, May 09, 2010 8:53 PM
  • Here is my code. In my case I have only one cell in a row, so I don't have to navigate through the cells. But this navigation should look like:

     

    for (j = 1; j <= table1.Rows[k].Cells.Count; j++)

     

    string rowText;
    int i, k;
    
    
    for (i = 1; i <= this.Application.ActiveDocument.Tables.Count; i++){
    	Word.Table table1 = this.Application.ActiveDocument.Tables[i];
    
    	for (k = 1; k <= table1.Rows.Count; k++)
       	  {
           	//Save the text from a row in rowText
             rowText = table1.Rows[k].Range.Text;
             try
               {
                 //An empty row contains only \r and \a
                 rowText = rowText.Replace('\r', ' ').Replace('\a', ' ').Trim();
               }
             catch { }
    
             if (String.IsNullOrEmpty(rowText))
               {
                 table1.Rows[k].Delete();
                 k--;
               }
           }
    }

     

    Thanks for your help, Doug!

     

    Monday, May 10, 2010 8:37 AM
  • You're welcome.  Sorry I can't help with the C# side of things; I'm strictly a VBA only proponent.

    --
    Hope this helps.
     
    Please reply to the newsgroup unless you wish to avail yourself of my
    services on a paid consulting basis.
     
    Doug Robbins - Word MVP, Posted via the NNTP Bridge

    Here is my code. In my case I have only one cell in a row, so I don't have to navigate through the cells. But this navigation should look like:

     

    for (j = 1; j <= table1.Rows[k].Cells.Count; j++)

     

    string rowText;
    int i, k;
    
    
    for (i = 1; i <= this.Application.ActiveDocument.Tables.Count; i++){
    	Word.Table table1 = this.Application.ActiveDocument.Tables[i];
    
    	for (k = 1; k <= table1.Rows.Count; k++)
      	 {
        	//Save the text from a row in rowText
         rowText = table1.Rows[k].Range.Text;
         try
          {
           //An empty row contains only \r and \a
           rowText = rowText.Replace('\r', ' ').Replace('\a', ' ').Trim();
          }
         catch { }
    
         if (String.IsNullOrEmpty(rowText))
          {
           table1.Rows[k].Delete();
           k--;
          }
        }
    }

     

    Thanks for your help, Doug!

     


    Doug Robbins - Word MVP
    Monday, May 10, 2010 9:16 AM