Last active
February 20, 2022 17:12
-
-
Save lundeen-bryan/3bcd197d287e1cc08957dd0c52337d50 to your computer and use it in GitHub Desktop.
TableInWord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Attribute VB_Name = "mod_TableInWord" | |
''=========================================================================================== | |
'' Procedure: ......... TableInWord.bas/SizeTable | |
'' Description: ....... Create table if none, size all tables based on user input | |
'' Module URL: ........ https://gist.github.com/lundeen-bryan/3bcd197d287e1cc08957dd0c52337d50 | |
'' Version: ........... 1.0.0 - major.minor.patch | |
'' Created: ........... 2022-02-20 | |
'' Updated: ........... 2022-02-20 | |
'' Compatibility: ..... Word | |
'' Install: ........... Save this as ".bas" file or copy and paste text to a module in vba IDE | |
'' Contact Author: .... Bryan Lundeen | |
'' Copyright: ........ ©2022. Permission to modify with credit to original. | |
'' Notes: ............. _ | |
'' (1) Provided as answer to question on a LinkedIn post found at: | |
'' https://www.linkedin.com/feed/update/urn:li:activity:6900789384522665984/ | |
'' (2) InchesToPoints method found at: | |
'' https://archive.is/wip/AegdR | |
'' (3) Tables.Count property found at: | |
'' https://archive.is/wip/FPfc7 | |
'' (4) Column.Width property found at: | |
'' https://archive.is/wip/yDksN | |
''=========================================================================================== | |
Option Explicit | |
Sub TableQuestionAnswer() | |
' | |
' TableQuestionAnswer | |
' Inset a table and change column width | |
' | |
Dim Table_tbl As Table 'represents a table | |
Dim TableCells_lng As Long 'represents the count of cells in a table | |
Dim TableWidth_sng As Single 'represents the table width | |
Dim TableCount_lng As Long 'represents the # of tables in document | |
Dim NumColumns_sng As Single 'represents the # of columns in new table | |
Dim NumRows_sng As Single 'represents the # of rows in new table | |
Let TableCount_lng = ActiveDocument.Tables.Count | |
If TableCount_lng = 0 Then | |
Let NumColumns_sng = InputBox( _ | |
Prompt:="How many columns do you need in the table?", _ | |
Title:="Columns Needed" _ | |
) | |
Let NumRows_sng = InputBox( _ | |
Prompt:="How many rows do you need in the table?", _ | |
Title:="Rows Needed" _ | |
) | |
ActiveDocument.Tables.Add Range:=Selection.Range _ | |
, NumRows:=NumRows_sng _ | |
, NumColumns:=NumColumns_sng _ | |
, DefaultTableBehavior:=wdWord9TableBehavior _ | |
, AutoFitBehavior:=wdAutoFitWindow | |
End If | |
Let TableWidth_sng = InchesToPoints( _ | |
InputBox( _ | |
Prompt:="How many inches wide do you want the column?" & vbCrLf & _ | |
"Can be in decimals, e.g. 1.5", _ | |
Title:="Column Width Setting" _ | |
) _ | |
) | |
For Each Table_tbl In ActiveDocument.Tables | |
With Table_tbl | |
.PreferredWidthType = wdPreferredWidthPoints | |
.PreferredWidth = TableWidth_sng | |
With .Range | |
For TableCells_lng = 1 To .Cells.Count Step 1 | |
.Cells(TableCells_lng).Width = TableWidth_sng | |
Next | |
End With | |
End With | |
Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment