Results 1 to 2 of 2

Thread: VBA Macro format specific tables in Word
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Sep 2019
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)

    Question VBA Macro format specific tables in Word

    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

    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

    '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

    '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
    End Sub

  2. #2
    Board Regular
    Join Date
    Mar 2016
    Post Thanks / Like
    3 Post(s)
    2 Thread(s)

    Default Re: VBA Macro format specific tables in Word

    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.
    Hobby: VBA and some .Net
    Career: the world of patent and trademark protection

    Where do I paste the code that I find on the internet to implement any code I provide

    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts