Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling all cells (well addressing really) (2002 SP3)

    I'm never sure whether to ask Word VBA questions in the Word or the VBA forum. This one one the coin toss today!

    When I want to set a word table so that the width automagically adjusts to the width of the contents, I have to select all the cells and then set off the preferred width checkboxes in both the column and cell tabs of the table properties box.

    So, in VBA, short of selecting the whole table, how do I address all of the cells/columns so as to alter the properties?

    TIA
    Regards
    Paul

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

    Re: Calling all cells (well addressing really) (2002 SP3)

    For Word and Excel, it desn't really matter whether you post VBA questions in the program's own forum or in the VB/VBA forum. For Access, we prefer to keep all questions, including those about VBA, in the Access forum.

    Does the following macro do what you want? You must click inside the table before running the macro.

    Sub AutoFitTable()
    With Selection.Tables(1)
    .AllowAutoFit = True
    .Select
    Selection.Cells.PreferredWidthType = wdPreferredWidthAuto
    Selection.Columns.PreferredWidthType = wdPreferredWidthAuto
    End With
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling all cells (well addressing really) (2002 SP3)

    Well sorta. My post did actually say "short of selecting the whole table" as normally in VBA you can achieve most things without selecting.

    This is quite similar to what I had, except I also had a bit to restore the original selection, and and you missed the preferred width setting for the table itself thus:
    <pre>Sub AutoFitTable()
    Set orig = Selection.Range
    With Selection.Tables(1)
    .AllowAutoFit = False
    .PreferredWidthType = wdPreferredWidthAuto
    .Select
    Selection.Cells.PreferredWidthType = wdPreferredWidthAuto
    Selection.Columns.PreferredWidthType = wdPreferredWidthAuto
    End With
    orig.Select
    End Sub
    </pre>


  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Calling all cells (well addressing really) (2002 SP3)

    Is this the sort of thing you want:
    <pre>With ActiveDocument.Tables(1)
    .AutoFitBehavior wdAutoFitContent
    .AllowAutoFit = True
    End With
    </pre>


    Note: I know almost nothing about Word, so am probably wrong!
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling all cells (well addressing really) (2002 SP3)

    You were just soooo close!

    it needs to be this:
    <pre>With Selection.Tables(1)
    .AutoFitBehavior wdAutoFitContent
    .AllowAutoFit = True
    End With
    </pre>

    otherwise all it does is work on the first table in the document instead of the current one.

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

    Re: Calling all cells (well addressing really) (2002 SP3)

    Here is one without selecting

    Sub AutoFitTable()
    Dim col As Column
    With Selection.Tables(1)
    .AllowAutoFit = True
    .PreferredWidthType = wdPreferredWidthAuto
    .Columns.PreferredWidthType = wdPreferredWidthAuto
    For Each col In .Columns
    col.Cells.PreferredWidthType = wdPreferredWidthAuto
    Next col
    End With
    End Sub

  7. #7
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Calling all cells (well addressing really) (2002 SP3)

    I did say "the sort of thing you want"... <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  8. #8
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling all cells (well addressing really) (2002 SP3)

    But did you see Rory's one, no selecting, no looping!

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

    Re: Calling all cells (well addressing really) (2002 SP3)

    Yes, Rory's code is much shorter! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

Posting Permissions

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