format white space before & after text in table cells

User avatar
stuck
Panoramic Lounger
Posts: 8187
Joined: 25 Jan 2010, 09:09
Location: retirement

format white space before & after text in table cells

Post by stuck »

I know even less Word VBA than I do Excel VBA but I need to learn...

With the aid of the macro recorder I've worked out that, with the cursor in a single Word table cell, the following code sets the white space before and after text in the cell:

Code: Select all

With Selection.ParagraphFormat
     .SpaceBefore = 12
     .SpaceAfter = 6
End With
How do I apply those paragraph formats to al the cells in the first row of the table? Having first selected the table I tried:

Code: Select all

With Selection.Rows(1)
     .ParagraphFormat
     .SpaceBefore = etc
End With
but .ParagraphFormat is not valid. Please can someone point me towards the correct syntax?

Thanks,

Ken

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: format white space before & after text in table cells

Post by HansV »

Hi Ken, you can use

Code: Select all

    With Selection.Rows(1).Range.ParagraphFormat
        .SpaceBefore = 12
        .SpaceAfter = 6
    End With
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: format white space before & after text in table cells

Post by macropod »

You could use code like:

Code: Select all

Sub Demo()
With ActiveDocument.Tables(1).Rows(1).Range.ParagraphFormat
  .SpaceBefore = 12
  .SpaceBeforeAuto = False
  .SpaceAfter = 6
  .SpaceAfterAuto = False
End With
End Sub
If you want to work with just a selected table, change ActiveDocument to Selection or, if you want to work with a table other than the first, simply replace the '1' in Tables(1) with the required #.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8187
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: format white space before & after text in table cells

Post by stuck »

HansV wrote:Hi Ken, you can use...
Yup, that works nicely, :thumbup:
macropod wrote:...if you want to work with a table other than the first, simply replace the '1' in Tables(1) with the required #...
:laugh: Are you telepathic? I've already got a loop that works it's way through each table in the doc :thankyou: too.

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: format white space before & after text in table cells

Post by macropod »

In that case, try something along the lines of:

Code: Select all

Sub Demo()
Dim i As Long
With ActiveDocument
  For i = 1 To .Tables.Count
    With .Tables(i).Rows(1).Range.ParagraphFormat
      .SpaceBefore = 12
      .SpaceAfter = 6
    End With
  Next
End With
End Sub
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8187
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: format white space before & after text in table cells

Post by stuck »

Thanks again. To me, a rookie, that just looks like an alternative to the loop I've already got, which is:

Code: Select all

Dim tTable As Table
     For Each tTable In ActiveDocument.Tables
          tTable.Select
          ...do stuff here...
    Next
but is it? What I mean is how does one know type of structure is the 'better' option?

Ken

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: format white space before & after text in table cells

Post by HansV »

For i = ... and For Each ... work equally well. The difference is that Paul's code doesn't select the table(s). Just like in Excel, code usually runs more efficiently if you don't select the range(s) you want to modify.
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8187
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: format white space before & after text in table cells

Post by stuck »

HansV wrote:...Just like in Excel, code usually runs more efficiently if you don't select the range(s) you want to modify.
Ah yes! Hadn't noticed that.

Ta!

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: format white space before & after text in table cells

Post by macropod »

A loop using For i = 1 To .Tables.Count loop also has an advantage over a For Each tTable In .Tables loop if you want to restrict processing to a particular set of tables (e.g. the 2nd till 2nd-last, for which you could use For i = 2 To .Tables.Count -1).

In other cases, such as when you might want to delete tables under certain circumstances, you'd loop backwards, using code like:
For i = .Tables.Count To 1 Step -1
Last edited by macropod on 04 Jul 2015, 10:41, edited 1 time in total.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8187
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: format white space before & after text in table cells

Post by stuck »

macropod wrote:A loop using For i = 1 To .Tables.Count loop also has an advantage...
Ah yes, hadn't seen that either, that helps.

Thanks again.

Ken