A
AltNrg4U
First off I would like to thank all of the people that post, and all
the people that respond. I have lurked for a long time, dependent on
the Q&A of others.
Today I have spent 12 hours reviewing and steeling snippets from all
over the web. I have my table set up to add a row at the end of my
table on my protected form, and to add a text form field to 5 of the 7
cells, and also add & populate 2 separate dropdown lists. This works
and I could stop here.
That being said, my table can be updated with only several rows or up
to 5 full pages. I would like to tweak my VBA code to request some
sort of input and it would populate that many rows. I am guessing that
the best answer would be to loop my current code the number of times
the user input. If so, I am guessing it should be fairly easy addition
for the loop. Lastly, I am posting because I am sure there are other
people that may / will be looking for a similar solution.
I am enclosing my code below in case this information is needed to
answer my question. I still have to update my comments, sorry.
Sub AddRow2()
Dim oTbl As Table
Set oTbl = Selection.Tables(1)
response = MsgBox("Would you like to Add a new row?", vbQuestion +
vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect
' Disable the AddRow macro that is currently in the last cell
' (since we're creating a new last cell)
Selection.Tables(1).Cell(Selection.Tables(1).Rows.Count,
Selection.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro = ""
Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
'Selection.SelectCell
Selection.Bookmarks.Add Name:="a1temp", Range:=Selection.Cells(1)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text1row" & myRow
.Enabled = True
End With
Selection.Bookmarks.Add Name:="a1temp", Range:=Selection.Range
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text2row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Set myField3 = Selection.FormFields.Add(Range:=Selection.Range,
Type:=wdFieldFormDropDown)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text3row" & myRow
.Enabled = True
With myField3.DropDown.ListEntries
.Add Name:=" "
.Add Name:=" Files "
.Add Name:=" Object "
.Add Name:=" Report "
End With
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text4row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Set myField5 = Selection.FormFields.Add(Range:=Selection.Range,
Type:=wdFieldFormDropDown)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text5row" & myRow
.Enabled = True
With myField5.DropDown.ListEntries
.Add Name:=" "
.Add Name:=" Promote "
.Add Name:=" Restore "
.Add Name:=" Delete "
End With
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text6row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text7row" & myRow
.Enabled = True
' .ExitMacro = "AddRow2"
End With
oTbl.Cell(oTbl.Rows.Count,
oTbl.Columns.Count).Range.FormFields(1).ExitMacro = "AddRow2"
oTbl.Cell(oTbl.Rows.Count, 1).Range.FormFields(1).Select
ActiveDocument.Protect NoReset:=True, Type:=wdAllowOnlyFormFields
End If
End Sub
the people that respond. I have lurked for a long time, dependent on
the Q&A of others.
Today I have spent 12 hours reviewing and steeling snippets from all
over the web. I have my table set up to add a row at the end of my
table on my protected form, and to add a text form field to 5 of the 7
cells, and also add & populate 2 separate dropdown lists. This works
and I could stop here.
That being said, my table can be updated with only several rows or up
to 5 full pages. I would like to tweak my VBA code to request some
sort of input and it would populate that many rows. I am guessing that
the best answer would be to loop my current code the number of times
the user input. If so, I am guessing it should be fairly easy addition
for the loop. Lastly, I am posting because I am sure there are other
people that may / will be looking for a similar solution.
I am enclosing my code below in case this information is needed to
answer my question. I still have to update my comments, sorry.
Sub AddRow2()
Dim oTbl As Table
Set oTbl = Selection.Tables(1)
response = MsgBox("Would you like to Add a new row?", vbQuestion +
vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect
' Disable the AddRow macro that is currently in the last cell
' (since we're creating a new last cell)
Selection.Tables(1).Cell(Selection.Tables(1).Rows.Count,
Selection.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro = ""
Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
'Selection.SelectCell
Selection.Bookmarks.Add Name:="a1temp", Range:=Selection.Cells(1)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text1row" & myRow
.Enabled = True
End With
Selection.Bookmarks.Add Name:="a1temp", Range:=Selection.Range
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text2row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Set myField3 = Selection.FormFields.Add(Range:=Selection.Range,
Type:=wdFieldFormDropDown)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text3row" & myRow
.Enabled = True
With myField3.DropDown.ListEntries
.Add Name:=" "
.Add Name:=" Files "
.Add Name:=" Object "
.Add Name:=" Report "
End With
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text4row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Set myField5 = Selection.FormFields.Add(Range:=Selection.Range,
Type:=wdFieldFormDropDown)
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text5row" & myRow
.Enabled = True
With myField5.DropDown.ListEntries
.Add Name:=" "
.Add Name:=" Promote "
.Add Name:=" Restore "
.Add Name:=" Delete "
End With
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text6row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text7row" & myRow
.Enabled = True
' .ExitMacro = "AddRow2"
End With
oTbl.Cell(oTbl.Rows.Count,
oTbl.Columns.Count).Range.FormFields(1).ExitMacro = "AddRow2"
oTbl.Cell(oTbl.Rows.Count, 1).Range.FormFields(1).Select
ActiveDocument.Protect NoReset:=True, Type:=wdAllowOnlyFormFields
End If
End Sub