Sub AdjustTableWidths()
Dim doc As Document
Dim tbl As Table
Dim tblWidth As Single
Dim docWidth As Single
Set doc = ActiveDocument
docWidth = doc.PageSetup.PageWidth - doc.PageSetup.LeftMargin - doc.PageSetup.RightMargin
For Each tbl In doc.Tables
tblWidth = tbl.PreferredWidth
If tblWidth > docWidth Then
tbl.PreferredWidth = docWidth
tbl.AllowAutoFit = False ' Prevent autofit to ensure width stays as set
' Optionally, you can adjust column widths proportionally
Dim col As Column
Dim totalColWidth As Single
totalColWidth = 0
' Calculate total width of columns
For Each col In tbl.Columns
totalColWidth = totalColWidth + col.Width
Next col
' Adjust each column proportionally
For Each col In tbl.Columns
col.Width = col.Width * docWidth / totalColWidth
Next col
End If
Next tbl
End Sub
Tuesday, December 24, 2024
VBA Word - Adjust Table alignment in middle of document
The following VBA code adjusts table alignment in middle of document:
Subscribe to:
Post Comments (Atom)
Hot Topics
-
Objectives To provide detailed information about ListBox Types of ListBox Using ListBox in VBA applications Please read the post till end...
-
The @page directive The @page directive in ASP.NET Core Razor Pages is crucial because it designates a Razor file as a Razor Page, allowin...
-
Excel VBA To Retrieve data from MS-Access Table By Ajeet Kumar Excel Range Object has a very interesting method called "CopyFromR...
No comments:
Post a Comment