Consulting

Results 1 to 12 of 12

Thread: Solved: Spacing between "objects"

  1. #1

    Solved: Spacing between "objects"

    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

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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:
    [VBA]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[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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....[vba]
    For Each opara In ActiveDocument.Paragraphs

    ' and

    For var = ActiveDocument.Paragraphs.Count To 1 Step -1
    [/vba]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.
    [vba]
    If opara.Range.Text = Chr(13) Then
    opara.Range.Delete
    End If
    [/vba]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.

  4. #4
    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...
    [VBA]ActiveDocument.Paragraphs(var).Range.InsertParagraphAfter[/VBA]

    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)?

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.[vba]
    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
    [/vba]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.

  7. #7
    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

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    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

  11. #11
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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 .Delete
    Do 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.
    Last edited by macropod; 09-25-2009 at 11:33 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •