Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 01-10-2017, 11:21 PM
gmayor's Avatar
gmayor gmayor is offline Trying to add rows to a protected document with form fields Windows 10 Trying to add rows to a protected document with form fields Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

See 'Add a row to a table in a protected form' at http://www.gmayor.com/word_vba_examples_2.htm The following code from that page should work with your table when added to the last field in the last row of the table in your document and protection applied. See the web page for versions for text and checkbox form fields.

Code:
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, j As Long
Dim sPassword As String
    sPassword = ""
    'password to protect/unprotect form
    With ActiveDocument
        .Unprotect Password:=sPassword
        'Unprotect document
        Set oTable = Selection.Tables(1)
        'Establish which table the cursor is in
        For j = 1 To .Tables.Count
            If oTable.Range.Start = .Tables(j).Range.Start Then
                'Table is j
                Exit For    'Stop checking
            End If
        Next j
        '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).DropDown.Value    '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 = "Tab" & j & "Col" & i & "Row" & CurRow    'eg Tab1Col1Row2
                .Execute    'apply the changes
            End With
        Next i
        'Select the formfield in the last cell of the previous row
        oLastCell.FormFields(1).ExitMacro = ""
        oLastCell.FormFields(1).DropDown.Value = sResult    'so restore the result of the cell
        .Protect NoReset:=True, _
                 Password:=sPassword, _
                 Type:=wdAllowOnlyFormFields
        'Reprotect the form
        .FormFields("Tab" & j & "Col1Row" _
                    & CurRow).Select    'and select the next field to be completed
    End With
lbl_Exit:
    Set oTable = Nothing
    Set oRng = Nothing
    Set oNewRow = Nothing
    Set oCell = Nothing
    Set oLastCell = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
macro, protected



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to add rows to a protected document with form fields Delete Rows in Protected Table with Form Fields Elan05 Word VBA 23 09-11-2014 12:47 PM
Spellcheck macro for protected form fields needed rharvey1215 Word 12 03-31-2014 06:47 PM
Add rows in protected table with Form Fields Apriljade Word 2 02-26-2014 06:42 AM
Inserting spreadsheet data rows as form fields in a document b3nz Word 3 03-31-2013 07:47 PM
Editing Password protected form fields in Word 2007 tamilan Word 2 02-16-2010 09:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:41 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft