none
word document tables RRS feed

  • Question

  • HI All,

             I have word document with number of tables in it, lets say 100 tables. when i double click on a particular table , i want to know the table number of current table clicked. for example if i am on 52th table and do double click then in event handler of double click, i want the table number (52) and process my logic on that. so my question is

    1. Is there any double click event available. (workaround will also work)

    2. How to get the table number of current table.(52 in above case) 

    thanks

    shashank


    shashank


    Friday, February 22, 2013 7:36 PM

Answers

  • Hi shashank

    1. WindowBeforeDoubleClick is an event of the Word.Application

    2. This should be done in two steps. First, you want to make sure the selection is actually in a table, I think? The WindowBeforeDoubleClick event provides an Argument for the Selection (where the double click occurred). You can test whether the Selection is in a table like this:

       If Selection.Tables.Count > 0 Then 'Selection is in a table

    If that's true, then you get a Range from the beginning of the document to that point and count the number of tables. Something like:

      Dim rng as Word.Range
      Set rng = ActiveDocument.Content
      rng.End = Selection.Range.End
      Dim nrTables as Long
      nrTables = rng.Tables.Count


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, February 23, 2013 5:51 AM
    Moderator
  • Try:

    Private Sub wdApp_WindowBeforeDoubleClick(ByVal Sel As Selection, Cancel As Boolean)
      If Sel.Information(wdWithInTable) = False Then Exit Sub
      MsgBox ActiveDocument.Range(0, Sel.End).Tables.Count
    End Sub

    I think what you're seeing is that the first click of a double-click event sets the selection.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, February 23, 2013 8:48 AM

All replies

  • Hi shashank

    1. WindowBeforeDoubleClick is an event of the Word.Application

    2. This should be done in two steps. First, you want to make sure the selection is actually in a table, I think? The WindowBeforeDoubleClick event provides an Argument for the Selection (where the double click occurred). You can test whether the Selection is in a table like this:

       If Selection.Tables.Count > 0 Then 'Selection is in a table

    If that's true, then you get a Range from the beginning of the document to that point and count the number of tables. Something like:

      Dim rng as Word.Range
      Set rng = ActiveDocument.Content
      rng.End = Selection.Range.End
      Dim nrTables as Long
      nrTables = rng.Tables.Count


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, February 23, 2013 5:51 AM
    Moderator
  • HI Cindy,

            Thank you very much. It is so easy to find table number, i did not know that.

    I have supplementary question that WindowBeforeDoubleClick event somehow could introduce abnormal behavior. suppose currently my selection(cursor position) is placed on 51st table. and i double click on 52nd table then it gives me table number 51 not 52. it is because my current selection is on 51. How can i fix this issue. 

    thanks

    shashank



    shashank

    Saturday, February 23, 2013 7:48 AM
  • Try:

    Private Sub wdApp_WindowBeforeDoubleClick(ByVal Sel As Selection, Cancel As Boolean)
      If Sel.Information(wdWithInTable) = False Then Exit Sub
      MsgBox ActiveDocument.Range(0, Sel.End).Tables.Count
    End Sub

    I think what you're seeing is that the first click of a double-click event sets the selection.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, February 23, 2013 8:48 AM