0

I have an excel form that will open up ms word and go to the sixth table and adjust the cell margins. I want the left and right cell margins to be 0.08 for the entire table.

https://i.stack.imgur.com/VvYJY.jpg

It works perfectly the first time the form is run, but the second time and after it won't do it. Here is my code. Can anyone tell me why this would happen?

Public Sub Table()

    Dim wrdApp
    Dim wrdDoc
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open("\\FileLocation")

    With wrdDoc
        'Goes to 6th table and selects it
        wrdApp.Selection.Goto wdGoToPage, wdGoToAbsolute, 1
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext
        wrdApp.Selection.Tables(1).Select


        With wrdApp.Selection.Tables(1)
            .TopPadding = InchesToPoints(0)
            .BottomPadding = InchesToPoints(0)
            .LeftPadding = InchesToPoints(0.08)
            .RightPadding = InchesToPoints(0.08)
            .Spacing = 0
            .AllowPageBreaks = True
            .AllowAutoFit = True
        End With

End Sub
Marcucciboy2
  • 3,156
  • 3
  • 20
  • 38
  • Also here, read up on how to avoid select, this is written for excel but goes the same way for word https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – Luuklag Oct 01 '18 at 15:58
  • Most of the comments and suggestions people are posting here also appear in your question https://stackoverflow.com/questions/52500495/distribute-column-width-on-6th-table-of-word-document-word-vba. One has to wonder why you didn't implement these things for that before asking another question with the same code... – Cindy Meister Oct 01 '18 at 17:20
  • I guess I have to get out of the habit of using the macro recorder. I’m still a noobie when it comes to vba. – Shelnes Elie Oct 01 '18 at 17:29

1 Answers1

2

Why are you using wrdApp.Selection.Goto What:=wdGoToTable, Which:=GoToNext? Why not Set wrdTbl = wrdDoc.Tables(6) and work with that? All that code may be referring to another table?

Try something like this:

Private Sub Sample()   
    Dim wrdApp As Object, wrdDoc As Object, wrdTbl As Object

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False

    Set wrdDoc = wrdApp.Documents.Open("\\FileLocation")

    Set wrdTbl = wrdDoc.Tables(6)

    With wrdTbl
        .TopPadding = wrdApp.InchesToPoints(0)
        .BottomPadding = wrdApp.InchesToPoints(0)
        .LeftPadding = wrdApp.InchesToPoints(0.08)
        .RightPadding = wrdApp.InchesToPoints(0.08)
        .Spacing = 0
        .AllowPageBreaks = True
        .AllowAutoFit = True
    End With    
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250