• How to insert paragraph before table

    Author
    Topic
    #481780

    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(wdHeaderFooterPrimary).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.

    Viewing 3 reply threads
    Author
    Replies
    • #1321723

      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

    • #1322804

      What you are looking for is something along the lines of Selection.SplitTable once you are at the top row of the table

    • #1322825

      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
      [Fmr MS MVP - Word]

      • #1322908

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

      • #1323212

        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

    • #1323215

      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.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1323411

        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.

        • #1323414

          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
          [Fmr MS MVP - Word]

    Viewing 3 reply threads
    Reply To: How to insert paragraph before table

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: