+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    06-02-2008
    Posts
    11

    Controling the size of a table when opened

    I have a form with a button that opens a table. I want to control the size of the table when it opens.
    Is this a macro control or some change to the button properties?

    Thanks
    Last edited by VBA Noob; 03-01-2009 at 01:35 PM.

  2. #2
    Forum Administrator
    Join Date
    03-18-2009
    Location
    India
    MS-Off Ver
    2003,2007
    Posts
    222

    Re: Controling the size of a table when opened

    Hi joe,

    I don't understand about controlling the size but if you want to "resize the table upto the page's width" you can do this.For this you can use this code this will allow you the resize the table fit to your page width.

    Code:
    Sub MakeTableFitPageSize()
    
    Dim myTable As Table
    Dim OriginalRange As Range
    Dim oRow As Row
    Dim oCell As Cell
    Dim UsableWidth As Single
    Dim TableWidth As Single
    Dim CellNo As Long
    
    If Selection.Tables.Count = 0 Then
        MsgBox "Please put your cursor inside a table and try again", vbInformation
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    System.Cursor = wdCursorWait
    
    Set OriginalRange = Selection.Range
    Set myTable = Selection.Tables(1)
    
    myTable.Rows.SetLeftIndent _
            LeftIndent:=0, RulerStyle:=wdAdjustNone
    
    'Calculate usable width of page
    
    With ActiveDocument.PageSetup
        UsableWidth = .PageWidth - .LeftMargin - .RightMargin
    End With
    
    'Calculate width of top row, on assumption this will be
    'the same as table width
    
    On Error Resume Next
    
    For CellNo = 1 To myTable.Rows(1).Cells.Count
    
        If Err = 5991 Then
            MsgBox "This macro doesn't work with tables that have vertically merged cells", _
                    vbInformation
            GoTo CleanUp
        Else If Err Then
            MsgBox Err.Description, vbInformation
            GoTo CleanUp
        End If
    
        TableWidth = TableWidth + myTable.Rows(1).Cells(CellNo).Width
    Next CellNo
    
    On Error Goto 0
    
    'Calculate and assign width of each cell in each row, such that the cell width relative
    'to the table's width stays the same as before. Do it for each row individually rather than
    'for a column at a time- otherwise the macro won't work
    'if any of the rows contain horizontally merged cells 
    
    For Each oRow In myTable.Rows
    
        For Each oCell In oRow.Cells
            oCell.Width = (oCell.Width) * (UsableWidth / TableWidth)
        Next oCell
    
    Next oRow
    
    OriginalRange.Select
    
    Cleanup:
         'Clear variables from memory
         Set  myTable = Nothing
         Set  OriginalRange = Nothing
         Set  oRow = Nothing
         Set  oCell = Nothing
         UsableWidth = 0
         TableWidth = 0
         CellNo = 0
    
         System.Cursor = wdCursorNormal
         Application.ScreenUpdating = True
    
    End Sub
    Try this hope it helps.Please let me know if this code not gives you appropriate results.

    Good Luck
    ExlGuru

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0