Consulting

Results 1 to 6 of 6

Thread: Adjust Cell width but not affect overal row width

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Adjust Cell width but not affect overal row width

    Tables can drive me to the point of insanity.

    If I have a four row x three column table in a document, I can select one of the column 2 cells and adjust its width without affecting the width of the overall row. If I try that with VBA the overall row size is changed.

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table
      Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
      oTbl.Range.Cells(5).Width = InchesToPoints(1)
    lbl_Exit:
      Exit Sub
    End Sub
    I can work around that with what I feel is a cumbersome process:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table
    Dim dblW As Double
      Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
      'Option A
      oTbl.Range.Cells(5).Width = InchesToPoints(1)
      'at this point the overall row not longer is fit to window.
      'Option B
      dblW = oTbl.Range.Cells(5).Width + oTbl.Range.Cells(6).Width
      oTbl.Range.Cells(5).Width = InchesToPoints(1)
      oTbl.Range.Cells(6).Width = dblW - oTbl.Range.Cells(5).Width
    lbl_Exit:
      Exit Sub
    End Sub
    Is there some setting that can be applied to the table (I thought wdAutoFitWindow) so that regardless of what you do to individual cells (provided you don't try something illogical) that the overall table row remains the same width?
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    PreferredWidthType and PreferredWidth? Supposedly applies to Cells and Tables
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    I think this is what you were looking for


    Option Explicit
    
    
    Sub ScratchMacro_phh()
        Dim oTable    As Table
        Dim numRows As Long, numCols As Long
        Dim numRow As Long, numCol As Long
        
        'delete existing table for development
        On Error Resume Next
        ActiveDocument.Tables(1).Delete
        On Error GoTo 0
        
        'size
        numRows = 4
        numCols = 3
        
        Set oTable = ActiveDocument.Tables.Add(Selection.Range, numRows, numCols, wdWord8TableBehavior, wdAutoFitWindow)
       
        'cell to change
        numRow = 2
        numCol = 2
        
        Call oTable.Range.Rows(numRow).Cells(numCol).SetWidth(InchesToPoints(1), wdAdjustProportional)
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Perhaps:
    Sub Demo()
    Dim oTbl As Table
    Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
    oTbl.Range.Cells(5).SetWidth InchesToPoints(1), wdAdjustProportional
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Paul H thanks. Exactly.

    Break

    Paul E
    Yes, our coding style is often similar.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Yea, I tend to be wordy

    I also tend to think of Word tables as an Excel worksheet with rows and columns



    Don't forget to mark this one [SOLVED]
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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