none
Naming/Searching for Tables in a Document (Word VBA) RRS feed

  • Question

  • Is there any way to "Name" a table in a Word Document, so that it can easily be referred to later, either directly or by searching?

    I see that the ID property can be set (i.e. Tables(1).ID = "Table1"), but after doing so, can the table be searched for, or do I have to iterate through each table in the document to try to find the table with ID = "Table1"?

    Is there no way to refer to a Table through a name [i.e. Tables("Table1")], just as one would reference a worksheet in an Excel workbook? [i.e. Sheets("Sheet1")] Or is there some other way to name/locate a particular table besides using "brute force"? 

    Many thanks!


    DragonForest

    Friday, April 20, 2012 2:59 PM

Answers

  • To expand on the bookmark route, and it is what I use.

    If you bookmark ANY table, you can then use it as a name.  The advantage is that you can move the table anywhere you like, change the rows...whatever, and it does not affect the ability to use a name.  Normally if you move a table (or add another table before it), the index number changes.  this makes using an index number veryb unreliable.

    So.  You bookmark a table with the name "ClientData".  Now you can use a Table object and set it to THAT table, no matter where it is in the document.

    Sub GetClientData()
    Dim file
    Dim path As String
    Dim oTable As Table
    Dim WriteToDoc As Document
    Dim SourceDoc As Document
    path = "X:\Yadda\Blah\"
    Set WriteToDoc = ActiveDocument

    file = Dir(path & "*.doc")
    Do While file <> ""
        Set SourceDoc = Documents.Open(FileName:=path & file)
        Set oTable = SourceDoc.Bookmarks("ClientData").Range.Tables(1)
        WriteToDoc.Range.InsertAfter oTable.Cell(3, 1).Range.Text & vbCrLf
        SourceDoc.Close
        Set oTable = Nothing
        Set SourceDoc = Nothing
        file = Dir()
    Loop
    End Sub

    This code runs through all .doc files in a folder, opening each, setting a table object for a bookmarked table named ClientData, getting the text from cell row3,column1, and putting that text into a document.

    Bookmarking tables allows use of names for that table.  It is a very powerful tool.  I name all my tables.


    • Edited by fumei2 Sunday, April 22, 2012 1:23 AM
    • Marked as answer by DragonForest Sunday, April 22, 2012 7:55 AM
    Sunday, April 22, 2012 1:23 AM
  • "Brute force" seems to be the way to go:

    Sub FindTable()
        Dim tbl As Table
        For Each tbl In ActiveDocument.Tables
            If tbl.ID = "MyID" Then
                Exit For
            End If
        Next tbl
        If Not tbl Is Nothing Then
            ' ...
        End If
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by DragonForest Friday, April 20, 2012 4:48 PM
    Friday, April 20, 2012 3:10 PM
  • The Tables(index).ID property is designed for Internet/HTML/XML purposes.

    If you want to use it in Word, you have to use something like this:

    -----

    Selection.Tables(1).ID = "My Crazy Table"

    -----

    and then:

    -----

    Dim tbl As Table

    For Each tbl In ActiveDocument.Range.Tables
        If tbl.ID = "My Crazy Table" Then
            tbl.Select
            Exit For
        End If
    Next

    -----

    Or else, use bookmarks:

    -----

    Selection.Tables(1).Range.Bookmarks.Add "Crazy_Table"
    -----

    and then:

    -----

    ActiveDocument.Bookmarks("Crazy_Table").Range.Select

    -----

    • Marked as answer by DragonForest Friday, April 20, 2012 4:47 PM
    Friday, April 20, 2012 3:20 PM

All replies

  • "Brute force" seems to be the way to go:

    Sub FindTable()
        Dim tbl As Table
        For Each tbl In ActiveDocument.Tables
            If tbl.ID = "MyID" Then
                Exit For
            End If
        Next tbl
        If Not tbl Is Nothing Then
            ' ...
        End If
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by DragonForest Friday, April 20, 2012 4:48 PM
    Friday, April 20, 2012 3:10 PM
  • The Tables(index).ID property is designed for Internet/HTML/XML purposes.

    If you want to use it in Word, you have to use something like this:

    -----

    Selection.Tables(1).ID = "My Crazy Table"

    -----

    and then:

    -----

    Dim tbl As Table

    For Each tbl In ActiveDocument.Range.Tables
        If tbl.ID = "My Crazy Table" Then
            tbl.Select
            Exit For
        End If
    Next

    -----

    Or else, use bookmarks:

    -----

    Selection.Tables(1).Range.Bookmarks.Add "Crazy_Table"
    -----

    and then:

    -----

    ActiveDocument.Bookmarks("Crazy_Table").Range.Select

    -----

    • Marked as answer by DragonForest Friday, April 20, 2012 4:47 PM
    Friday, April 20, 2012 3:20 PM
  • Many thanks. That's extremely helpful!

    DragonForest

    Friday, April 20, 2012 4:47 PM
  • Many thanks. That's extremely helpful!

    DragonForest

    Friday, April 20, 2012 4:48 PM
  • To expand on the bookmark route, and it is what I use.

    If you bookmark ANY table, you can then use it as a name.  The advantage is that you can move the table anywhere you like, change the rows...whatever, and it does not affect the ability to use a name.  Normally if you move a table (or add another table before it), the index number changes.  this makes using an index number veryb unreliable.

    So.  You bookmark a table with the name "ClientData".  Now you can use a Table object and set it to THAT table, no matter where it is in the document.

    Sub GetClientData()
    Dim file
    Dim path As String
    Dim oTable As Table
    Dim WriteToDoc As Document
    Dim SourceDoc As Document
    path = "X:\Yadda\Blah\"
    Set WriteToDoc = ActiveDocument

    file = Dir(path & "*.doc")
    Do While file <> ""
        Set SourceDoc = Documents.Open(FileName:=path & file)
        Set oTable = SourceDoc.Bookmarks("ClientData").Range.Tables(1)
        WriteToDoc.Range.InsertAfter oTable.Cell(3, 1).Range.Text & vbCrLf
        SourceDoc.Close
        Set oTable = Nothing
        Set SourceDoc = Nothing
        file = Dir()
    Loop
    End Sub

    This code runs through all .doc files in a folder, opening each, setting a table object for a bookmarked table named ClientData, getting the text from cell row3,column1, and putting that text into a document.

    Bookmarking tables allows use of names for that table.  It is a very powerful tool.  I name all my tables.


    • Edited by fumei2 Sunday, April 22, 2012 1:23 AM
    • Marked as answer by DragonForest Sunday, April 22, 2012 7:55 AM
    Sunday, April 22, 2012 1:23 AM
  • Many thanks! That's tremendously helpful!

    DragonForest

    Sunday, April 22, 2012 7:56 AM
  • Can I follow up on the original question with a slight variant? Is there a VBA query that can tell me whether I'm in the first, second, or n-th table in a document?  

    Thanks in advance!

    Mark

    Thursday, December 19, 2013 3:40 PM
  • You could use code like this:

    Sub WhereAmI()
        Dim i As Long
        For i = 1 To ActiveDocument.Tables.Count
            If Selection.Start >= ActiveDocument.Tables(i).Range.Start And _
                    Selection.End <= ActiveDocument.Tables(i).Range.End Then
                MsgBox "(Start of) selection in table #" & i, vbInformation
                Exit Sub
            End If
        Next i
        MsgBox "(Start of) selection not in any table.", vbInformation
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, December 19, 2013 4:32 PM