Thread: Table Spitting
View Single Post
 
Old 09-05-2023, 12:46 PM
ranjan ranjan is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default Table Spitting

Need Word VBA code:
In a word document tables contains only text and blank cells in the last row of the table and to the immediate table of second row having only text and blank cells then it should be tagged ## at the begining of that immediate table

Document only contains table, i need to check all tables in a document.

I tried the below code, but it is not working could you please anyone help me...

For reference PFA.

Code:
Sub Spliting()
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim wdTable As Object
    
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("path_to_word_document.docx")
    
    For Each wdTable In wdDoc.Tables
        If wdTable.Rows.Count > 1 Then
            If IsTableTextAndEmptyLastRow(wdTable) And IsTableTextAndEmptyLastRow(wdTable.Next)) Then
                wdDoc.Content.InsertAfter "##"
            End If
        End If
    Next wdTable
    
    wdDoc.Close savechanges:=True
    wdApp.Quit
    
    Set wdTable = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub

Function IsTableTextAndEmptyLastRow(table As Object) As Boolean
    Dim lastRow As Object
    Dim cell As Object
    
    Set lastRow = table.Rows(table.Rows.Count)
    
    For Each cell In lastRow.Cells
        If cell.Range.Text <> " " Then
            IsTableTextAndEmptyLastRow = False
            Exit Function
        End If
    Next cell
    
    IsTableTextAndEmptyLastRow = True
End Function
Attached Files
File Type: docx TEST.docx (64.0 KB, 7 views)
Reply With Quote