Consulting

Results 1 to 2 of 2

Thread: Sorting a table full of images

  1. #1

    Question Sorting a table full of images

    Also posted here:https://www.msofficeforums.com/word-...le-images.html

    Hi


    I'm trying to modify a table so as to have it's cells sorted from top to bottom and then from left to right

    I came across this VBA script, generously shared by Greg Maxey : Table Re-sorter

    Option Explicit
    Dim m_oTbl As Word.Table
     
    Sub SortTable()
      'Set the table object = the table with the selection
      On Error GoTo Err_Handler:
      Set m_oTbl = Selection.Tables(1)
      '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
      Exit Sub
    Err_Handler:
      MsgBox "Select a table an try again.", vbInformation + vbOKCancel, "Table Not Selected"
    End Sub
     
    Sub TableSort_Re_Sort(Optional bTopToBottom As Boolean = True)
    Dim oCell As Cell
    Dim arrData() As String
    Dim i As Long, j As Long, k As Long
     
      'Initialize the array with no elements
      ReDim arrData(i)
      'Load the array with data in table.  Skip loading empty cells
      For Each oCell In m_oTbl.Range.Cells
        If Left(oCell.Range, Len(oCell.Range) - 2) <> "" Then
          ReDim Preserve arrData(i)
          arrData(i) = Left(oCell.Range, Len(oCell.Range) - 2)
          i = i + 1
        End If
      Next
      'Sort the array
      WordBasic.SortArray arrData
      'Delete content of table
      m_oTbl.Range.Delete
      'Reset counter
      i = 0
      'Fill table with sorted results
      If bTopToBottom Then
        For k = 1 To m_oTbl.Columns.Count
          For j = 1 To m_oTbl.Rows.Count
            m_oTbl.Cell(j, k).Range.Text = arrData(i)
            'Get out when lasted array element has been inserted in table
            If i < UBound(arrData) Then
              i = i + 1
            Else
             GoTo lbl_Exit
            End If
          Next
        Next
      Else
        For Each oCell In m_oTbl.Range.Cells
          oCell.Range = arrData(i)
          'Get out when lasted array element has been inserted in table
          If i < UBound(arrData) Then
            i = i + 1
          Else
            GoTo lbl_Exit
          End If
        Next
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    The problem I have is that each cell in my table contains an image (barcode), not a string

    Is it possible to adapt Greg's code to handle images ? Maybe using different data types (Object, Shape, ...) ?

    Regards
    yann

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Seems you are still having an issue. Yann, please note how the cross post is notified. This allows those members here who wish to assist to follow the link to see what assistance is being offered on the alternative site. I've also included Greg's code here rather than ask people to have to go search for it.
    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
  •