Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a cell in a certain table 1

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
I have many tables in a document some of which will be deleted if they're not needed. Those that are needed will have rows added with thier cells filled.

I am able to use Bookmarks to delete tables and add rows but I can't figure how to fill certain rows in a certain table.

I don't want to use anything like ActiveDocument.Tables(6).Cell(2, 2). I want to be able to identify(and fill) a cell by using a table name.

Is this possible and if so please help me accomplish this?

Here's a couple snippets of what I have so far to add rows and delete a table:
Code:
Dim oRows As Range
  If AppWd.ActiveDocument.Bookmarks.Exists("TableToAddRowTo1") Then
    Set oRows = AppWd.ActiveDocument.Bookmarks("TableToAddRowTo1").Range
    oRows.Rows.Add
  End If

  If AppWd.ActiveDocument.Bookmarks.Exists("TableToDelete1") Then
    Set oRows = AppWd.ActiveDocument.Bookmarks("TableToDelete1").Range
    oRows.Rows.Delete
  End If
 
If you are already using bookmarks, then continue to use them.

Bookmark the tables, using whatever names are appropriate. For my example, I will just use numbers.

Say you have 10 tables. Bookmark each table, making sure you have the whole table in the bookmark.

Table1 = named....Table1
Table2 = named....Table2

etc. If the tables have significant information that will help with naming, all the better.

Table1 = named ClientData

or whatever.


OK. So they are bookmarked. Now you can easily make table objects of them. You can, of course, also make table objects from the Tables collection. Having them bookmarked though makes other actions easy, and it is a convenient way to attach a name to a table.

If you just wanted all tables bookmarked and numbered in sequence:
Code:
Sub BookmarkTables()
Dim oTable As Word.Table
Dim j As Long
j = 1
For Each oTable In Appwd.ActiveDocument.Tables
    Appwd.ActiveDocument.Bookmarks.Add Name:="Table" & j, _
        Range:=oTable.Range
    j = j + 1
Next
End Sub
will make bookmarks of all the tables with names starting at Table1 and continuing.

Say you want to do something with Table81 (the 81st table in the document). It has been bookmarked with the name Table81.
Code:
Dim oTable81 As Word.Table
Set oTable81 = Appwd.ActiveDocument.Bookmarks("Table81).Range. _
   Tables(1)
Now you can anything you want with that table.

Anything.

Say you want to get the contents of each cell in Row 3 of the 81st table. You can create and use a Rows object of the table object.
Code:
Dim oTable81 As Word.Table
Dim oRow As Word.Row
Dim oCell As Word.Cell
Set oTable81 = Appwd.ActiveDocument.Bookmarks("Table81)_
   .Range.Tables(1)
Set oRow = oTable81.Rows(3)
    For Each oCell In oRow.Cells
        MsgBox oCell.Range.Text
    Next
Set oRow = Nothing
Set oTable = Nothing

Or say, if Table27 has the text "blah" in Row 2, Col 2, you want to add the text "I can't believe he said Blah!" to Row 3, Col 3 of Table 32.
Code:
Sub AddToBlah()
Dim oTable27 As Word.Table
Dim oTable32 As Word.Table

Set oTable27 = Appwd.ActiveDocument.Bookmarks("Table27"). _
    Range.Tables(1)
Set oTable32 = Appwd.ActiveDocument.Bookmarks("Table32"). _
    Range.Tables(1)

If oTable27.Cell(2, 2).Range.Text = "blah" & Chr(13) _
    & Chr(7) Then
   oTable32.Cell(3,3).Range.Text = _
        "I can't believe he said Blah!"
End If
Set oTable27 = Nothing
Set oTable32 = Nothing
End Sub

And so on and on and on. Once the tables are bookmarked, you can easily make table objects of them. You can doing pretty much whatever you want with the table objects.

Gerry
My paintings and sculpture
 

Thank you, thank you thank you!

I also appreciate the detail.

One more question.....you mention "If you are already using bookmarks, then continue to use them." Is there anything else I could/should be using? I just started this project so it's not too late to edit what I'm doing.
 
No, bookmarks are good. It is the easiest way to get the tables. I don't know about Word 2007, but not being able to name tables has always been a pain IMO. However, you can bookmark them, and that is just as good. As stated, once bookmarked, it is a very simple step to creating a table object for whatever table you want.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top