FormFields in a table Strange bookmark naming problems

R

Robert S.

I am working with Word 2002.

I have a table (2 columns wide), that has two empty
FormFields. When a user enters information in one of the
form fields, an exit macro is run, and a new row in the
table is created with new empty FormFields.

This all works fine. The problem is that I want Word to
give the newly created FormFields a bookmark name of my
choice.

In this 2 column table, two FormFields are created when
the exit macro is run, but only the FormField on the left
column has the bookmark applied to it. The bookmark for
the FormField in the right column remains blank.

HOWEVER, if I try to name the bookmark in the right column
first, and then the one in the left column second (see
code below), both than have their bookmarks names applied.
The only difference is the order in the code. For ex.

===============================================
Sub addrow()

Dim rownum As Integer
ActiveDocument.Unprotect
ActiveDocument.Tables(5).Rows.Add
rownum = ActiveDocument.Tables(5).Rows.Count

ActiveDocument.FormFields.Add Range:=ActiveDocument.Tables
(5).Cell(rownum, 2).Range, Type:=wdFieldFormTextInput

'BOOKMARK BEING APPLIED
ActiveDocument.Tables(5).Cell(rownum, 2).Range.FormFields
(1).Name = "ForeignEmployees" & rownum

ActiveDocument.FormFields.Add Range:=ActiveDocument.Tables
(5).Cell(rownum, 1).Range, Type:=wdFieldFormTextInput

' BOOKMARK BEING APPLIED
ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).Name = "ForeignCountries" & rownum

ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).ExitMacro = "addrow"

ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True

End Sub

===========================================

The above code works, however if I switch order in which
the FormFields are named, only the left column FormField
is named, and the right one is blank.

===========================================

Sub addrow()

Dim rownum As Integer
ActiveDocument.Unprotect
ActiveDocument.Tables(5).Rows.Add
rownum = ActiveDocument.Tables(5).Rows.Count

ActiveDocument.FormFields.Add Range:=ActiveDocument.Tables
(5).Cell(rownum, 1).Range, Type:=wdFieldFormTextInput
' BOOKMARK BEING APPLIED TO LEFT COLUMN
ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).Name = "ForeignCountries" & rownum

ActiveDocument.FormFields.Add Range:=ActiveDocument.Tables
(5).Cell(rownum, 2).Range, Type:=wdFieldFormTextInput
'BOOKMARK BEING APPLIED TO RIGHT COLUMN
ActiveDocument.Tables(5).Cell(rownum, 2).Range.FormFields
(1).Name = "ForeignEmployees" & rownum

ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).ExitMacro = "addrow"

ActiveDocument.Tables(5).Cell(rownum, 1).Range.FormFields
(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True

End Sub
========================================
The above does not work correctly.

So, while the first subroutine was able to make the
program correctly, the quirkiness of this solution leaves
me confused. My problem now, is that I also have a table
with 3 columns that needs to work the same way, but no
matter which order I name the FormFields in the new row, I
can never have the name applied to the FormField in the
rightmost column.

I am guessing that the solution lies somewhere in the way
the bookmark name is applied. I hope someone can shed some
light on this situation. Thanks.

-Robert
 
R

Robert S.

Thanks Harold,

Works great now.

Robert
-----Original Message-----
Hi Robert,
This would appear to be a bug in Word 2002. I will report it as such. I
found the behavior doesn't reproduce in Word 2003 beta.

I worked out a workaround. It appears cumbersome but it does work.
I used a table with 4 columns. You can modify the code to meet your table
size. I added the Exit macro, but I think I would prefer to use a button to
add the row. With the exit macro everytime the user moves off the cell it
will create a new row.
I would either add a button to a menu bar or place it on the document.
Anyway here's my sample code:
Sub AddRow()
'WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS EXAMPLE IS
'AT YOUR OWN RISK. Microsoft provides this macro code "as is" without
warranty of
'any kind, either express or implied, including but not limited to the
implied warranties of
'merchantability and/or fitness for a particular purpose.

Dim nRow As Integer
Dim x As Integer
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
'add a row to the table
ActiveDocument.Tables(1).Rows.Add
nRow = ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Rows(nRow).Cells(1).Select
'set this value for the number of columns in the table
For x = 1 To 4
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveLeft unit:=wdCharacter, Count:=4
If x <> 4 Then Selection.MoveRight unit:=wdCell
Next x
renameField
ActiveDocument.Tables(1).Cell(nRow, 4).Range.FormFields (1).ExitMacro =
"AddRow"

ActiveDocument.Protect wdAllowOnlyFormFields, True
End Sub
Sub renameField()
Dim nRow As Integer
Dim myCell As Cell
Dim x As Integer
Dim myColIndex As Integer
nRow = ActiveDocument.Tables(1).Rows.Count
'loop the cells in last row
For Each myCell In ActiveDocument.Tables(1).Rows (nRow).Cells
myColIndex = myCell.ColumnIndex
'by default the cell name is Text+number. When the condition is true
'delete the formfield and readd it.

If Left(myCell.Range.FormFields(1).Name, 4) = "Text" Then
myCell.Range.FormFields(1).Select
myCell.Range.FormFields(1).Delete
Selection.FormFields.Add Selection.Range, wdFieldFormTextInput
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
Select Case myColIndex
Case 1
Selection.FormFields(1).Name = "ForeignName" & nRow
Case 2
Selection.FormFields(1).Name = "ForeignAddress" & nRow
Case 3
Selection.FormFields(1).Name = "ForeignPerson" & nRow
Case 4
Selection.FormFields(1).Name = "Foreigner" & nRow
End Select

x = x + 1
End If
Next myCell

End Sub

Hope it works for you.

Harold Kless, MCSD
Support Professional
Microsoft Technical Support for Business Applications
(e-mail address removed)

--


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top