Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 02-18-2022, 01:35 AM
gmayor's Avatar
gmayor gmayor is offline Help with script - Collapse Tables as of a point till the end of doc Windows 10 Help with script - Collapse Tables as of a point till the end of doc Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
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
 

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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:53 PM.


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