Home > Software design >  Formatting a table pasted from Excel to Word
Formatting a table pasted from Excel to Word

Time:05-25

Hi all from a rookie and sporadic vba user. I have successfully copied a set of tables from Excel into Word and, with the first routine below, formatted each one. In order to accommodate a larger font, I then need to change the row height of the first row and also to change the paragraph line spacing but the second piece of code fails to do this. I'm sure the solution is simple ......

For t = 1 To 6
    With appWD.Selection.Tables(t)
    .TopPadding = 0
    .BottomPadding = 0
    .LeftPadding = 0.11
    .RightPadding = 0
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = True
    .Rows.SetHeight RowHeight:=12, HeightRule:=2
    End With
Next t

For t = 1 To 6
    With appWD.Selection.Tables(t).Rows(1)
    .SetHeight RowHeight:=18
    .ParagraphFormat.LineSpacing = 15
    End With
Next t

••••ˇˇˇˇ

I'm running Excel/Word 2016 on a mac.

Thanks.

CodePudding user response:

Using appWD.Selection is poor practice. You have an open document you want to work on, so reference that directly. For example:

With wdDoc
  For t = 1 To 1
    With .Tables(t)
      .TopPadding = 0
      .BottomPadding = 0
      .LeftPadding = 0.11
      .RightPadding = 0
      .Spacing = 0
      .AllowPageBreaks = True
      .AllowAutoFit = True
      .Rows.SetHeight RowHeight:=12, HeightRule:=2
      .Rows(1).SetHeight RowHeight:=18, HeightRule:=1
      .Rows(1).ParagraphFormat.LineSpacing = 15
    End With
  Next t
End With

Note the the inclusion of the appropriate HeightRule argument for the Row 1 SetHeight method. Note also that all the processing is done in a single loop, rather than the two loops your code used.

  • Related