none
Determining whether the cursor is inside a table and passing that table as a parameter RRS feed

  • Question

  • Hello,

    Here's my goal:

    If a document has several tables, the user should click inside a given table, then run a ribbon button, which should identify if the cursor is inside a table, and, if so, pass that table as a parameter to another procedure.

    I suspect that once I find out how to identify whether the cursor is inside a table, I will be on my way.

    Many thanks.

    Keith


    R K Howard

    Sunday, January 6, 2019 2:49 PM

Answers

  • You can use code like this:

        Dim tbl As Table
        If Selection.Information(wdWithInTable) Then
            Set tbl = Selection.Tables(1)
            ' ...
        Else
            MsgBox "The selection is not in a table!", vbInformation
        End If


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

    Sunday, January 6, 2019 7:54 PM
  • Hans' solution works specifically when the selection is fully contained inside a table. I would normally do a test to verify that the selection *includes* at least one table so that the selection might start and/or finish with a table included.

    If Selection.Tables.Count > 0 then

    Thursday, January 10, 2019 2:37 AM

All replies

  • You can use code like this:

        Dim tbl As Table
        If Selection.Information(wdWithInTable) Then
            Set tbl = Selection.Tables(1)
            ' ...
        Else
            MsgBox "The selection is not in a table!", vbInformation
        End If


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

    Sunday, January 6, 2019 7:54 PM
  • Many thanks Hans.

    That looks spot on. I need a couple of days to test please. I will come back shortly and either reply or close the posting. Happy 2019!

    Keith


    R K Howard

    Monday, January 7, 2019 5:08 PM
  • Hi Hans,

    You solved both problems.

    Many thanks.

    Keith


    R K Howard

    Wednesday, January 9, 2019 2:00 PM
  • Hi Hans, One more thing - do you do MS Word Dot Net consultancy by any chance? We have a Word add-in that we are continuing to expand. Thanks. -Keith

    R K Howard

    Wednesday, January 9, 2019 2:02 PM
  • Hello Keith,

    I'm sorry, I don't do .Net at all.


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

    Wednesday, January 9, 2019 2:57 PM
  • Hans' solution works specifically when the selection is fully contained inside a table. I would normally do a test to verify that the selection *includes* at least one table so that the selection might start and/or finish with a table included.

    If Selection.Tables.Count > 0 then

    Thursday, January 10, 2019 2:37 AM
  • Hello. That is a good point. Your approach provides more flexibility, in that it works if the selection contains part or all of a table or if the selection is entirely within the table, whereas Hans' approach requires the selection to be entirely within the table. Both approaches achieve my goal; I have gone with yours. :) Kind regards, Keith

    R K Howard

    Thursday, January 10, 2019 1:55 PM