Set Word table column widths via VBA code

Liloj -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Hello,

I need to format Excel tables in Word. Most of it is done via Excel; I just need to copy into Word and resize the columns. I have about 80 tables to process, each consisting of 5 columns with different widths. (This work is intended to fit precise dimensions so that they can be inserted into a Publisher document that goes to the printer.)

Is there a VBA code that would modify the column widths of all selected tables? And if yes, preferably in this way:

- first merged row (9.1 cm)
- column 1: 1.7 cm
- column 2: 1.6 cm
- column 3: 1 cm
- column 4: 3 cm
- column 5: 1.8 cm

Thank you in advance for helping me; I’ve been looking for a code for a week and I can’t get anything to work...

7 answers

  1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
     
    Hello, I think you can easily figure out how to do this by recording a macro in Word while you adjust column widths.
    0
  2. Liloj
     
    Hello,
    So I did try, but with the macro recorder in Word I can't select elements after elements and perform the manipulations, it seems to me that with automatic mode you can't do what I want :(

    That's why I'm looking for a code in Visual Basic, I thought I could manage since I use VBA regularly with Excel... but I'm stuck too much :-S
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Have you obtained a macro? you’ll see VBA code there that you can draw inspiration from to modify your VBA code in Excel?
      0
      1. Liloj > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
         
        Unfortunately nothing is being saved, I did as I went along, suspend and resume the macro, but no code in the macro... I tried to reproduce an Excel code but no, then I found the following code on forums, but still not... then many others, but nothing works... You’re really my last hope! Thank you again for taking the time to help me resolve my problem. Sub Format_colonnes()
        '
        ' Format tbl
        For Each colonne In ActiveDocument.Tables_
        .Columns(1).Width = CentimetersToPoints(1.7)
        .Columns(2).Width = CentimetersToPoints(1.6)
        .Columns(3).Width = CentimetersToPoints(1)
        .Columns(4).Width = CentimetersToPoints(3)
        .Columns(5).Width = CentimetersToPoints(1.8)
        Next

        End Sub
        0
      2. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > Liloj
         
        indeed, i get the same kind of code when i save a macro in Word.
        why doesn't that work for you?
        0
  3. Liloj
     
    Well it doesn't work, I must have done something wrong somewhere, it gives me: compilation error, incorrect or unqualified reference

    I've toiled my brain but I can't understand
    0
  4. Liloj
     
    Sorry for my late reply, I had urgent matters to deal with :-S
    I hope my use of the tags is correct.
    No line indicated, it’s just noted compilation error: function or variable expected
    This is the first time I see this error code, which makes me think I must have messed up the syntax of the code

    Thanks again for your help !

    Sub Format_colonnes() ' ' Format tbl For Each colonne In ActiveDocument.Tables_ .Columns(1).Width = CentimetersToPoints(1.7) .Columns(2).Width = CentimetersToPoints(1.6) .Columns(3).Width = CentimetersToPoints(1) .Columns(4).Width = CentimetersToPoints(3) .Columns(5).Width = CentimetersToPoints(1.8) Next End Sub
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      There should be a highlighted line at the moment the error occurs.
      I would try:
      option explicit Sub Format_colonnes() ' ' Format tbl dim tb as table For Each tb In ActiveDocument.Tables tb.Columns(1).Width = CentimetersToPoints(1.7) tb.Columns(2).Width = CentimetersToPoints(1.6) tb.Columns(3).Width = CentimetersToPoints(1) tb.Columns(4).Width = CentimetersToPoints(3) tb.Columns(5).Width = CentimetersToPoints(1.8) Next tb End Sub
      0
  5. Liloj
     
    This time I do have a line highlighted... error code '5992' on the next line:

    tb.Columns(1).Width = CentimetersToPoints(1.7)

    I can't figure it out...
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      no message accompanying error 5992?
      0
  6. Liloj
     
    Impossible to access individual columns of this collection because the table has cells of different widths.
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      it seems clear to me: the table does not have the same number of columns on every row.

      the code works on all the tables in the document. if you want to work on all the selected tables, try replacing
      ActiveDocument
      with
      selection
      .
      0
  7. Liloj
     
    My bad, my first lines were merged :-s but I managed to fix that easily thanks to your remark.

    Thank you very much for your help!!!!!

    I still have a small issue with VBA but Excel, should I create a new post or continue on this one?

    Whatever the case, thanks again!
    0
    1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      It is preferable to mark this as resolved and to open a new discussion.
      0