locked
VBA Word Tables - Store Tables in an Array For Formatting RRS feed

  • Question

  • Hi,

    is there a way to store table numbers in an array.

    Example the document may have 20+ tables I only want to change the borders of tables

    (1,3,5), later I can then choose another set of tables etc.

    the code below does not work

    [code]

    Sub UpdateTables()

    Dim i As Long
    Dim TableNumber As String
    TableNumber = Array("1", "3", "5")
    BorderColor = Array("wdBlue, wdWhite", "wdRed")

    Set oDoc = ActiveDocument

    For i = 0 To UBound(Split(TableNumber, ",")(i))

        With .Tables  '(i)

        With .Borders(Split(BorderColor, ","))

         .Borders(wdBorderLeft).ColorIndex = BorderColor
         .Borders(wdBorderRight).ColorIndex = BorderColor

    '      .Borders(wdBorderTop).ColorIndex = BorderColor
    '      .Borders(wdBorderBottom).ColorIndex = BorderColor
    '      .Borders(wdBorderHorizontal).ColorIndex =BorderColor
        End With
    Next
    End Sub


    thank you for your help

                    
    • Edited by Dan_CS Thursday, May 26, 2016 1:09 PM
    Thursday, May 26, 2016 1:08 PM

Answers

  • Just remove the quotes in your array.

    Sub test()
    
      Dim a As Variant
      Dim c As Variant
      Dim t As Table
      Dim i As Integer
      
      a = Array(2, 3, 5)
      c = Array(wdRed, wdWhite, wdBlack)
      
      For i = 0 To UBound(a)
        Set t = ActiveDocument.Tables(a(i))
        t.Borders(wdBorderLeft).ColorIndex = c(0)
        '' etc
      Next i
    End Sub

    • Marked as answer by Dan_CS Friday, May 27, 2016 1:57 PM
    Friday, May 27, 2016 1:02 PM

All replies

  • You can do something like this.  Not sure what you are trying to do with border color but adapt to your needs.

    Sub test()
    
      Dim a As Variant
      Dim t As Table
      Dim i As Integer
      
      a = Array(1, 3, 5)
      
      For i = 0 To UBound(a)
        Set t = ActiveDocument.Tables(a(i))
        t.Borders(wdBorderLeft).ColorIndex = wdBlack
        '' etc
      Next i
    End Sub


    • Edited by mogulman52 Thursday, May 26, 2016 2:14 PM
    • Proposed as answer by ryguy72 Thursday, May 26, 2016 5:26 PM
    Thursday, May 26, 2016 2:09 PM
  • Hi Mogul man,

    thanks for helping.

    How do i put the colors in an array.

     BorderColor = Array("wdBlue", "wdWhite", "wdBlack")

    Other wise i have to repeat the colours in the main code for each color

    thanks

    Friday, May 27, 2016 12:45 PM
  • Just remove the quotes in your array.

    Sub test()
    
      Dim a As Variant
      Dim c As Variant
      Dim t As Table
      Dim i As Integer
      
      a = Array(2, 3, 5)
      c = Array(wdRed, wdWhite, wdBlack)
      
      For i = 0 To UBound(a)
        Set t = ActiveDocument.Tables(a(i))
        t.Borders(wdBorderLeft).ColorIndex = c(0)
        '' etc
      Next i
    End Sub

    • Marked as answer by Dan_CS Friday, May 27, 2016 1:57 PM
    Friday, May 27, 2016 1:02 PM
  • Smashing my friend

    thank you very much!

    :)

    Friday, May 27, 2016 1:57 PM