PDA

View Full Version : Solved: Naming Formfields Within A Table



petedw
06-22-2005, 07:58 AM
Hi Guys,

I have a Macro that creates a table and then inserts a Formfield into each Cell.
I also require my macro to name each formfield (happy1, happy2 etc)
At the moment, it only names the Formfields in Column 1 and leaves the remaining ones without a name. :banghead:
Please take a look at my code and see if you can fix this for me.

Sub maketable()
Dim tbl As Table, rng As Range, titlerow As Row, FFName As Integer
Dim cCells As Integer, cColumns As Integer, cRows As Integer, counter As Integer
cColumns = 4
FFName = 1

'Dialog box for number of payments
cCells = Val(InputBox("Enter number of payments"))
cCells = cCells * 2
If cCells = 0 Then Exit Sub
cRows = Fix(cCells / cColumns)
If (cCells Mod cColumns) <> 0 Then cRows = cRows + 1

'Draws Table
Set rng = ActiveDocument.Bookmarks("PmtTable").Range
rng.Collapse direction:=wdCollapseEnd
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("PmtTable").Select
Set tbl = Selection.Tables.Add(rng, cRows, cColumns)

'fills table


For ffcolloop = 1 To cColumns
'Date Columns
For FFRowLoop = 1 To cRows
tbl.Cell(FFRowLoop, ffcolloop).Select
Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput
Selection.PreviousField.Select
'If FFName = 1 Then
With Selection.FormFields("text1")
.Name = "Happy" & FFName
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.OwnHelp = False
.HelpText = ""
.OwnStatus = False
.StatusText = ""
End With


FFName = FFName + 1


Next ffcolloop

ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
End Sub

Many Thanks

Pete

Killian
06-22-2005, 08:25 AM
Hi Pete,

You haven't closed your second For...Next loop (For FFRowLoop = 1 To cRows)
Otherwise, it seems to work fine

petedw
06-22-2005, 08:33 AM
Sorry Killian,
That was an error on my part. I edited some text out of my coding before i posted it on here.

If i insert the line ....... Next FFRowLoop after the line..... FFName = FFName + 1 then it still only labels the 1st column and doesnt bother with the other formfields

fumei
06-22-2005, 08:33 AM
Hi Pete.

Could you please use the _ character in your code to break your lines? Having the codelines so long makes it difficult to read, as you have to scroll left and and right. For example:

Selection.FormFields.Add _
Range:=Selection.Range, Type:=wdFieldFormTextInput

OK. This may help a bit.
Sub MakeFFInTable()
Dim iRows As Integer
Dim iCols As Integer
Dim FFCounter As Integer
Dim NumberOfCells As Integer
Dim var
FFCounter = 1
iRows = ActiveDocument.Tables(1).Rows.Count
iCols = ActiveDocument.Tables(1).Columns.Count
NumberOfCells = iRows * iCols

ActiveDocument.Tables(1).Cell(iRows - (iRows - 1), _
iCols - (iCols - 1)).Select
' this fills each cell with a formfield
' EXCEPT for the last cell
' the .MoveRight would otherwise make a new row
For var = 1 To NumberOfCells - 1
With Selection
.FormFields.Add Range:=Selection.Range, _
Type:= wdFieldFormTextInput
.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
.FormFields(1).Name = "Happy" & FFCounter
.MoveRight Unit:=wdCell
FFCounter = FFCounter + 1
End With
Next
' do the last cell separately
With Selection
.FormFields.Add Range:=Selection.Range, _
Type:= wdFieldFormTextInput
.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
.FormFields(1).Name = "Happy" & FFCounter
End With
End Sub

This fills up Table(1), but you can easily modify it so that it actions the table the selection is in, OR go to a table to action, OR make a table to action.

petedw
06-22-2005, 08:45 AM
Thanks Gerry,

Thats spot on!:thumb