PDA

View Full Version : Auto Delete Nested Tables rows in word



JStew
09-08-2016, 11:33 AM
To keep my items positioned and flow correctly ive had to have a main table with two columns and have two nested tables inside with 2 columns table on the left and 3 column table on the right.
im populating the data using mail merge and if the fields are empty i dont want them to show up. Is it possible to use VBA to auto delete nested Tables when empty. i can do it they are not nested but when they are nested.
it just over looks those tables and rows.


__________________________________________
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
| |------|------| | |------|------|------| |
--------------------------------------------------------

I

gmaxey
09-08-2016, 12:27 PM
Something like this:


Sub ScratchMacro()
Dim oTbl As Word.Table
For Each oTbl In ActiveDocument.Tables
RecursiveLoop oTbl
Next oTbl
lbl_Exit:
Exit Sub
End Sub
Function RecursiveLoop(ByRef oTblMajor As Word.Table)
Dim oCellNested As Word.Cell
Dim oTblMinor As Word.Table
For Each oCellNested In oTblMajor.Range.Cells
If oCellNested.Tables.Count > 0 Then
For Each oTblMinor In oCellNested.Tables
ProcessTable oTblMinor
Next oTblMinor
End If
Next oCellNested
lbl_Exit:
Exit Sub
End Function
Sub ProcessTable(oTblNested As Table)
If Len(oTblNested.Range.Text) = oTblNested.Range.Cells.Count * 2 + (oTblNested.Rows.Count * 2) Then
oTblNested.Delete
End If
lbl_Exit:
Exit Sub
End Sub

JStew
09-08-2016, 01:01 PM
Im a little novice when using VBA, but when i run this bit of code nothing happens. it at first gave me and error for exit sub inside the end function. i removed this but nothing happened. No nested rows were deleted.

gmaxey
09-08-2016, 01:13 PM
It does what you asked here:

JStew
09-08-2016, 02:11 PM
Sorry, if i did word my question correctly. What im looking for:

1705817059

What Im looking for is only deleting an empty row of a nested table not the whole table. Only delete the row if the entire nested table row is empty.

gmaxey
09-08-2016, 03:01 PM
Sub ProcessTable(oTblNested As Table)
Dim lngIndex As Long
If Len(oTblNested.Range.Text) = oTblNested.Range.Cells.Count * 2 + (oTblNested.Rows.Count * 2) Then
oTblNested.Delete
Else
For lngIndex = oTblNested.Rows.Count To 1 Step -1
If Len(oTblNested.Rows(lngIndex).Range.Text) = (oTblNested.Rows(lngIndex).Cells.Count * 2) + 2 Then
oTblNested.Rows(lngIndex).Delete
End If
Next lngIndex
End If
lbl_Exit:
Exit Sub
End Sub

JStew
09-09-2016, 10:52 AM
I want to thank you again for your help in this. Its just my novice speaking. I ran into a couple of different issues:

Item one: (macro no showing)

when i copied and passed the code into words VBA editor. The macro doesn't show up as an available macro. Until i remove "oTblNested As Table" from parenthesis
Sub ProcessTable(oTblNested As Table)
? - Is that right? Should i have to remove "oTblNested As Table" from the sub to be able to run the code?

Second Item: (Change the code slightly)
So i changed the code to include the variable oTblNested As Table and removed it from the Sub ProcessTable() . See altered code below:


Sub ProcessTable()
Dim oTblNested As Table
Dim lngIndex As Long
If Len(oTblNested.Range.Text) = oTblNested.Range.Cells.Count * 2 + (oTblNested.Rows.Count * 2) Then
oTblNested.Delete
Else
For lngIndex = oTblNested.Rows.Count To 1 Step -1
If Len(oTblNested.Rows(lngIndex).Range.Text) = (oTblNested.Rows(lngIndex).Cells.Count * 2) + 2 Then
oTblNested.Rows(lngIndex).Delete
End If
Next lngIndex
End If
lbl_Exit:
Exit Sub End Sub

Results:
After doing this the macro becomes available to run. i run the macro, but it fails on line 4
If Len(oTblNested.Range.Text) = oTblNested.Range.Cells.Count * 2 + (oTblNested.Rows.Count * 2) Then

Error Message:
Run-time error '91':
Object variable or With Block variable not set
17061

Thoughts?

gmaxey
09-10-2016, 01:03 AM
As before,

Sub ProcessTable(oTblNested As Table)
Dim lngIndex As Long
If Len(oTblNested.Range.Text) = oTblNested.Range.Cells.Count * 2 + (oTblNested.Rows.Count * 2) Then
oTblNested.Delete
Else
For lngIndex = oTblNested.Rows.Count To 1 Step -1
If Len(oTblNested.Rows(lngIndex).Range.Text) = (oTblNested.Rows(lngIndex).Cells.Count * 2) + 2 Then
oTblNested.Rows(lngIndex).Delete
End If
Next lngIndex
End If
lbl_Exit:
Exit Sub
End Sub

is a procedure that is called by Function RecursiveLoop(ByRef oTblMajor As Word.Table)

You still nee all three procedures and you start by running Sub ScratchMacro()

JStew
09-12-2016, 09:18 AM
Would you be willing to provide training help on this? Im willing to pay a fee if needed. i just cant seem to wrap my head around this and get this to work. Thanks.

gmaxey
09-12-2016, 12:30 PM
Send me website feedback. I don't want to publish private consulting rates in this forum.