Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which Word table am I in? 1

Status
Not open for further replies.

Bruce18W

Technical User
May 9, 2011
27
US
I'd like to develop a Word VBA function to highlight selected parts of a table (e.g., a row, column, etc.). I'd like to place the cursor anywhere in a table and then run the function (macro). One of the first steps in coding is to define a table object for the table that I am in. I see lots of info for manipulating a table that's already defined, e.g., tbl.Delete, but how do I set a table object to reference the table the cursor is in?

Thanks!

 
I just found the solution - Writing Word Macros by Roman is fantastic, dense, but fantastic. Here's the code if anyone else is interested:

Sub TableDelete()

Dim rng As Range, tbl As Table

Selection.Range.Expand wdTable
Set rng = Selection.Range
Set tbl = rng.Tables(1)
tbl.Delete
End Sub
 
Hi Bruce,

Your solution is over-engineered:
Code:
Sub Demo()
Selection.Tables(1).Delete
End Sub
and, even with error-checking (always good practice):
Code:
Sub Demo()
If Selection.Information(wdWithInTable) Then Selection.Tables(1).Delete
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi, please note that in my original query I am not trying to delete the table - just to get a reference to it. The "solution" (albeit over-engineered) that I submitted gets the reference. I should have left out the bit about deleting the table. But thanks for the tip about how to delete a table, with error checking.

Bruce
 



the reference to it is...
Code:
with Selection.Tables(1)
'do something here with this object

end with


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top