Consulting

Results 1 to 3 of 3

Thread: copy paste table issue

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location

    copy paste table issue

    Let me start by saying I know this code is inefficient. It copies table 2 and pastes a copy of below table 2. What I'm trying to figure out is how do I get it paste without adding to table 2? I need to have a paragraph marker in between them. Any thoughts on how to do this and maybe how to make the code more efficient?

    Private Sub CommandButtonSectionB_Click()
    '
    Dim oTable As Table
    Dim oRng As Range
    Dim oLastCell As Range
    Dim sResult As String
    Dim iRow As Integer
    Dim iCol As Integer
    Dim sPassword As String
    sPassword = ""                                    'This is the password used to protect/unprotect form
    With ActiveDocument
     .Unprotect Password:=sPassword                        'Unprotect document
      Set oTable = .Tables(2)                              'Select the second table in the document
      iRow = (oTable.Rows.Count)                           'Set iRow to the last row in the supporting document section
      iCol = 2
      Set oLastCell = oTable.Range        'Record the row
      sResult = oLastCell.FormFields(1).Result             'Get the value in the row
      Set oRng = oTable.Range                   'Add the last row to a range
      oRng.Copy                                            'Copy the row
      oRng.Collapse wdCollapseEnd                          'Collapse the range to its end
      oRng.Select                                          'the end of the table
      Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
      Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
      
      Selection.Paste                                      'Paste the row at the end of the table
      
      .Protect NoReset:=True, Password:=sPassword, _
       Type:=wdAllowOnlyFormFields                          'Reprotect the controlled form
    'Selection.InsertBreak Type:=wdColumnBreak
    End With
    End Sub
    Last edited by Kilroy; 05-29-2019 at 01:45 PM.

  2. #2
    There are some variables in your code that are unused or have no apparent purpose; however the following is an improvement and will do what you require

    Private Sub CommandButtonSectionB_Click()'
    Dim oTable As Table
    Dim oRng As Range
    Dim iRow As Integer
    Dim sPassword As String
    sPassword = ""                                    'This is the password used to protect/unprotect form
        With ActiveDocument
            If Not .ProtectionType = wdNoProtection Then
                .Unprotect Password:=sPassword                        'Unprotect document
            End If
            Set oTable = .Tables(2)                              'Select the second table in the document
            iRow = oTable.Rows.Count                           'Set iRow to the last row in the supporting document section
            Set oRng = oTable.Range                   'Add the last row to a range
            oRng.Collapse wdCollapseEnd                          'Collapse the range to its end
            oRng.FormattedText = oTable.Range.FormattedText
            oTable.Rows(iRow + 1).Select
            Selection.SplitTable
            .Protect NoReset:=True, Password:=sPassword, _
                     Type:=wdAllowOnlyFormFields                          'Reprotect the controlled form
        End With
        Set oTable = Nothing
        Set oRng = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Thanks Graham. I was adding the lines below in a lame attempt to include the 2 empty paragraphs before and after the table:

    Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
      Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
    I have been scouring the web and cannot find anything to accomplish my task.

Posting Permissions

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