none
Word 2003:VBA Insert paragraph before table in a header RRS feed

  • Question

  • This issue is very similar to:
    "Word 2007:VBA - How do I insert text before a table in a header"

    But the table has vertically merged cells which causes a
    '5991: Cannot access individual rows in this collection because
    the table has vertically merged cells

    With tbl
          .Rows.Add BeforeRow:=.Rows(1)
          .Split (2)
    End With

    I know I can use the Selection.splitTable function,
    but I am trying to avoid using the selection object.

    Tim
    Wednesday, October 26, 2011 6:21 PM

Answers

  • Tim,

     

    Crude perhaps but if there is only one table in the header then maybe this would work:

    Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    oRng.Tables(1).Range.Cut
    oRng.InsertBefore vbCr
    oRng.Move wdParagraph, 1
    oRng.Paste
    End Sub

     


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by Tim_Shaf Monday, October 31, 2011 2:18 PM
    Friday, October 28, 2011 2:40 PM

All replies

  • Hi Tim,

    I'm afraid we have to use selectioin object to implement this. But after some researches, I find we might don't need to add row, split table and delete table to insert a paragraph. Check the code below: 

    Sub insertHeader()
        
        Dim tbl As Table
        Set tbl = ThisDocument.Tables(1)
        
        tbl.Select
        
        Selection.splittable
    
    End Sub
    

    It seems to me that this method is faster though the selection object is involved.

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 27, 2011 11:36 AM
    Moderator
  • I do something similar to this but I use a bookmark.  I manually create the bookmark in the template I use to create the document.  I then replace the bookmark with the paragraph text.  It is simple and reliable.
    Thursday, October 27, 2011 12:31 PM
  • Calvin,

    Below is what I am currently using.
    This tries not to use the Selection object,
    but if the table has vertically merged cells,
    it then uses it and backs out of the header.
    Still looking for a way without using the Selection object.

    Thanks,
    Tim

    'Use when table is at top of document or header
    Public Function AddParagraphBeforeTable(ByVal tbl As Table)
    On Error GoTo ErrTrap
    Dim rng As Range
    Set rng = tbl.Range
    With tbl
          .Rows.Add BeforeRow:=.Rows(1)
          .Split (2)
    End With
     rng.Tables(1).Delete
    Set rng = Nothing
    Exit Function
    ErrTrap:
    '5991: Cannot access individual rows in this collection because the table has vertically merged cells
    If Err.Number = 5991 Then
      tbl.Select: Selection.SplitTable
      ActiveWindow.View = wdPrintView   'Since Selection is used in Headers, must back out of Header
      ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
      ActiveWindow.View = wdPrintView
    Else
      Err.Raise vbObjectError + 12345, , Err.Description
    End If
    End Function


    Tim
    Thursday, October 27, 2011 1:26 PM
  • Thanks for the reply.

    These headers are in several thousand documents,
    so manually creating a bookmark is not an option.


    Tim
    Thursday, October 27, 2011 1:29 PM
  • Hi Tim,

    The main reaon we try to avoid using selection object is that this probably decrease the processing speed. Under your situation, however, in order to insert a paragraph before a table and to avoid using selection object, you need to process the table for three times: add a row, split table, delete splited table. So I don't think using this method will provide higher performance that directly using Selection.SplitTable method though Selection object will decrease speed somehow.

    In addition, you can use Application.ScreenUpdating property to turn off screen updating.

    http://msdn.microsoft.com/en-us/library/ff197438.aspx

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 28, 2011 9:58 AM
    Moderator
  • Tim,

     

    Crude perhaps but if there is only one table in the header then maybe this would work:

    Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    oRng.Tables(1).Range.Cut
    oRng.InsertBefore vbCr
    oRng.Move wdParagraph, 1
    oRng.Paste
    End Sub

     


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    • Marked as answer by Tim_Shaf Monday, October 31, 2011 2:18 PM
    Friday, October 28, 2011 2:40 PM
  • Calvin,

    My experience with setting "Application.ScreenUpdating to False" is
    that there is still significant screen activity apparent to the user.

    Another reason I am trying to avoid using the selection object is that
    this is being used in headers, so additional code must be used to back
    out of the headers.

    Thanks for the reply.
    Tim


    Tim
    Monday, October 31, 2011 1:47 PM
  • Greg,

    Using your suggestion, I created the following code.
    I need to do more testing, but it seems to work.

    Thanks,
    Tim

    'Works even when table is at top of document or header
    Public Function AddParagraphBeforeTable(ByVal tbl As Table)
    Dim rng As Range
    Set rng = tbl.Range
    With rng
      .Cut
      .InsertBefore vbCr
      .Move wdParagraph, 1
      .Paste
    End With
    Set rng = Nothing
    End Function



    Tim
    Monday, October 31, 2011 2:17 PM