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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Calling all cells (well addressing really) (2002 SP3)

    Author
    Topic
    #439499

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1050384

      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

      • #1050387

        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:

        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
        
        • #1050388

          Is this the sort of thing you want:

          With ActiveDocument.Tables(1)
              .AutoFitBehavior wdAutoFitContent
              .AllowAutoFit = True
          End With
          

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

          • #1050391

            You were just soooo close!

            it needs to be this:

            With Selection.Tables(1)
               .AutoFitBehavior wdAutoFitContent
               .AllowAutoFit = True
            End With
            

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

            • #1050393

              I did say “the sort of thing you want”… grin

        • #1050392

          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

    Viewing 0 reply threads
    Reply To: Reply #1050412 in Calling all cells (well addressing really) (2002 SP3)

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

    Your information:




    Cancel