JohnRobinAllen
asked on
Fix Word Table column widths with VBA
I have an eight-column table in Word. I want to fix (i.e. freeze) the column widths of the first three columns. The following code works with a table in one document but not with a similar table in another document. What have I done wrong?
Dim i as Integer
Dim tbl as Table
Set tbl = ActiveDocument.Tables(1)
For i = 1 To 3
tbl.Columns(i).Width = tbl.Columns(i).Width
Next i
The error message I get is "Run-time error '4608': value out of range.
The i is not out of range, so it must be something about the columns the table in one document but not in the other.
I hope someone can help. Please.
j.r.a.
Dim i as Integer
Dim tbl as Table
Set tbl = ActiveDocument.Tables(1)
For i = 1 To 3
tbl.Columns(i).Width = tbl.Columns(i).Width
Next i
The error message I get is "Run-time error '4608': value out of range.
The i is not out of range, so it must be something about the columns the table in one document but not in the other.
I hope someone can help. Please.
j.r.a.
Are you sure it is the first table in both documents?
ASKER
Unfortunately it is the only table in both documents, but even if it were not, the code should still work if Tbl is set properly
j.r.a.
j.r.a.
I'm not sure about the error, but what is the purpose of this part of the code:
tbl.Columns(i).Width = tbl.Columns(i).Width
You just want to set the width of a column to the whatever it currently is? In other words, if the widths of the 3 columns are set to 1", 2", 1". Your code will set them to those numbers but if someone manually changes the widths to 2", 2", 2", the code will now set the columns to 2", 2", 2" as well.
Are you sure its the activedocument ... because there isn't much else ... assuming the column widths are within the size of the page size.
Chris
Chris
ASKER
The purpose of the code is to change the width setting from "PreferredWidth" to actual width. The problem I have is that I need to hide some lines in a table. I do so by moving them to the bottom of the table, and then I set the font size to 3 (the smallest possisble) and set the hidden value to True. To move the lines, I copied them, deleted the now empty rows, located to the bottom of the table, and pasted the lines there. Sometimes the widths of pasted columns do does not match the width of unmoved columns above. That led to problems.
To solve that problem, I thought that the width of the columns was a "preferred width" that could change to fit the data, so if I specified the width of the columns without using "preferred width" that would fix the problem. It worked, so I put the code into other documents, and then found out that with some documents the computer did not like that line.
When the table is first set up, I think I use a preferred width to handle data that might not fit a fixed width column. Once the columns are set to widths that fit the data, I think I have to freeze those widths so that I can copy lines, paste them elsewhere, and not have the lines mismatch other columns.
An alternative is simply to delete the code that tries to fix the widths and hope that the columns do not change width when being copied and pasted.
Any suggestions are most welcome. Please.
j.r.a.
To solve that problem, I thought that the width of the columns was a "preferred width" that could change to fit the data, so if I specified the width of the columns without using "preferred width" that would fix the problem. It worked, so I put the code into other documents, and then found out that with some documents the computer did not like that line.
When the table is first set up, I think I use a preferred width to handle data that might not fit a fixed width column. Once the columns are set to widths that fit the data, I think I have to freeze those widths so that I can copy lines, paste them elsewhere, and not have the lines mismatch other columns.
An alternative is simply to delete the code that tries to fix the widths and hope that the columns do not change width when being copied and pasted.
Any suggestions are most welcome. Please.
j.r.a.
I think that the problem is that Word returns 999999 when the value from individual objects in a collection differ.
If you don't want to set to a specific value such as 3cm or 1", just try matching the cell in the first row:
tbl.Columns(i).Width =tbl.Rows.First.cells(i).w idth
If you don't want to set to a specific value such as 3cm or 1", just try matching the cell in the first row:
tbl.Columns(i).Width =tbl.Rows.First.cells(i).w
ASKER
GrahamSkan's solution still produces an error message.
I'll try to make a document with the problem (and 99% of my current code deleted) to demonstrate the problem.
Coming soon to a computer near you . . .
j.r.a.
I'll try to make a document with the problem (and 99% of my current code deleted) to demonstrate the problem.
Coming soon to a computer near you . . .
j.r.a.
ASKER
The attached file has a VBA Subroutine, “SelectedRowsDelete.” For it to do its task, you should have selected a few rows to “delete” in the table of the doc, but to demo the problem you do not need to select the rows. You can launch the subroutine by pressing the "Alt + /" (slash) shortcut or just step through the lines. When you get to line 63 you get an error message, the subject of this EE question.
What is going on?
j.r.a.
Demo.doc
What is going on?
j.r.a.
Demo.doc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The solution will not work. Adding the "AllowAutofit" command simply does not solve the problem.
However the sub-text is that one should not use
tbl.Columns(i).Width = . . .
if it gives problems.
If Graham Skan cannot solve this problem, the problem is unsolvable, and one can save time by simply forgetting about the ".Width" command. We have to rely on the ".PreferredWidth" command.
I appreciate his help, as always.
j.r.a.
However the sub-text is that one should not use
tbl.Columns(i).Width = . . .
if it gives problems.
If Graham Skan cannot solve this problem, the problem is unsolvable, and one can save time by simply forgetting about the ".Width" command. We have to rely on the ".PreferredWidth" command.
I appreciate his help, as always.
j.r.a.