Consulting

Results 1 to 6 of 6

Thread: Executing macro on each subsequent page/table

  1. #1

    Question Executing macro on each subsequent page/table

    Hi

    I have the following label template in Windows (v11) Word (v2021) : https://bit.ly/3K8IrKS

    The objective is to print a list of mac addresses on individual labels

    When merging data into this template, Word naturally fills the cells in from left to right and then from top to bottom

    I would like to modify the resulting order of the merged cell entries in the table so as to sort them from top to bottom, and then from left to right

    I came across this VBA script, generously shared by Greg Maxey : https://gregmaxey.com/word_tip_pages/table_sorter.html

    It produces exactly the sought result; but for one page/table only. Which is totally fine.
    Except that I need to execute this sort procedure on the subsequent pages/tables also present in this document

    I therefore added some lines of code around the line that calls the "TableSort_Re_Sort" procedure as follows :

      
      Dim Pages As Long, PageNb As Long  Pages=Selection.Information(wdNumberOfPagesInDocument)
      For PageNb = 1 to Pages
    	Selection.GoTo What:=wdGoToPage, Name:=PageNb
    	TableSort_Re_Sort
      Next
    To test this out, I place the cursor at the top left cell of page/table #1 and then run the macro

    The added lines correctly position the cursor on the top left cell of each subsequent page/table through each iteration
    But unfortunately, the "TableSort_Re_Sort" procedure only gets executed on page/table #1

    If can obtain the desired (global) result (without my added code) if I position the cursor
    in the top left cell of each page/table and manually execute the macro

    There is obviously something that I don't understand in the VBA mechanism/process and hope that someone can point me in the right direction

    Thank you

    regards
    yann

  2. #2
    See https://www.msofficeforums.com/word-...age-table.html
    Please crosspost correctly
    
    Sub SortTable()  'Set the table object = the table with the selection
      On Error GoTo Err_Handler:
      For Each m_Otbl In ActiveDocument.Tables
        'Table must be uniform (not split or merged cells)
        If Not m_Otbl.Uniform Then
            MsgBox "The selected table has split or merge cells and cannot be sorted with this procedure", vbInformation + vbOKOnly, "Non-Uniform Table"
            Exit Sub
        End If
        TableSort_Re_Sort
      Next m_Otbl
      Exit Sub
    Err_Handler:
    End Sub
    Last edited by Aussiebear; 07-22-2023 at 06:41 AM. Reason: Apologies Graham but I've included your code here
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    This wouldn't have been necessary had I been allowed to delete this post

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,234
    Location
    Yann.shukor, Firstly welcome to the VBAX forum. We don't remove posts lightly. All we ask is that if you post the issue on another forum, please let us know. In this case all you had to say was that was posted on msofficeforum.com and that would have been that. I also note that you posted on another forum immediately after post your thread here, so there was plenty of time in which to let us know.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Quote Originally Posted by Aussiebear View Post
    Yann.shukor, Firstly welcome to the VBAX forum. We don't remove posts lightly. All we ask is that if you post the issue on another forum, please let us know. In this case all you had to say was that was posted on msofficeforum.com and that would have been that. I also note that you posted on another forum immediately after post your thread here, so there was plenty of time in which to let us know.
    understood, thanks and sorry

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,234
    Location
    That's okay.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •