Set Word table column widths via VBA code
Liloj
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention -
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...
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
-
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. -
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-
Have you obtained a macro? you’ll see VBA code there that you can draw inspiration from to modify your VBA code in Excel?
- 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
-
-
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-
could you share your code, and indicate on which line the error occurs? please use the code tags: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
-
-
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
-
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
-
-
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... -
Impossible to access individual columns of this collection because the table has cells of different widths.
-
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!