View Single Post
 
Old 02-18-2022, 01:35 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

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