Identifying a Table in Word

B

Bigfoot17

I have a Useform gathering info and placing it into FormFields in my
document. I have a table (2 col and 4 row) that holds eight of the many
formfields (txtCt1 through 8).

What I am trying to do do is delete rows if not needed. Here is the code
that I was tring to run but which does not work for me, followed by what I
thought the code would do.

j = 0
For k = 1 To 8
If TextBox & k > "" Then
j = j + 1
End If
Next k
If j = 1 Or j = 2 Then
ActiveDocument.Tables(2).Rows(4).Delete
ActiveDocument.Tables(2).Rows(3).Delete
ActiveDocument.Tables(2).Rows(2).Delete
ElseIf j = 3 Or j = 4 Then
'ActiveDocument.Tables(2).Rows(4).Delete
'ActiveDocument.Tables(2).Rows(3).Delete
Selection.Tables(2).Rows(4).Delete 'I saw this in another post
Selection.Tables(2).Rows(3).Delete 'I saw this in another post
ElseIf j = 5 Or j = 6 Then
ActiveDocument.Tables(2).Rows(4).Delete
End If

Check TextBoxes 1 through 8 (k) on the userform and count how many have
content.
If only the first two have content delete rows 2-4 off of the table.
If ony the first four have content delete rows 3 &4 of the table.

I have a couple of tables in the documetn so I am not sure how to identlfy
which table I am trying to delete the rows from, plus apparently I am not
deleting rows from any of my tables so I need help!

Thank you.
 
S

StevenM

Bigfoot,

Perhaps the following example code will help you out.

'
' DeleteEmptyRows
' Deletes every empty row in every table in the active document.
' I don't know if it is absolutely necessary to work from the
' bottom up, but it has been my experience that it is often so
' when deleting something, otherwise you throw off your count.
'
Sub DeleteEmptyRows()
Dim oDoc As Document
Dim oRange As Range
Dim oTable As Table
Dim oRow As Row
Dim nTable As Long
Dim nRow As Long
Dim nCol As Long
Dim Flag As Boolean

Set oDoc = ActiveDocument
Set oRange = oDoc.Range

For nTable = oDoc.Tables.Count To 1 Step -1
Set oTable = oRange.Tables(nTable)
For nRow = oTable.Rows.Count To 1 Step -1
Set oRow = oTable.Rows(nRow)
Flag = False
For nCol = 1 To oRow.Cells.Count
'Empty cells always have a length of 2
If Len(oRow.Cells(nCol).Range.Text) > 2 Then
Flag = True
Exit For
End If
Next nCol
If Flag = False Then
oRow.Delete
End If
Next nRow
Next nTable
End Sub

Steven Craig Miller
 
S

StevenM

To: Bigfoot,

' DeleteEmptyRows (Version 2)
' Deletes every empty row in every table in the active document.
' I don't know if it is absolutely necessary to work from the
' bottom up, but it has been my experience that it is often so
' when deleting something, otherwise you throw off your count.
'
Sub DeleteEmptyRowsVer2()
Dim oRow As Row
Dim nTable As Long
Dim nRow As Long
For nTable = ActiveDocument.Tables.Count To 1 Step -1
For nRow = ActiveDocument.Tables(nTable).Rows.Count To 1 Step -1
Set oRow = ActiveDocument.Tables(nTable).Rows(nRow)
If Len(oRow.Range) = (oRow.Cells.Count * 2) + 2 Then oRow.Delete
Next nRow
Next nTable
End Sub

Steven Craig Miller
 
B

Bigfoot17

Thanks for your reply and the time you took to work on this. It gets me
pointed in the general direction.

In my specifiic case all 8 cells have a already been populated with some
content and a bookmark that (I think) I need to place the content from the
userform. But if I am not placing any content in a row then I want to delete
the row and the contents of the two cells int he row.
 
B

Bigfoot17

I almost forgot the original point to the question though.

The table I wish to work with appears as the second table from the top of
the document, but it was not created second when I was making the template.
So I am re-asking how do I reference which table I want to work with? Is it
Tables(2)?
 
S

StevenM

To: Bigfoot,

<< The table I wish to work with appears as the second table from the top of
the document, but it was not created second when I was making the template.
So I am re-asking how do I reference which table I want to work with? Is it
Tables(2)? >>

Run macro for answer. <grin>

Sub TableIndices()
Dim newDoc As Document
Dim oRange As Range
Dim oTable As Table
Dim nIndex As Long
Dim nTables As Long

Set newDoc = Documents.Add
Set oRange = newDoc.Range
oRange.Text = vbCr & vbCr & vbCr
oRange.Collapse wdCollapseEnd
Set oTable = oRange.Tables.Add(oRange, 2, 2)
oTable.Range.Rows(1).Cells(1).Range.Text = "First Table Created"
Set oRange = newDoc.Range(0, 0)
Set oTable = oRange.Tables.Add(oRange, 2, 2)
oTable.Range.Rows(1).Cells(1).Range.Text = "Second Table Created"
nTables = newDoc.Tables.Count
For nIndex = 1 To nTables
newDoc.Tables(nIndex).Range.Rows(2).Cells(1).Range.Text = "Table
Index: " & nIndex
Next nIndex
End Sub

I assumed I knew the answer, but how many times have I made assumptions only
to be wrong? Too many to count. I spend a lot of my time writing similar
code, which basically asks: How does this work?

Steven Craig Miller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top