Consulting

Results 1 to 6 of 6

Thread: Find Table number with bookmark

  1. #1
    VBAX Regular
    Joined
    Sep 2005
    Posts
    6
    Location

    Find Table number with bookmark

    I have an array of data that I wish to put in a Table in a Word document, which contains several Tables. I will be using VBA

    To locate the starting point of the data column in the table, I have inserted a bookmark.

    Does anyone know how I can then determine the Table number from the BookMark properties using VBA?

    regards,
    Jacko

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Jacko,

    If you've bookmarked the table, you really don't need to know which table in the document it is - you can work with the table directly using code like:
    Sub Demo()
    Dim oCel As Cell, i As Integer
    With ActiveDocument.Bookmarks("MyTable").Range.Tables(1).Range
      For Each oCel In .Cells
        oCel.Range.Text = i
        i = i + 1
      Next
    End With
    End Sub
    For the above, the table's bookmark name is 'MyTable'.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Dec 2007
    Posts
    5
    Location

    Table number needed

    Thanks for this, macropod, much appreciated.

    I am a newbie when it comes to Word VBA ( but not Excel).

    The Word document I am using, prepared as a template by others, has many tables ( some used for normal text ), so it is difficult to know which Table number a table is eg Table(n) where I am looking for n

    I need the value of n so as to know where to start transferring data into a specific table using VBA. In fact, if I also had the column number and the cell number, that would pinpoint it exactly.

    Can anyone help here please?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Jill (Jacko?),

    In your(?) original post, it was said "To locate the starting point of the data column in the table, I have inserted a bookmark". That being the case, the code I provided shows how to work with the bookmarked table - you really don't need to know what the table # it might be to do that.

    If the bookmark is in a particular cell within the table, you can locate the bookmarked cell with code like:
    Sub Demo()
    Dim BkMk As String
    BkMk = "MyTable"
    With ActiveDocument.Bookmarks(BkMk).Range.Tables(1).Range
      With .Bookmarks(BkMk).Range.Cells(1)
        MsgBox "Column: " & .ColumnIndex & ", Row: " & .RowIndex
      End With
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Sep 2005
    Posts
    6
    Location
    Thanks macropod. I actually signed up with this forum the other day with my name Jacko , then today when searching the forum that I found an item under my sister's name. She had signed up some 12 months ago , but only used it twice.

    Today I logged in under Jill's name to see if it still worked, and while there answered this post in her name. I realised my mistake straight away, but could not delete the post.



    Thanks for the code, much appreciated. I am on a real steep learning curve.

    regards

    Jacko ( me)

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "so it is difficult to know which Table number a table is eg Table(n) where I am looking for n"

    Indeed it is. With Table(n), n always means, and ONLY means, the order in the document.

    Table(3) means the third table in the document.
    If you put another table in before it, Table(3) automatically becomes Table(4).

    For this reason, Table(index) has limited use. It is MUCH better if you have tables bookmarked. You can then action that particular table by name, no matter where it is, where it has possibly been moved, or even if it has changed the number of rows/columns.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •