Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to add rows to table in locked-down form

Status
Not open for further replies.

rdudejr

Programmer
May 26, 2009
1
US
All,

I need some help with a macro to add rows to a table in a locked down form. I already have a macro that works for 90% of the functionality. I am using word 2003

The desired behaviour is when the last row is tabbed through (or entered and exited), a new row is added, with all the same pre-defined dropdowns as the preceeding row. The macro I have only adds a row, but does not copy the drop downs in the table as well or save any of the cell attributes (max char, char type, ect.)

I also want to know why when I sent this document to a colleague of mine, the macro no longer worked. I sent the .doc file..is that the wrong thing to send?

Below is my code (ive spaced each line so you can clearly see line breaks)

Sub addRow()

Dim rownum As Long, i As Long

With ActiveDocument

.Unprotect

With Selection.Tables(1)

.Rows.Add

rownum = .Rows.Count

For i = 1 To .Columns.Count

ActiveDocument.FormFields.Add Range:=.Cell(rownum, i).Range, Type:=wdFieldFormTextInput

Next i

.Cell(.Rows.Count, .Columns.Count).Range.FormFields(1).ExitMacro = "addRow"

.Cell(.Rows.Count, 1).Range.FormFields(1).Select

End With

.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End With

End Sub


 
1. "Below is my code (ive spaced each line so you can clearly see line breaks)"

It would be better to use the TGML code tags when posting code. Here is a copy of your code (without spaces, and using the underscore character to make it easier to read line breaks).
Code:
Sub addRow()
Dim rownum As Long, i As Long

With ActiveDocument
   .Unprotect
   With Selection.Tables(1)
      .Rows.Add
      rownum = .Rows.Count
      For i = 1 To .Columns.Count
         ActiveDocument.FormFields.Add _
            Range:=.Cell(rownum, i).Range, _
            Type:=wdFieldFormTextInput
      Next i
      .Cell(.Rows.Count, .Columns.Count).Range _
         .FormFields(1).ExitMacro = "addRow"
      .Cell(.Rows.Count, 1).Range.FormFields(1).Select
   End With
   .Protect Type:=wdAllowOnlyFormFields, _
         NoReset:=True
End With
End Sub

2. Regarding sending the .doc file and the code not working, there could be a couple of reasons. What is the security level used by the other person? Is the procedure actually in the doc file?

3. "The desired behaviour is when the last row is tabbed through (or entered and exited), a new row is added, with all the same pre-defined dropdowns as the preceeding row. The macro I have only adds a row, but does not copy the drop downs in the table "

Your code does not have any dropdowns. You are making text formfields.
Code:
         ActiveDocument.FormFields.Add _
            Range:=.Cell(rownum, i).Range, _
            Type:=[b]wdFieldFormTextInput[/b]
There is no such thing as "pre-defined dropdowns". If you add a dropdown formfield (which you do not), then you need to populate it. If the items in your previous dropdowns are standard, then I would:

1. declare and use a formfield object to make your new dropdown formfields.

2. call a separate procedure to populate it. First of all, it is easier to work with objects, and second - if it IS a standard list of items in the dropdown - best-practice is to keep that action as a separate procedure. It makes debugging MUCH easier.

Something like:
Code:
Option Explicit

Sub addRow()
Dim rownum As Long, i As Long
[b]Dim r As Range[/b]
With ActiveDocument
   .Unprotect
   With Selection.Tables(1)
      .Rows.Add
      rownum = .Rows.Count
      For i = 1 To .Columns.Count
         [COLOR=red]'  set range object for each cell
         '  and pass it to the MakeNewFF procedure [/color red]
         [b]Set r = .Cell(rownum, i).Range
         Call MakeNewFF(r)[/b]
      Next i
      .Cell(.Rows.Count, .Columns.Count).Range _
         .FormFields(1).ExitMacro = "addRow"
      .Cell(.Rows.Count, 1).Range.FormFields(1).Select
   End With
   .Protect Type:=wdAllowOnlyFormFields, _
         NoReset:=True
End With
End Sub

Sub MakeNewFF(r As Range)
Dim myItems()
Dim j As Long
Dim oFF As FormField
[COLOR=red]'  collapsing the range is VERY IMPORTANT![/color red]
r.Collapse 1
myItems = Array("Item1", "Item2", _
   "Item3", "Item4")

   Set oFF = ActiveDocument.FormFields.Add(Range:=r, _
         Type:=wdFieldFormDropDown)
   With oFF
      For j = 0 To UBound(myItems)
         .DropDown.ListEntries.Add myItems(j)
      Next j
   End With
End Sub
Note that because the code uses a formfield object the parameters used to Set and .Add must be in parenthesis.

The above code:

1. makes a new row
2. set a range object for each cell in row
3. passes the range to the formfield creating procedure
4. creates a dropdown formfield
5. populates the dropdowns with identical dropdown items
6. adds the exit macro to the last formfield in the new row


Hope this helps.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top