none
Replace empty cells with Hypen in all tables in a given document by using VBA RRS feed

  • Question

  • Hi,

    I have a document which contains nested tables (It can be inside a cell and can be very deeply nested) and non nested tables inside it. I want to find empty table cells in the document and than replace these empty cells with Hypen (-)  and then change the font color of the hypen to white color. I want to do this for all the tables including nested tables and  non nested as well.

    Regards

    Rajaniesh

    Friday, June 9, 2017 8:39 PM

All replies

  • Hi,

    I'd like to know the further detail:
    1. How many tables in a document? only one or more than one?
    2. How many levels nested in each table?
        If more than one table exists in a document, please provide the number of levels nested in each table.

    ____________
    Ashidacchi
    Saturday, June 10, 2017 1:07 AM
  • Hi Rajaniesh,

    You could use Range.Tables to confirm if the a cell contains nested tables. I suggest you use recursive method so we could access very deeply nested tables. Here is the example.

    Sub RecursionAllTable()
    Call RecursionTB(ThisDocument.Tables)
    End Sub
     
    Function RecursionTB(tbs As Tables)
    For Each tb In tbs
    For Each cel In tb.Range.Cells
    If cel.Tables.Count > 0 Then
    Call RecursionTB(tb.Tables)
    ElseIf cel.Range.Text = Chr(13) & Chr(7) Then
      cel.Range.Font.ColorIndex = wdDarkRed
      cel.Range.Text = "-"
    End If
    Next cel
    Next tb
    End Function

    Best Regards,

    Terry

    Monday, June 12, 2017 5:03 AM
  • Hi Rajaniesh Kaushikk,

    I have not heard from you for a several days.  Has your question been answered?
    (I wanted to make code, if I could understand specification of nested tables.)

    Ashidacchi

    Tuesday, June 13, 2017 12:27 AM
  • Hi,

    Actually there can be any number of nested tables. Some tables are inside tables and some are outside

    Regards
    Rajaniesh

    Tuesday, June 13, 2017 1:48 PM
  • This recursive version is hanging the system. It is not efficient. Moreover it is not changing the font to white so that we can hide it.
    Tuesday, June 13, 2017 4:44 PM
  • Hi Rajaniesh,

    I have updated my code. I divided my code into two part. First part is getting all tables(including nested tables) via recursion and second part is iterating these tables and cells. For just getting tables, recursion is efficient. In my test, I have created 100 tables with 4*4 cells, recursion spent less then 1s to get all tables but the second part spent about 6s. If you have too many tables and cells for handling, word will be busy to handle these cells which may cause word no response. So how many tables in your document? Besides, for changing the font to white, you could change wdDardRed to wdWhite.

    Here is my code for testing.

    Sub RecursionAllTable()
    Dim tbArr() As Table
    ReDim Preserve tbArr(0) As Table
    StartTime = Now
    Call RecursionTB(ThisDocument.Tables, tbArr())
    Debug.Print "Total tables count:" & UBound(tbArr)
    MidleTime = Now
    Debug.Print "Recursion Tables spent time:" & DateDiff("s", StartTime, MidleTime)
    Application.ScreenUpdating = False
    For i = 1 To UBound(tbArr)
    For Each cel In tbArr(i).Range.Cells
    If cel.Range.Text = Chr(13) & Chr(7) Then
      cel.Range.Font.ColorIndex = wdWhite
      cel.Range.Text = "-"
    End If
    Next cel
    Next i
    Application.ScreenUpdating = True
    EndTime = Now
    Debug.Print "Handling cells spent time:" & DateDiff("s", StartTime, EndTime)
    End Sub
     
    Sub RecursionTB(tbs As Tables, tbArr() As Table)
    For Each tb In tbs
    Count = UBound(tbArr)
    ReDim Preserve tbArr(Count + 1) As Table
    Set tbArr(Count + 1) = tb
    If tb.Tables.Count > 0 Then
    Call RecursionTB(tb.Tables, tbArr())
    End If
    Next tb
    End Sub

    Best Regards,

    Terry

    Friday, June 16, 2017 1:59 AM