#1
|
|||
|
|||
Select a table in Word
I'm trying to develop a script to select an entire table in Word, but only if the insertion point (cursor) is within the table. If the insertion point is NOT within a table, I want the script to signal an error.
I have a working prototype, but it's not quite right. The code selects just one column, not the entire table. Sub SelectThisTable() On Error GoTo ErrorHandler Selection.SelectColumn ' Do something with the selected table Exit Sub ErrorHandler: MsgBox "Not in a table" End Sub |
#2
|
|||
|
|||
PS: I should add that there are many tables in the document. The macro should act on the table that has keyboard focus, not on other tables.
|
#3
|
||||
|
||||
The following will do that
Code:
Sub SelectThisTable() Dim oTable As Table If Selection.Information(wdWithInTable) Then Set oTable = Selection.Tables(1) oTable.Range.Select Else MsgBox "Not in a table", vbCritical End If Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
||||
|
||||
There are multiple possibilities for the selection when the macro is run. The selection might not be in a table, inside a table, include part of a table, include a full table, include (parts of) more than one table.
Graham's code will work when the current selection is situated INSIDE a table. An alternative way of looking at the problem is to see if any part of any tables is included in the selection and select the first table instance. That alternative is shown in this code. Code:
Sub SelectATable() If Selection.Tables.Count > 0 Then Selection.Tables(1).Select Else MsgBox "There is no table in the current selection", vbCritical + vbOKOnly, "Oh No!" End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
I went overboard with this twenty years ago, and wrote a slew of macros to deal with partial tables. I realized that the selection could start either within or without a table, and could end within or without. If the end points were outside tables, then ENTIRE applied. But if either or both end points were in a table, one could ask for ENTIRE (that is, all tables embraced by the selection) or FRACTIONAL. As well I had a method for processing just the bounding tables OR the tables excluding the bounding tables. At the time I was doing a great deal of batch-processing - document conversion and cleansing - and this all made sense! Cheers, Chris |
#6
|
|||
|
|||
Thank you for the helpful responses!
While testing your code samples, and more of my own, I began to question the requirement that the cursor be inside a table. For example, what if the cursor is immediately AFTER a table? That should be OK. Through trial-and-error, I learned that this instruction... Code:
Selection.Tables(1).Select ...selects the table above when the cursor is on the blank line that follows a table. My guess is that the blank line inherits the formatting carried by the end-of-table marker. It's as if the blank line is programmatically part of the table that precedes it. But the blank line doesn't get selected. I learned a lot today! |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to Select All or Part of a Table in Word | jeffreybrown | Word VBA | 2 | 09-03-2018 08:23 PM |
Looking to copy select cells in table using dropdown list to paste to new table in another worksheet | CaptainRetired | Excel Programming | 18 | 01-04-2018 07:22 PM |
VBA Word Table - Select More than 1 Column at a time - Apply Formatting | jc491 | Word VBA | 12 | 09-24-2015 06:03 AM |
Can't select a cell in my table | dilorenzoc | Word Tables | 1 | 06-03-2015 05:56 AM |
Select Text in Table but Table Gets Selected Too | RBusiness | Word | 1 | 06-07-2011 04:26 PM |