I have a macro enabled word document. In the document is a table which captures values…
- User
I have a macro enabled word document. In the document is a table which captures values from an excel sheet. Is there a VBA code that I can add that will do the following: adjust row hieght to fit the contents.
- Assistant
Yes, you can use the following VBA code to adjust the row height in a Word table to fit the contents:
Sub AdjustRowHeight() Dim tbl As Table Dim cell As Cell Dim rng As Range Set tbl = ActiveDocument.Tables(1) 'Change the table index if necessary For Each cell In tbl.Range.Cells cell.Range.Select Selection.Rows.AutoFit Next cell End SubYou can run this code in the VBA editor in your Word document by pressing
Alt+F11, then go toInsert>Moduleand paste the code into the module. After that, you can run theAdjustRowHeightmacro to adjust the row height in the table to fit the contents. - User
Error: Method or data member not found - .AutoFit
- Assistant
I apologize for the oversight. The correct method to adjust the row height in a Word table is
SetHeight. Here is the updated VBA code:Sub AdjustRowHeight() Dim tbl As Table Dim row As row Set tbl = ActiveDocument.Tables(1) 'Change the table index if necessary For Each row In tbl.Rows row.SetHeight RowHeightRule:=wdRowHeightAuto, HeightRule:=wdRowHeightAuto Next row End SubYou can run this code in the VBA editor in your Word document by pressing
Alt+F11, then go toInsert>Moduleand paste the code into the module. After that, you can run theAdjustRowHeightmacro to adjust the row height in the table to fit the contents. - User
Error: Named argument not found - RowHeightRule:=
- Assistant
I apologize for the confusion. It seems I provided incorrect information. Here is the revised VBA code that should adjust the row height to fit the contents:
Sub AdjustRowHeight() Dim tbl As Table Dim row As Row Set tbl = ActiveDocument.Tables(1) 'Change the table index if necessary For Each row In tbl.Rows row.AutoFit Next row End SubYou can run this code in the VBA editor in your Word document by pressing
Alt+F11, then go toInsert>Moduleand paste the code into the module. After that, you can run theAdjustRowHeightmacro to adjust the row height in the table to fit the contents. Let me know if you encounter any further issues. - User
Error: Method or data member not found - .AutoFit
- Assistant
Apologies for the oversight. The
AutoFitmethod is not directly available for rows in a Word table. You can achieve the desired result by modifying the row height based on the content in each cell of the row. Here's an updated VBA code snippet to adjust the row height to fit the contents:Sub AdjustRowHeight() Dim tbl As Table Dim row As Row Dim cell As cell Set tbl = ActiveDocument.Tables(1) 'Change the table index if necessary For Each row In tbl.Rows For Each cell In row.Cells cell.HeightRule = wdRowHeightAuto Next cell Next row End SubYou can run this updated code in the VBA editor in Word by pressing
Alt+F11, inserting a new module, and pasting the code. After that, run theAdjustRowHeightmacro to resize the row height in the table to fit the contents of each cell. Let me know if you need further assistance.