• Standardizing Table Width

    Author
    Topic
    #357279

    Using Word 97 in Windows 95/98.

    I need to convert a lot of tables to have the same overall width. They are all 2 columns, but the columns are not all the same width. The first columns give a term which vary a good bit in length. The second column will be shorter when the first column is longer. Right now the second columns are all too long, going past the page margins (they are output from a conversion from HTML-based help).

    In essense, the following steps show what I want to happen (If you have a better way of accomplishing the same goal, feel free.):

    1. For every two column nonbordered table in the document.
    2. Find the width of the first column.
    3. Find the width of the second column.
    4. Add the widths together.
    5. Subtract the sum from 390 pt. (for example 100+400=500; 390-500=-110 [table too wide])
    6. If the result is negative (this will be true when the original table is wider than 390 pt), subtract the difference to the width of the second column.
    7. If the result is positive (this will be true when the original table is narrower than 390 pt), add the difference to the width of the second column.
    8. Go to the next table…until the end of the document.

    Thanks for your help!!
    Troy
    P.S. This Saturday I start Visual Basic 6.0 class. Hopefully I’ll be able to start doing some of this for myself and helping others soon.

    Viewing 1 reply thread
    Author
    Replies
    • #530483

      Hi Troy,

      The following is a pretty literal transcription of your outlined steps (big credit to Big Al for the function to test for borders):

      Public Sub ResetTableWidths()
      'Gary Frieder  Woody's Lounge VB/VBA Forum  June 2001
      Dim aTable As Table
      Dim sngCol1W As Single
      Dim sngCol2W As Single
      Dim sngTblW As Single
      Dim sngDiffW As Single
      
      For Each aTable In ActiveDocument.Tables
          If aTable.Columns.Count = 2 And bIsBorderless(aTable) Then
              sngCol1W = aTable.Columns(1).Width
              sngCol2W = aTable.Columns(2).Width
              sngTblW = sngCol1W + sngCol2W
              sngDiffW = 390 - sngTblW
              If sngDiffW < 0 Then 'negative number, so add it:
                  aTable.Columns(2).Width = sngCol2W + sngDiffW
              Else
                  aTable.Columns(2).Width = sngCol2W + sngDiffW
              End If
          End If
      Next aTable
      End Sub
      '========================
      Public Function bIsBorderless(CurTable As Table) As Boolean
      'Borrowed from Big Al:
      Dim aBorder As Border
      For Each aBorder In CurTable.Borders
          If aBorder.LineStyle  wdLineStyleNone Then
              bIsBorderless = False
              Exit Function
          End If
      Next aBorder
      bIsBorderless = True
      End Function
      

      Good luck with the VB course!
      (You might find though that an Office VBA course will be useful as well, for doing stuff like this.)

      Gary

      • #530484

        Because you’re either adding a negative number, or adding a positive number, there’s no need for the If..Then..Else structure:

        Public Sub ResetTableWidths()
        'Gary Frieder  Woody's Lounge VB/VBA Forum  June 2001
        Dim aTable As Table
        Dim sngCol1W As Single
        Dim sngCol2W As Single
        Dim sngTblW As Single
        Dim sngDiffW As Single
        
        For Each aTable In ActiveDocument.Tables
            If aTable.Columns.Count = 2 And bIsBorderless(aTable) Then
                sngCol1W = aTable.Columns(1).Width
                sngCol2W = aTable.Columns(2).Width
                sngTblW = sngCol1W + sngCol2W
                sngDiffW = 390 - sngTblW
                aTable.Columns(2).Width = sngCol2W + sngDiffW
            End If
        Next 'aTable
        End Sub
        '========================
        Public Function bIsBorderless(CurTable As Table) As Boolean
        'Borrowed from Big Al:
        Dim aBorder As Border
        For Each aBorder In CurTable.Borders
            If aBorder.LineStyle  wdLineStyleNone Then
                bIsBorderless = False
                Exit Function
            End If
        Next aBorder
        bIsBorderless = True
        End Function
        
      • #530636

        This worked great. Thanks!!

        You mentioned an Office VBA class. What do you mean by this and how can I find them?

        Thanks!!
        Troy

        • #530662

          Hi Troy,

          Glad that worked!

          As far as Office/VBA, I have heard of classroom courses being available but I’m afraid I don’t have details – most likely you need to be in a major city to find these.

          There are excellent course materials for self-study available from AppDev – have a look here – there’s also some free downloadable intro material there as well. I did the CD-ROM version of their course about a year ago, and found it useful.

          Good luck!

          Gary

    • #530608

      Got the idea from here. yep

    Viewing 1 reply thread
    Reply To: Standardizing Table Width

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: