Results 1 to 13 of 13
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    TableColumns (2000)

    Hello, I have lots of documents to reformt, and one of the things that takes the most time (and mouseclicks) is resizing the columns on tables.

    Is there a way of getting the column sizes from the first table i resize, and then automaticaaly applying them to later tables? I am repeating the same process as each table needs the same resizing!

    I have macros to do 'autosize row' and 'ask user for row size'. but wouldnt really know where to start to write a macro to get column sizes and then reapply to a selection.

    Any WORD gurus out there? <img src=/S/coffeetime.gif border=0 alt=coffeetime width=32 height=48>
    Thanks,

    pmatz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: TableColumns (2000)

    The following macro will set the column width for all subsequent tables in a document equal to those in the first one. You would need to adapt it if you want to apply it to some tables only. The macro assumes that all tables have the same number of columns.

    Sub Equalize()
    Dim a() As Single
    Dim n As Integer, i As Integer, j As Integer
    Dim aTable As Table

    On Error GoTo ErrHandler

    ' Store column widths of first table in array
    Set aTable = ActiveDocument.Tables(1)
    n = aTable.Columns.Count
    ReDim a(1 To n)

    For j = 1 To n
    a(j) = aTable.Columns(j).Width
    Next j

    ' Loop through the other tables and set column widths
    For i = 2 To ActiveDocument.Tables.Count
    Set aTable = ActiveDocument.Tables(i)
    For j = 1 To n
    aTable.Columns(j).Width = a(j)
    Next j
    Next i

    ExitHandler:
    Set aTable = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    Thanks, thats pretty good stuff [img]/forums/images/smilies/wink.gif[/img]

    I have been trying to find a way of getting the sizes from one table (1 macro assigned to toolbar), and then applying them to another table(2nd button on toolbar)

    problems [img]/forums/images/smilies/sad.gif[/img]

    Cant find a way to make a global array.(To hold the widths)

    Cant find a property to tell where the 1st column starts from (the indent), this is crewshall to getting the tables in line.

    On the plus i did find a post by your good self that had a CurrentTable function. Thanks [img]/forums/images/smilies/smile.gif[/img]

    so, i cant make a global array? but i can make a global variable... [img]/forums/images/smilies/sad.gif[/img]
    I had this problem in access, but then just made the global array in the modules section and it was fine.

    hmmmm...
    Thanks,

    pmatz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: TableColumns (2000)

    Do you want this to work across different documents? In that case, you should put the code in Normal.dot or a global template. Place the declaration for the array at the top of a standard module:

    Public a() As Single

    >> Cant find a property to tell where the 1st column starts from (the indent)

    Do you mean the "Indent from left" setting in Table Properties? It is ActiveDocument.Tables(1).Rows.LeftIndent

  5. #5
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    True, i found the indent from left property (though the measurements throw me a bit!)

    I dont want the public variable accross documents, just for the one i am working on (which is a read only template, so the code stays in there). But it wont let me Dimension a Public variable at the top !

    i get the error attached!

    cheers
    Attached Images Attached Images
    Thanks,

    pmatz

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: TableColumns (2000)

    Apparently, you put the declaration in an object module, that is, either ThisDocument or the module behind a userform. You should put it in a standard module, the kind you create by selecting Insert | Module.

    The indent is in points: 72 points = 1 inch.

  7. #7
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    Thankyou Hans. Very helpful man, I see what i was doing wrong!

    <font face="EraserDust">Have a good weekend, </font face=dust>
    Thanks,

    pmatz

  8. #8
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    Hi, my final code, working well as a module <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

    Option Explicit
    Public colSize() As Single, leftIndent As Single, noCols As Integer

    Sub getColSize()
    Dim j As Integer
    Dim aTable As Table
    'Check that selection is in table
    If inTable = True Then
    ' Store column widths of first table in array
    Set aTable = ActiveDocument.Tables(tableNo)
    noCols = aTable.Columns.Count
    ReDim colSize(1 To noCols)
    'Get column sizes
    For j = 1 To noCols
    colSize(j) = aTable.Columns(j).Width
    Next j
    leftIndent = aTable.Rows.leftIndent
    End If
    End Sub

    Sub setColSize()
    Dim n As Integer, j As Integer
    Dim aTable As Table
    'Check that selection is in table
    If inTable = True Then
    ' Store column widths of first table in array
    Set aTable = ActiveDocument.Tables(tableNo)
    If aTable.Columns.Count = noCols Then
    'Get column sizes
    For j = 1 To noCols
    aTable.Columns(j).Width = colSize(j)
    Next j
    aTable.Rows.leftIndent = leftIndent
    End If
    End If
    End Sub

    Function tableNo() As Integer
    Dim i As Integer
    For i = 1 To ActiveDocument.Tables.Count
    If (Selection.Range.Start >= ActiveDocument.Tables(i).Range.Start) And _
    (Selection.Range.End <= ActiveDocument.Tables(i).Range.End) Then
    tableNo = i
    Exit For
    End If
    Next i
    End Function

    Function inTable() As Boolean
    inTable = Selection.Information(wdWithInTable)
    End Function


    These two subs frun off buttons. I may do one to cycle through all tables aswell....
    Thanks,

    pmatz

  9. #9
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    Hi, well, it didnt take long for me to want to do something a little more complex !

    I now need to find a way of doing the following, and any help would be appreciated greatly [img]/forums/images/smilies/smile.gif[/img]

    I have 60 documents, each of which has three different tables. The table structure of each of the three is the same for each document repectively. The tables contain merged cells and a mixture of row / column sizes.

    I want to apply the same table propertys (measurements etc) to each document for each table in turn. so say i have table type 1, 2 and 3 i want to store the values of each row / column and then apply to the respective table 1, 2 or 3 for all 60 ocuments.

    i cant get column and row sizes as there are some merged. Can this be done by getting measurements for each cell?

    thanks
    Thanks,

    pmatz

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: TableColumns (2000)

    Yuck, no help from me this time. I hate tables with merged cells!

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: TableColumns (2000)

    You can get/set measurements for each cell. Example, if your insertion point is in the table:

    dblWidth = Selection.Tables(1).Cell(1,1).Width

    Selection.Tables(1).Cell(2,3).Width = 160

    I guess an array is most practical:

    <pre>Sub TableCellArray()
    Dim dblTable() As Double, r As Integer, c As Integer
    ' Assumes cells were only merged horizontally, not vertically
    With Selection.Tables(1)
    ReDim dblTable(1 To .Rows.Count, 1 To .Columns.Count)
    For r = 1 To UBound(dblTable)
    For c = 1 To .Rows®.Cells.Count
    dblTable(r, c) = .Rows®.Cells©.Width
    Next
    Next r
    End With
    Stop ' Inspect array info in View>Locals
    End Sub</pre>

    Hope this helps.

  12. #12
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: TableColumns (2000)

    Thanks ! That's halfway there! I was looking at the cell. width and height propertys. This should be very helpful,as I have to resize all the tables to fit in narrower margins of the new template they are going in. I dont think there are any merged verically cells, but due to my curios nature I was trying to get readings for them if they were.

    Strangely when I output to the debug, any vertically merged cells give a height of 999999 !!

    I understand your 'YUCK' comment Hans. Merged cells are not so easy to deal with.

    Thanks again Jefferson.
    Thanks,

    pmatz

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: TableColumns (2000)

    999999 means "undefined" or could be interpreted as "multiple," so it's actually a useful marker for vertically merged cells. (I see is most when there is a combination of two or more highlighting colors in a range and you try to get the color.)

Posting Permissions

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