Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Current table number (WinNT/Word97)

    <P ID="edit" class=small>(Edited by BerylM on 06-May-03 10:59. to add additional info)</P>I'm sure this has been covered before, but when I went to Search for it ... well, I'm sure you've read Leif's post! So apologies in advance for that.

    Could someone please tell me, in VBA, how do you (or is it possible to) take the table number for the table the cursor is currently sitting in? It will always be referenced from a dropdown field listing (as an exit macro). I will be using the same information in six different tables, so if it is not possible I can do six copies of the macro, each referencing a different table, but it seemed more efficient to just reference the current table each time ...

    Many thanks in advance!
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Current table number (WinNT/Word97)

    There are probably better methods, but the function below will return the index of a table if the entire selection is within that table, 0 otherwise.

    Function CurrentTable() As Integer
    Dim i As Integer
    For i = 1 To ActiveDocument.Tables.Count
    If (Selection.Range.Start >= ActiveDocument.Tables(i).Range.Start) And _
    (Selection.Range.End<= ActiveDocument.Tables(i).Range.End) Then
    CurrentTable = i
    Exit For
    End If
    Next i
    End Function

    If you would like to return the index of the table that contains the start of the selection, replace Selection.Range.End by Selection.Range.Start.

    (Note: I tried to use the pre-defined table bookmark and the Selection.Tables collection but got inconsistent results)

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Current table number (WinNT/Word97)

    Hans, you're a lifesaver! That part of my code now works perfectly!

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Now I've just got to work the field referencing bit out (see my other post this morning ...)
    Beryl M


  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Current table number (WinNT/Word97)

    If you are using the current selection to define the table then a simpler piece of code would be
    Selection.Tables(1) rather than trying to work out ActiveDocument.Tables(??)
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Current table number (WinNT/Word97)

    Good point, Andrew - I'll remember that for the next time (since the code Hans helped me with is working perfectly I'm not going to change it this time!).

    Many thanks, all! <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Beryl M


Posting Permissions

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