Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2023, 08:54 PM
AlanCantor AlanCantor is offline Select a table in Word Windows 10 Select a table in Word Office 2019
Competent Performer
Select a table in Word
 
Join Date: Nov 2020
Posts: 134
AlanCantor is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 01-15-2023, 09:03 PM
AlanCantor AlanCantor is offline Select a table in Word Windows 10 Select a table in Word Office 2019
Competent Performer
Select a table in Word
 
Join Date: Nov 2020
Posts: 134
AlanCantor is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 01-15-2023, 10:22 PM
gmayor's Avatar
gmayor gmayor is offline Select a table in Word Windows 10 Select a table in Word Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 01-15-2023, 10:45 PM
Guessed's Avatar
Guessed Guessed is offline Select a table in Word Windows 10 Select a table in Word Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 01-16-2023, 04:42 AM
ChrisGreaves ChrisGreaves is offline Select a table in Word Windows 10 Select a table in Word Office 97-2003
Novice
 
Join Date: Nov 2022
Location: Bonavista, Newfoundland
Posts: 22
ChrisGreaves is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
Hello Andrew.
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
Attached Images
File Type: png tables.png (81.3 KB, 13 views)
Attached Files
File Type: txt Tables.txt (1,016 Bytes, 3 views)
Reply With Quote
  #6  
Old 01-16-2023, 08:38 AM
AlanCantor AlanCantor is offline Select a table in Word Windows 10 Select a table in Word Office 2019
Competent Performer
Select a table in Word
 
Join Date: Nov 2020
Posts: 134
AlanCantor is on a distinguished road
Default

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!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select a table in Word 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
Select a table in Word Can't select a cell in my table dilorenzoc Word Tables 1 06-03-2015 05:56 AM
Select a table in Word Select Text in Table but Table Gets Selected Too RBusiness Word 1 06-07-2011 04:26 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:00 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft