Hi all,
I'm struggling with a macro for automatic formatting tables in a Word document. The macro below, while flawed did work when only tables of a certain format where in the Word file. Now other tables have been added and that breaks down the macro.
The macro should only format specific tables that match the format in code below and ignore the other tables. I'm stuck now, I did try to use On Error Resume Next in the code, but that broke the format.
The code below is quite simple and the solution also should be, but I can't find it at the moment. I'm sure one of you has a simple solution. Many thanks for all input!
Private Sub CommandButton1_Click()
Dim r As Word.Range
Dim tbl As Table
Dim pgno, nOfTbl As Integer
nOfTbl = 0
'Update status of controls in the userform
With UserForm1
.Label1.Enabled = False
.Label2.Enabled = False
.Label3.Caption = "Please wait..."
.TextBox1.Enabled = False
.TextBox2.Enabled = False
.CommandButton1.Enabled = False
.CommandButton2.Enabled = False
End With
DoEvents
Set r = ActiveDocument.Content
'Pick Start page and end page
stpNum = CInt(UserForm1.TextBox1.Value)
enpNum = CInt(UserForm1.TextBox2.Value)
'Loop through each table and format it
tbpgno = tb1.Range.Information(wdActiveEndAdjustedPageNumber)
If tbpgno >= stpNum And tbpgno <= enpNum Then
'Count column number
nOfTbl = nOfTbl + 1
UserForm1.Label3.Caption = "Please wait... processing table: " & nOfTbl & " in page: " & tbpgno
DoEvents
'Sort column
tb1.Sort ExcludeHeader:=True, FieldNumber:=3, SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, FieldNumber2:=6, SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:=wdSortOrderAscending
'Adjust column width
tb1.Columns(1).Width = InchesToPoints(0.72)
tb1.Columns(2).Width = InchesToPoints(1.9)
tb1.Columns(3).Width = InchesToPoints(0.72)
tb1.Columns(4).Width = InchesToPoints(0.78)
tb1.Columns(5).Width = InchesToPoints(1.9)
tb1.Columns(6).Width = InchesToPoints(0.87)
tb1.Columns(7).Width = InchesToPoints(1.9)
End If
Next
'Update status of controls in the userform
With UserForm1
.Label1.Enabled = True
.Label2.Enabled = True
.Label3.Caption = nOfTbl & " Tables formatted successfully..."
.TextBox1.Enabled = True
.TextBox2.Enabled = True
.CommandButton1.Enabled = True
.CommandButton2.Enabled = True
End With
DoEvents
End Sub
I'm struggling with a macro for automatic formatting tables in a Word document. The macro below, while flawed did work when only tables of a certain format where in the Word file. Now other tables have been added and that breaks down the macro.
The macro should only format specific tables that match the format in code below and ignore the other tables. I'm stuck now, I did try to use On Error Resume Next in the code, but that broke the format.
The code below is quite simple and the solution also should be, but I can't find it at the moment. I'm sure one of you has a simple solution. Many thanks for all input!
Private Sub CommandButton1_Click()
Dim r As Word.Range
Dim tbl As Table
Dim pgno, nOfTbl As Integer
nOfTbl = 0
'Update status of controls in the userform
With UserForm1
.Label1.Enabled = False
.Label2.Enabled = False
.Label3.Caption = "Please wait..."
.TextBox1.Enabled = False
.TextBox2.Enabled = False
.CommandButton1.Enabled = False
.CommandButton2.Enabled = False
End With
DoEvents
Set r = ActiveDocument.Content
'Pick Start page and end page
stpNum = CInt(UserForm1.TextBox1.Value)
enpNum = CInt(UserForm1.TextBox2.Value)
'Loop through each table and format it
tbpgno = tb1.Range.Information(wdActiveEndAdjustedPageNumber)
If tbpgno >= stpNum And tbpgno <= enpNum Then
'Count column number
nOfTbl = nOfTbl + 1
UserForm1.Label3.Caption = "Please wait... processing table: " & nOfTbl & " in page: " & tbpgno
DoEvents
'Sort column
tb1.Sort ExcludeHeader:=True, FieldNumber:=3, SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, FieldNumber2:=6, SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:=wdSortOrderAscending
'Adjust column width
tb1.Columns(1).Width = InchesToPoints(0.72)
tb1.Columns(2).Width = InchesToPoints(1.9)
tb1.Columns(3).Width = InchesToPoints(0.72)
tb1.Columns(4).Width = InchesToPoints(0.78)
tb1.Columns(5).Width = InchesToPoints(1.9)
tb1.Columns(6).Width = InchesToPoints(0.87)
tb1.Columns(7).Width = InchesToPoints(1.9)
End If
Next
'Update status of controls in the userform
With UserForm1
.Label1.Enabled = True
.Label2.Enabled = True
.Label3.Caption = nOfTbl & " Tables formatted successfully..."
.TextBox1.Enabled = True
.TextBox2.Enabled = True
.CommandButton1.Enabled = True
.CommandButton2.Enabled = True
End With
DoEvents
End Sub