Consulting

Results 1 to 9 of 9

Thread: Code to insert new row with content control including drop downs

  1. #1

    Code to insert new row with content control including drop downs

    I have word 2010.
    I am creating a form and need the form to automatically insert a new row that has the same contents as the row before. The rows include form fields as well as drop down boxes. The code I am currently using says it will do this, but it only adds a new row, which is blank. I am thinking it isn't for drop down boxes? I appreciate any help!
    It is table 2, and has 12 columns and 9 rows
    Here is the code I am using:
    Sub AddRow()
    'Run on exit from the last form field in
    'the last row of the table
    Dim oTable As Table
    Dim oRng As Range
    Dim oNewRow As Range
    Dim oCell As Range
    Dim oLastCell As Range
    Dim sResult As String
    Dim iRow As Long
    Dim iCol As Long
    Dim CurRow As Long
    Dim i As Long
    Dim sPassword As String
    sPassword = "GRIN"
    'password to protect/unprotect form
    With ActiveDocument
    .Unprotect Password:=sPassword
    'Unprotect document
    Set oTable = Selection.Tables(2)
    'Select the appropriate table
    iCol = oTable.Columns.Count 'Record the last column number
    Set oLastCell = oTable.Cell(iRow, iCol).Range 'Record the last cell
    sResult = oLastCell.FormFields(1).Result 'Get the value in the last cell
    Set oRng = oTable.Rows.Last.Range
    'Add the last row to a range
    Set oNewRow = oTable.Rows.Last.Range 'Add the last row to another range
    oNewRow.Collapse wdCollapseEnd 'Collapse the second range to the end of the table
    oNewRow.FormattedText = oRng
    'insert the content of the last row into the new range
    'thereby adding a new row with the same content as the last row
    CurRow = oTable.Rows.Count 'Determine the new last row of the table
    For i = 1 To iCol 'Repeat for each column
    Set oCell = oTable.Cell(CurRow, i).Range 'process each cell in the row
    oCell.FormFields(1).Select 'Select the first field in the cell
    With Dialogs(wdDialogFormFieldOptions) 'and name it
    .Name = "Col12" & i & "Row9" & CurRow 'eg Col1Row2
    .Execute 'apply the changes
    End With
    Next i
    'Select the formfield in the last cell of the previous row
    oLastCell.FormFields(1).Select
    With Dialogs(wdDialogFormFieldOptions)
    .Exit = "" 'and remove the exit macro
    .Execute 'apply the changes
    'but note that this clears the value from the cell
    End With
    oLastCell.FormFields(1).Result = sResult 'so restore the result of the cell
    .Protect NoReset:=True, _
    Password:=sPassword, _
    Type:=wdAllowOnlyFormFields
    'Reprotect the form
    .FormFields("Col1Row" _
    & CurRow).Select 'and select the next field to be completed
    End With
    End Sub
    Last edited by saundrals; 06-24-2014 at 04:21 PM. Reason: Add code tags

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Cross-posted at: http://www.msofficeforums.com/word-t...t-new-row.html
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    Without placing too fine appoint on it, the code you've posted is for formfields, not content controls. Your thread title refers to content controls but the body refers to formfields. These are not the same. Which are you using?

    PS: When posting code, please use the code tags. They're inserted via the # symbol on the posting menu.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thank you. I guess I am using content controls. I have two columns with drop down boxes and 10 columns with text fields. I need these to copy into the inserted row

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    I already gave you a link to a demo for that at msOfficeForums: http://www.msofficeforums.com/word-v...html#post38461
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    yes, but I was unable to use that code. When I right clicked the last cell in the last row, then properties, there was nothing listed to run on exit. Here's the code:
    Option Explicit
    Dim bLastCell As Boolean
    
    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    With Selection.Range
      If .Information(wdWithInTable) Then
        'To limit processing to a particular table, change:
        ' Selection.Tables(1).Range below, to:
        ' ActiveDocument.Tables(#).Range, where # is the table index.
        With ActiveDocument.Tables(2).Range
          If Selection.Cells(1).RowIndex = .Cells(.Cells.Count).RowIndex Then
            If Selection.Cells(1).ColumnIndex = .Cells(.Cells.Count).ColumnIndex Then
              bLastCell = True
            End If
          End If
        End With
      End If
    End With
    End Sub
    
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim CCtrl As ContentControl, Prot As Variant, Pwd As String
    If bLastCell = True Then
      If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
        With ActiveDocument
          Prot = .ProtectionType
          If .ProtectionType <> wdNoProtection Then
            Pwd = "GRIN" 'Insert password here
            Prot = .ProtectionType
            .Unprotect Password:=Pwd
          End If
          With Selection.Tables(1).Rows
            With .Last.Range
              .Copy
              .Next.InsertBefore vbCr
              .Next.Paste
            End With
            For Each CCtrl In .Last.Range.ContentControls
              With CCtrl
                If .Type = wdContentControlCheckBox Then .Checked = False
                If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""
                If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select
                If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select
                If .Type = wdContentControlDate Then .Range.Text = ""
              End With
            Next
          End With
          .Protect Type:=Prot, Password:=Pwd
        End With
      End If
      bLastCell = False
    End If
    End Sub
    End Sub

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Since the code works, as you can see from using it in the document in the link, what problem are you having using it? Did you try exiting the last content control on the last row?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    I did, and nothing happened.

  8. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Did you try it with the document attached to that link? If you did and 'nothing happened' that suggests either you disabled macros when you opened the document or your macro security is set so high it isn't even allowing macros to run.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    It worked it the document in the link, but did nothing when I copied it to my document. But I read up some and took some online vba courses and made a macro that does what I needed. Thank you for all your help though!

Posting Permissions

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