Thanks, Jay!
I've created a Macro named "AddRow" and included the code below
however, when I exit the field in the last cell of the row the macro
doesn't execute.
Again, I'm rather new to the world of Word macros and I can't see
where I've gone wrong. Any suggestions you might have would be
greatly appreciated.
Thanks!
Hi John,
It's easier to answer question 2 first -- yes, use Tools > Macro >
VBE (or the shortcut Alt+F11). You'll also need to create a module
to hold the code. Full instructions are at
http://www.gmayor.com/installing_macro.htm.
For question 1: No, Stephanie's code inserts only five form fields
in each new row. You could add another five segments of code to
fill the remaining five columns, but that would be tedious, not to
mention making the macro that much harder to update if your
requirements change in the future. I recommend the following
version, which figures out how many times to repeat the action by
asking how many columns the table has. [Note that this version will
generate an error if the table contains any merged or split cells.
I assume it's all uniform.]
Sub addrow()
Dim response As Integer
Dim myRow As Long
Dim myCount As Long
Dim colCount As Long
Dim colIndex As Long
Dim myNewField As String
response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect
colCount = Selection.Tables(1).Columns.Count
Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)
For colIndex = 1 To colCount - 1
Selection.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text" & colIndex & "row" & myRow
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Next colIndex
Selection.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Name = "text" & colCount & "row" & myRow
.Enabled = True
.ExitMacro = "addrow"
End With
myNewField = "text1row" & myRow
ActiveDocument.Protect NoReset:=True, _
Type:=wdAllowOnlyFormFields
ActiveDocument.Range.FormFields(myNewField).Select
End If
End Sub
A couple of other comments:
- You may decide that the prompt "Add new row?" is unnecessary. If
so, remove the MsgBox line, the If statement that follows it, and
the End If at the end of the macro.
- The Dim statements at the start of the code are "optional" but
highly recommended; see
http://word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm.
- For a macro that might be used in other templates, there should
be some error-trapping. At a minimum, the macro should start by
making sure that the Selection is currently in a table, and not in
a form field elsewhere in the document (it shouldn't be, since
this is an exit macro, but you never know whether the next
template obeys the unwritten rules). The statements
If Not Selection.Information(wdWithInTable) Then
Exit Sub
End If
when placed right after the Dim statements will prevent such
errors.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
JohnLute wrote:
Hi, Stephanie.
I can't believe I found such an informative thread on my first
search! I have a couple questions as I'm entirely unfamiliar with
Word's macro functions (I'm more familiar with Access and I
suppose there are some similarities).
1. My table has 10 columns. Will this code still work?
2. How do I create this code in Word? I'm assuming it's Tools >
Macro
VBE.
Thanks so much!