Using MS Word VBA. I already have a nice macro that autofits all table widths to the window size (margin to margin).
I'm looking to do something similar to autofit all of the table row heights to display all of the text in each row. Currently, the table rows only display one line, and then the text wraps below and is not visible.
Any help is appreciated. Code is below:
Sub ResizeAllTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.AutoFitBehavior wdAutoFitWindow
Next oTbl
End Sub
You are looking for the Row.HeightRule property. Specifically, you will want it to be set to wdRowHeightAuto which is
The row height is adjusted to accommodate the tallest value in the row.
So using your example I would imagine it would look something like this
Sub ResizeAllTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.Rows.HeightRule = wdRowHeightAuto
Next oTbl
End Sub
Related
I'm very new to VBA. I review reports in MS Word from my co-workers on a daily basis and need to make sure that all instances of table borders of 1/2pt be converted to 3/4pt.
There are also instances of thicker border weights up to 1 1/2pt. But I only need to make sure that the minimum weight is 3/4pt.
This is my code so far (that I modified from this post) but it applies the weight to ALL BORDERS and only seems to work for one table in each document.
Sub BorderWeight()
Dim myTable As Table
Dim r As Variant
Set myTable = ThisDocument.Tables(1)
For Each r In myTable.Rows ' <-- loop through all rows in table
r.Borders(wdBorderBottom) = wdLineWidth075pt
Next r
End Sub
Any help is much appreciated.
Whenever I update the document, I need to add a row to the top of the table, just below the headers, and then move the old row to the next level. The table is usually present on the last page of the Word document. Please find attached the screenshot for the expected and actual behaviors.
Below is the code I have written to achieve:
Go to the last page and find the table
Add a row (This adds above the column header)
Expectations:
Add the row immediately after the column header and move the existing row to the next level
Add the date to the second column.
Click for the screenshot
My code:
Sub Macro1()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First)
'Other row formatting
Next theTable
End Sub
I am learning VBA and any help would be of great use. Thanks for your time.
screenshot2
As Intellisense shows you the input parameter for Table.Rows.Add is [BeforeRow]. This is an optional parameter so if it is omitted the new row is added after the last row of the table.
As you want the row to come after the first row and before the second you need to pass the second row as the parameter, e.g.
With ActiveDocument.Tables(ActiveDocument.Tables.Count)
With .Rows.Add(.Rows(2))
'add date
.Cells(2).Range.Text = Format(Date, "MMMM d, yyyy")
'add any additional row formatting
End With
End With
For example:
With ActiveDocument
With .Tables(.Tables.Count)
.Split (2)
With .Range.Characters.Last.Next
.FormattedText = .Next.Rows(1).Range.FormattedText
End With
.Rows(2).Range.Delete
End With
End With
It's not clear, though, what you mean by "move the existing row to the next level".
It needs next.
Sub test()
Selection.EndKey Unit:=wdStory
Dim theTable As Table
Dim theNewRow As Row
For Each theTable In ActiveDocument.Tables
Set theNewRow = theTable.Rows.Add(theTable.Rows.First.Next) '<~~ add next
'Other row formatting
Next theTable
End Sub
I'm very new to vba and word macros. I'm having an issue where I'm exporting a document to word and a few of the tables exceed page width. I was wondering if there is a way to check if each table is within page width and, if not, ONLY target the tables that have exceeded page width and auto fit those tables to the window.
Here is some code that autofits every table in the document to the window:
Sub tablesAutoFit()
Dim table As table
For Each table In ActiveDocument.Tables
table.AutoFitBehavior (wdAutoFitWindow)
table.AllowAutoFit = False
Next
End Sub
I want to know if there is a way to just autofit the tables that exceed page width, not every table in the document.
Thanks
In order to determine whether a table "fits" on a page it's necessary to determine the table width and compare it to the page width. The following example assumes the table contains no horizontally merged cells, so that the columns can be addressed. If that's not the case, you need to "walk" the cells in a(ny) row to get their widths.
Sub TableWidth()
Dim tbl As Word.Table
Dim col As Word.Column
Dim tblWidth As Double, pgWidth As Double
Dim pgSetup As Word.PageSetup
Set pgSetup = ActiveDocument.PageSetup
pgWidth = pgSetup.PageWidth - pgSetup.LeftMargin - pgSetup.RightMargin
Set tbl = ActiveDocument.Tables(1)
For Each col In tbl.Columns
tblWidth = tblWidth + col.width
Next
If pgWidth < tblWidth Then
tbl.AutoFitBehavior wdAutoFitWindow
End If
Debug.Print tblWidth, pgWidth
End Sub
I have a word document with a lot of tables. I would like a macro which changes the fonts size of all tables to 10, autofits each table to the window and distributes the columns evenly. I can accomplish the last two goals using the below code, but not sure how to change the font size. Any help would be greatly appreciated.
Sub changetables()
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.AutoFitBehavior wdAutoFitWindow
tbl.Columns.DistributeWidth
Next
End Sub
For your exact code use
tbl.Range.Font.Size = 12
I have retyped a few things to show better naming and spacing etc.
Sub changetables()
Dim CurrentTable As Table
For Each CurrentTable In ActiveDocument.Tables
With CurrentTable
.AutoFitBehavior wdAutoFitWindow
.Columns.DistributeWidth
.Range.Font.Size = 12
End With
Next CurrentTable
End Sub
Just change .size to .name, as in tbl.Range.Font.Name = "Calibri"
This should also work for whatever font name you wish to use by replacing Calibri within the double quotes to your desired font name.
very new to VBA but our clients want the all the data in 1,850 pages of Word Tables aligned right. I'm thinking this is pretty easy in VBA. I am trying to figure it out and I'm sure I could nail it on my own, but a deadline is forcing me to seek help. So I apologize in advance if I missed a published solution.
As an example they want this:
To be this:
So i've got:
Dim oTable As Table
Dim oRow As Row
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
But I don't know how to loop through just the body of the table. Also the top 4 rows (table title) is merged to one cell and the first column is still left aligned. Help, and the next rounds on me :)
Normally I'm not a huge fan of "please write code for me" but I've not done enough with VBA in Word and want to learn some myself.
This is going to get you most of the way there.
You do not currently provide enough information to allow me to guarantee the if statement is workable for the entire document but you should be able to go from here.
Sub alignTableElementsRight()
Dim oTable As Table
Dim oRow As Row
Dim i As Integer
Dim dataTable As Boolean
For Each oTable In ActiveDocument.Tables
'this will be set once you are in the "table" part and
'not headings
dataTable = False
For Each oRow In oTable.Rows
'you will need custom logic here to determine what your if statement
'is to properly execute on the right row, this is going to depend based on your table
'format, etc. This checks if a leftmost column heading is "65 to 66"
If (InStr(oRow.Cells(1).Range.Text, "65 to 66") > 0) Then
dataTable = True
End If
'if you are in the datatable, move all values to align right in each row following
If (dataTable = True) Then
For i = 2 To oRow.Cells.Count
oRow.Cells(i).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
Next i
End If
Next oRow
Next oTable
End Sub