PDA

View Full Version : Solved: Spacing between "objects"



Jinky Julie
07-29-2009, 06:02 AM
Hi again...

My macro (that many of you helped with) leaves my document a bit messy (esthetically) ...

After the run, I am left with single line paragraphs and small tables throughout the document. The problem is there is irregular spacing between the "objects".

EXAMPLE

paragraph


table


paragraph

table
table

etc....

Is there a way I can force single line spacing between the "objects" in the entire document???

Your help and direction is always appreciated...

With respect...

Julie

lucas
07-29-2009, 01:12 PM
I'm going to beat Gerry to the punch and tell you that if you were using styles that you would not have all those extra paragraphs for spacing, the styles would take care of it for you.

That being said you might use something like this which really just adds to your grief because it retains paragraphs between lines much as you have already done. But if you have no choice under the circumstances:
Sub TestMacro2()
Dim opara As Word.Paragraph
Dim var
For Each opara In ActiveDocument.Paragraphs
If opara.Range.Text = Chr(13) Then
opara.Range.Delete
End If
Next
For var = ActiveDocument.Paragraphs.Count To 1 Step -1
ActiveDocument.Paragraphs(var).Range.InsertParagraphAfter
Next
End Sub

fumei
07-29-2009, 01:46 PM
Julie, do keep Show/Hide as Show? If you are going to use Word seriously I strongly suggest you keep Show on.

The reason I mention this is:
EXAMPLE

paragraph


table


paragraph

table
table

OK, but is the "space" between the first "paragraph" and "table" ONE paragraph? Or multiple paragraphs. If you have Show/Hide as Show, then this can be seen in a glance.

I would also add a caution that Word considers every cell in a table as a paragraph, so....
For Each opara In ActiveDocument.Paragraphs

' and

For var = ActiveDocument.Paragraphs.Count To 1 Step -1
may have unintended results. In fact, it could very likely fail with a 5251 error: "This is not a valid action for the end of a row." on the line bolded below.

If opara.Range.Text = Chr(13) Then
opara.Range.Delete
End If
Word can be/do strange things with "paragraphs" some times. It is always best to be very very explicit and exact, AND definitely better to use styles properly.

And of course....use Styles.

Jinky Julie
07-30-2009, 06:15 AM
Thanks guys... so much information... I know that I have to be more explicit in my explanations... I try...

lucas: your macro worked to get rid of the paragraphs... which left the document info "compressed"... (no paragraphs between the "objects")

but as fumei predicted... 5251 error... the debugger highlighted this...
ActiveDocument.Paragraphs(var).Range.InsertParagraphAfter

fumei: I often use the Show/Hide feature... the "lines" between each "objects" are indeed separate paragraphs (each one with a ¶)

You both mentioned Styles... I of course understand what a style is but not how I would use them in this case...

As mentioned... lucas' code worked to get rid of the spaces... and with fumei's note that a cell is considered a paragraph... can single lines be placed between each "object" (paragraph and entire table)?

macropod
07-30-2009, 06:47 PM
Hi Julie,

Perhaps:
Sub CleanUp()
Dim i As Integer
With ActiveDocument
For i = .Paragraphs.Count - 1 To 2 Step -1
With .Paragraphs(i).Range
If .Information(wdWithInTable) = False Then
If .Previous.Information(wdWithInTable) = False Then
If .Previous.Text = vbCr Then .Previous.Delete
End If
End If
End With
Next
End With
End Sub

fumei
07-31-2009, 10:23 AM
macropod...VERY nice. I can use that.

Julie, I am not sure what you mean by: "can single lines be placed between each "object" (paragraph and entire table)?"

You can easily put a paragraph before each table.
Option Explicit

Sub ParaBeforeTable()
Dim aTable As Table
Dim r As Range

For Each aTable In ActiveDocument.Tables
Set r = aTable.Range
With r
.Collapse 1
.MoveStart Unit:=wdCharacter, Count:=-1
.InsertParagraphBefore
End With
Next
End Sub
Demo attached. Click "Paragraph Before Table" on the top toolbar.

The point of Styles is that you can define things for paragraphs, like the space before it, or the space after it. By doing so, you can have a document with NO "empty" paragraphs.

Jinky Julie
08-04-2009, 10:03 AM
Thanks everyone...

Again... everyone's stuff is working but... I'll try again to illustrate what I mean...

Original example (random numbers of ¶ between objects)
paragraph (single "sentence" or "line" of text)
¶ ("blank")

table (could be up to 85 rows but usually not more than 4)


paragraph

table
table

What I am looking for (single "blank" ¶ between each "object")
paragraph

table

paragraph

table

table

Can the .InsertParagraphBefore be used on the lines of text?

I will continue to try new things with your code... that's how I learn best...

Julie

lucas
08-04-2009, 10:34 AM
styles apply to tables too. You don't need a paragraph between if styles are used. That's the point everyone is trying to make.

macropod
08-05-2009, 04:46 PM
Hi Julie,

Style formatting doesn't insert an empty paragraph, per se, between each paragraph and the table before or after, but it allows you to force the document to preserve a defined amount of vertical space before or after a paragraph. Visually, that's the same as having the empty paragraph you seem so concerned about.

However, since you seem to be having trouble grasping the correct use of Styles, the following macro should do what you say you want:
Sub CleanUp()
Dim oPara As Paragraph
With ActiveDocument
' Delete all empty inter-table paragraphs. Note, though, that there
' must be at least one paragraph, empty or not, after the last table.
For Each oPara In .Paragraphs
With oPara.Range
If .Information(wdWithInTable) = False Then
If .Text = vbCr Then .Delete
End If
End With
Next
' Restore an empty paragraph before & after each table
For Each oPara In .Paragraphs
With oPara.Range
If Not .Previous Is Nothing Then
If .Information(wdWithInTable) = False Then
If .Previous.Information(wdWithInTable) = True Then
' If an empty last paragraph follows the last table,
' don't add another empty paragraph
If .Text <> vbCr Then .InsertBefore (vbCr)
End If
End If
End If
If Not .Next Is Nothing Then
If .Information(wdWithInTable) = False Then
If .Next.Information(wdWithInTable) = True Then
.MoveEnd wdCharacter, -1
.InsertAfter vbCr
End If
End If
End If
End With
Next
End With
End Sub
The first for .. next loop does essentially the same as the loop in my previous macro - except that it (now) preserves inter-paragraph breaks for paragraphs containing text between the tables. The second for .. next loop restores the empty paragraph immediately before & after each table.

Jinky Julie
09-25-2009, 06:28 AM
Hi Macropod (et al)...

Your code works great... but there is a slight issue that I cannot seem to get my head around...

Let's look at my original example...

¶ ("blank")

table (could be up to 85 rows but usually not more than 4)


paragraph

table
table

At the end there are two tables (which could happen anywhere in the document)... when the empty inter-table paragraphs are deleted, the two tables become one table... therefore when the new empty paragraphs are to be inserted before each table, Word only sees one table... no space can be inserted...

Can something be done about that??? I continue to play with and tweak it to get it to work... but...

Would it be helpful to mention that any of the tables that immediately proceed or follow another would never be of the same "structure" (i.e. different widths or different number of rows/columns)? Maybe looking at the structure of the table could help... I'm grasping, I know...

Thanks again for all of your help and guidance...

Julie

macropod
09-25-2009, 10:52 PM
Hi Julie,

Try changing:
If .Information(wdWithInTable) = False Then
If .Text = vbCr Then .Delete
End If
to:
If Not .Next Is Nothing And Not .Previous Is Nothing Then _
If .Information(wdWithInTable) = False Then _
If .Previous.Information(wdWithInTable) = False Then _
If .Text = vbCr Then .DeleteDo note though, that there was nothing in the previous macro that would have joined two tables that originally had no separators between them, as per your example.

Jinky Julie
09-28-2009, 09:13 AM
That did it Macropod!!!

Thanks a bunch.... One step closer to completing this big macro that I've been bugging you guys about so much...

Julie