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...
-
Main points about class Class is a template to create objects. Class is user defined data type. Class represents a business entity. Class is...
-
JavaScript was created in 1995 by a programmer named Brendan Eich for Netscape Communications Company. Brendan Eich worked in the same comp...
No comments:
Post a Comment