Results 1 to 9 of 9
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    861
    Thanks
    0
    Thanked 10 Times in 9 Posts

    How to insert paragraph before table

    Using Word 2010, I have a table in the primary header. I need to insert a blank paragraph before (above) the table. I'm using the following code:

    Dim rng As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Range
    rng.InsertBefore vbCr

    The result is a paragraph at the beginning of the first cell of the table. How can I ensure that the newly added paragraph is before (not in) the table?

    Thanks.

  2. #2
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Partway to a solution

    I started playing with this in Word 2007 (which is what I have). If the table is the first thing in the header, you get the results you mentioned. I couldn't put anything above the table either manually or with an ordinary macro.

    So I tried something more elaborate. Put a carriage return AFTER the table, and select both the table and the paragraph mark. Then run this code:

    Sub insertbefore()
    '
    ' Works ONLY if you select the table AND the paragraph mark after it
    '
    '
    Selection.Copy
    Selection.Delete

    Selection.insertbefore ("dsfsd")
    Selection.Collapse direction:=wdCollapseEnd
    Selection.Paste
    End Sub

    You end up with an extra paragraph mark after the table (total of two), and of course you will probably insert something other than "dsfsd"

    I leave it to you to put in and delete the extra paragraph marks.

    - Jessica

  3. #3
    New Lounger
    Join Date
    Feb 2012
    Location
    New York
    Posts
    16
    Thanks
    0
    Thanked 5 Times in 5 Posts
    What you are looking for is something along the lines of Selection.SplitTable once you are at the top row of the table

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,979
    Thanks
    0
    Thanked 208 Times in 189 Posts
    The simplest method is to use SplitTable, but that means you must select the first cell, as SplitTable only works with selections.
    Code:
    Sub Demo()
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 1).Select
    Selection.SplitTable
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    jweissmn1 (2012-03-02)

  6. #5
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Cool!

    Thanks for pointing out SplitTable. I don't know how I missed that in the docs.

  7. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    861
    Thanks
    0
    Thanked 10 Times in 9 Posts
    I prefer not to use the Selection object. This is what I've found to work to my satisfaction:

    Code:
    Dim rng as Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range
    rng.Cut
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    If rng.Paragraphs.Count < 2 Then
          rng.InsertParagraph
    End If
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range
    rng.Collapse wdCollapseEnd
    rng.Paste
    (I know I should use tags around code, but I can't find where to add them.)

    Richard
    Last edited by macropod; 2012-03-04 at 16:26. Reason: Added code tags

  8. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,979
    Thanks
    0
    Thanked 208 Times in 189 Posts
    I generally prefer to not use Selections either. In this case, though, it's simpler. The only significant drawback is that it changes the selection. That can be overcome by:
    Code:
    Sub Demo()
    Dim Rng As Range
    Set Rng = Selection.Range
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 1).Select
    Selection.SplitTable
    Rng.Select
    Set Rng = Nothing
    ActiveWindow.View.Type = wdPrintView
    End Sub
    PS: The code tags can be found on the 'Advanced' panel - see the '#' there.
    Last edited by macropod; 2012-03-06 at 00:05.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #8
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    861
    Thanks
    0
    Thanked 10 Times in 9 Posts
    Also, selecting the table cell in the header causes the view to switch to draft, and the header opens in a pane at the bottom of the screen. I t hink that's a lot to have to "recover" from.

  10. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,979
    Thanks
    0
    Thanked 208 Times in 189 Posts
    Quote Originally Posted by richardbarrett View Post
    Also, selecting the table cell in the header causes the view to switch to draft, and the header opens in a pane at the bottom of the screen. I t hink that's a lot to have to "recover" from.
    Easily achieved by adding 'ActiveWindow.View.Type = wdPrintView' to the code, which I've now done.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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