![]() |
|
#2
|
||||
|
||||
|
You seem to be confusing selections with ranges. If you are processing the tables, then either process the tables in a range or by their index e.g.
To process the last three tables, or the tables after the cursor location . The first example writes a text to the first cell of the last three tables. The second example changes the first cell text in all the tables (where it exists) from the cursor point to the end of the document. Only those tables are processed. There is no need to select a range in order to process it and if you collapse a selection or a range then the selection or range becomes the (empty) point it is collapsed to. Code:
Sub Macro1()
Dim oTable As Table
Dim lngCount As Long
Dim i As Long
lngCount = ActiveDocument.Tables.Count
For i = lngCount - 2 To lngCount 'process last three tables
Set oTable = ActiveDocument.Tables(i)
'do something with oTable e.g.
oTable.Cell(1, 1).Range.Text = "This is cell 1"
Next i
Set oTable = Nothing
End Sub
Sub Macro2()
Dim oRng As Range
Dim oTable As Table
Set oRng = ActiveDocument.Range(Selection.Range.Start, ActiveDocument.Range.End)
For Each oTable In oRng.Tables
'do something with otable e.g.
With oTable.Cell(1, 1).Range.Find
.Text = "cell 1"
.Replacement.Text = "the first cell"
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
Next oTable
Set oTable = Nothing
Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
| Tags |
| collapse, help please |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help please, Script to Delete Paragraph marks b4 Tables | Cendrinne | Word VBA | 4 | 02-08-2022 06:54 PM |
| Help with a script to find Duplicate data in the same row of a table or tables | Cendrinne | Word VBA | 9 | 09-07-2021 07:54 PM |
| Need Help to Script to align all the tables only as of a section to end of doc? | Cendrinne | Word VBA | 4 | 04-05-2021 11:37 AM |
| Printing Word-Tables as PDFs without making the borders 1 point | Tobinobi | Word Tables | 3 | 12-12-2017 10:29 AM |
| Script starts nesting tables without reason | selman555 | Word VBA | 1 | 10-17-2014 01:01 AM |