Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-16-2020, 04:50 PM
John 4 John 4 is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2013
Advanced Beginner
Insert a Paragraph mark before and after a table
 
Join Date: Oct 2019
Posts: 44
John 4 is on a distinguished road
Default Insert a Paragraph mark before and after a table

Word often (always?) leaves no space above and below tables. I'd like to add a paragraph before and after the table. I could have simply selected the table, then collapsed the selection to before or after the table and added paras that way, but if there's no text between the tables Word butts them tight against each other so that that method doesn't work in those cases.



I thought I had it figured out by cutting the table, inserting the paras, and then pasting the table back in again. However Word appears to treat the pasted table as a new table and performs the operation on that table again (to infinity probably).

Is there a way to get the loop to move on to the next table? Perhaps assigning a value to each table (which will be recognised after pasting it back in again). I've tried this but it takes me an age (and countless error messages) to get anything done.

Thanks for your help.

Code:
Sub AddParaB4andAfterTables()
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
  oTable.Select
  Selection.Cut
  Selection.InsertParagraphBefore
  Selection.InsertParagraphBefore
  Selection.Collapse Direction:=wdCollapseStart
  Selection.Move Unit:=wdCharacter, Count:=1
  Selection.Paste
Next
End Sub
Reply With Quote
  #2  
Old 10-16-2020, 06:32 PM
macropod's Avatar
macropod macropod is online now Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,489
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

If you give the table 'Around' wrapping, you can set the required space above & below without the need for empty paragraphs that will inevitably be problematic when the empty paragraph encounters a page boundary. Alternatively, you can increase the existing preceding/trailing paragraph space after/before to provide the desired spacing. Inserting empty paragraphs is the least desirable approach.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-16-2020, 07:55 PM
John 4 John 4 is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2013
Advanced Beginner
Insert a Paragraph mark before and after a table
 
Join Date: Oct 2019
Posts: 44
John 4 is on a distinguished road
Default

By problems "when the empty paragraph encounters a page boundary", do you mean having an empty line at the top of the page that's just below a table?
Reply With Quote
  #4  
Old 10-16-2020, 08:18 PM
macropod's Avatar
macropod macropod is online now Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,489
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

You could end up with an empty paragraph above the table when the table is otherwise at the top of the page, as well as an empty paragraph at the top of the next page where the table ends the current page.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 10-16-2020, 10:41 PM
John 4 John 4 is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2013
Advanced Beginner
Insert a Paragraph mark before and after a table
 
Join Date: Oct 2019
Posts: 44
John 4 is on a distinguished road
Default

Thanks Macropod,

I use widow/orphan control so blank lines at the bottom of a page is common regardless. The space at the top of the page doesn't really bother me to be honest. The two suggestions you've made introduce a number of issues I'd rather avoid. To me at least, it seems my solution is the most straightforward.

Also, increasing the space before and after a paragraph doesn't introduce a space between two tables that butt each other.

I don't like to impose on the advisors on this forum, so I usually spend a lot of time trying to figure out what i know said advisors could answer in less than two minutes. I keep my questions until something comes up that appears to me as though it would be relevant to a number of other problems I have, and would give me a better understanding of VBA coding (i think it's obvious I'm still at the 'struggling' stage). So if you (or anyone else) can answer my question without unreasonable trouble I'd be grateful.

On the other hand, I'm aware you're not under any obligation of course. Thanks again.
Reply With Quote
  #6  
Old 10-17-2020, 12:44 AM
gmayor's Avatar
gmayor gmayor is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,298
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

The following will work as requested
Code:
Dim oTable As Table
Dim oRng As Range
    If Selection.Information(wdWithInTable) = True Then
        Set oTable = Selection.Tables(1)
        Set oRng = oTable.Range
        oRng.Collapse 1
        oRng.Select
        Selection.SplitTable
        Set oRng = oTable.Range
        oRng.Collapse 0
        oRng.InsertParagraphAfter
    Else
        MsgBox "Selection is not in a table", vbCritical
    End If
    Set oTable = Nothing
    Set oRng = Nothing
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 10-17-2020, 03:04 PM
John 4 John 4 is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2013
Advanced Beginner
Insert a Paragraph mark before and after a table
 
Join Date: Oct 2019
Posts: 44
John 4 is on a distinguished road
Default

Many thanks Graham for your speedy response.

Here's your code, with some lines stripped out that appeared unnecessary (unless you can say otherwise?), inserted into the loop:
Code:
Dim oTable As Table
Dim oRng As Range
For Each oTable In ActiveDocument.Tables
    oTable.Select
        Set oTable = Selection.Tables(1)
        Set oRng = oTable.Range
        oRng.Collapse 1
        oRng.Select
        Selection.SplitTable
        Set oRng = oTable.Range
        oRng.Collapse 0
        oRng.InsertParagraphAfter
Next
End Sub
It works perfectly except for when tables are butted; in which case it adds a row to the top of the bottom butting table instead of separating them (I don't know why, because if i place the cursor in the same place (the collapse position) and select "split table" manually it works, i.e. separates the tables with a paragraph).

It was doing something very strange for a while if I traced through it with the F8 key instead of "running" it: when it came to the butting tables it would copy the first page of the document and paste it onto the end of the document while deleting the original last page. (Both problems were occurring before [and after I think] I stripped the extra lines out of the code by the way).

I'm not sure what settings I've changed, but the tables are no longer pasting as butted together so the mentioned strange problem has taken care of itself apparently; as well as the problem with adding a row instead of separating the tables. So everything's working well now

Many thanks for your help. Seeing what way people code who actually know how to code is very informative.

And thanks also to Macropod, the space at the top of the page doesn't bother me now like I said, but it may become an issue in future. And of course his "Update All Documents in a Folder" macro is gold
Reply With Quote
  #8  
Old 10-17-2020, 07:46 PM
Guessed's Avatar
Guessed Guessed is online now Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,935
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

I've had problems with table edge cases such as when a table is the first thing in a document. The consistent way I get around it is to use Word's insert caption which seems to avoid that issue and may well also work on your butted tables. I can't produce two butted inline tables without them merging - perhaps you are floating your tables?

In any case, try this approach which may work with your doc.
Code:
Sub aTest()
  Dim oTable As Table, oRng As Range, oFld As Field
  For Each oTable In ActiveDocument.Tables
    Set oRng = oTable.Range
    oRng.InsertCaption Label:=wdCaptionTable, ExcludeLabel:=True, Position:=wdCaptionPositionBelow
    oRng.InsertCaption Label:=wdCaptionTable, ExcludeLabel:=True, Position:=wdCaptionPositionAbove
  Next
  For Each oFld In ActiveDocument.Fields
    If LCase(oFld.Code) Like "*seq table*" Then oFld.Delete
  Next oFld
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 10-18-2020, 06:38 AM
gmayor's Avatar
gmayor gmayor is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,298
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

The code I posted works for the selected table, whether or not the table is at the start of the document. If you want it to work for all tables in the main story range then
Code:
Dim oTable As Table
Dim oRng As Range
Dim i As Integer
    For i = ActiveDocument.Tables.Count To 1 Step -1
        Set oTable = ActiveDocument.Tables(i)
        Set oRng = oTable.Range
        oRng.Collapse 1
        oRng.Select
        Selection.SplitTable
        Set oRng = oTable.Range
        oRng.Collapse 0
        oRng.InsertParagraphAfter
        DoEvents
    Next i
    Set oTable = Nothing
    Set oRng = Nothing
If you have an example of tables it doesn't work with, post the document so we can evaluate the problem.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #10  
Old 10-19-2020, 08:55 AM
Charles Kenyon Charles Kenyon is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 6,923
Charles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to behold
Default

Just one note, perhaps off-topic. An in-line-with-text table will always have a blank paragraph after it.
This has resulted in many a blank page when the table fills a page.

The manual method to insert a paragraph before such a table at the beginning of the document is to add a row at the beginning and then convert that row to text.
Reply With Quote
  #11  
Old 10-19-2020, 01:34 PM
macropod's Avatar
macropod macropod is online now Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,489
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Quote:
Originally Posted by Charles Kenyon View Post
The manual method to insert a paragraph before such a table at the beginning of the document is to add a row at the beginning and then convert that row to text.
That is far from optimal. pressing Ctrl-Home (to take you to the start of the first cell), then Enter, achieves the same outcome.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 10-20-2020, 06:27 AM
John 4 John 4 is offline Insert a Paragraph mark before and after a table Windows 10 Insert a Paragraph mark before and after a table Office 2013
Advanced Beginner
Insert a Paragraph mark before and after a table
 
Join Date: Oct 2019
Posts: 44
John 4 is on a distinguished road
Default

Thank you for your quick replies, and I apologise for my delayed response.

Both Andrew's and Graham's codes work perfectly (I added a few lines to Andrew's to convert all caption styles to my default paragraph style as the caption style left too big a space).

You're right Andrew, tables at the start and end of the document were causing the cutting and pasting problem and both your's and Graham's codes deal with it.

I've attached a shortened version of the document (with the text replaced), so you can see the butted tables. At present they're pasting again as butted. I did manage to reproduce the instance of them pasting as unbutted but once again am unsure how i did it. Altering the “Show text boundaries” or setting “Style area in draft and outline views” to a value greater than "0" may have been what was causing the change (from pasting butted or unbutted), but I can't reproduce the effect consistently so I can't say for sure. I'm saying this more for your benefit than mine as it appears to be something some of you are at least a little curious about. I've now got codes that add a space above and below whether they're butted or not so I'm happy enough no matter which way they paste; and the many table and formatting settings (on top of trying to get my head around VBA) is making me tired
Attached Files
File Type: docx Tables example.docx (22.9 KB, 6 views)
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert a Paragraph mark before and after a table Indention below paragraph mark... kikola Word VBA 13 05-26-2020 06:21 AM
Paragraph mark added on the top of every second page aptbs00 Word 6 09-14-2018 03:53 PM
Insert a Paragraph mark before and after a table Replace space with paragraph mark jeffreybrown Word VBA 8 08-22-2018 03:31 PM
Insert a Paragraph mark before and after a table can't delete paragraph mark at end of document kb Word 10 10-06-2017 02:32 PM
Insert a Paragraph mark before and after a table Final paragraph mark Caroline Word 2 02-22-2011 10:39 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 03:20 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft