VBA Macro format specific tables in Word

TASvdN

New Member
Joined
Sep 11, 2019
Messages
1
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
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the forum. However, you do realize that this is an Excel forum, right? The code you posted is for Word.

I can see a few issues with the code, and it must have changed a little bit if it was working before because it shouldn't even compile as is. Before offering some suggestions, I just want to verify that you haven't posted this in another forum for Word where the good people there could also be helping you.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top