form field loses name property

H

Hilary

Hi.

I have a Word doc with a form field. The form field is
populated with a list of strings.

Here is the problem. When the doc is created in Word 2000
and run in either Word 2000 or 2003, the form field
populates correctly. When the doc is created in Word 2003
and run in either Word 2000 or 2003, after the routine is
called more than twice, the form field loses its name and
the text is populated out of order.

Here is the code that is used in the Word VBA program and
is very similar to the one used in the VB executable. This
works using Word 2003 VBA, no matter what version the
document was created in.

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse
As String)
If ActiveDocument.FormFields(BookmarkToUpdate).Result
= "" Then
ActiveDocument.FormFields(BookmarkToUpdate).Result
= TextToUse
Else
strOldData = ActiveDocument.FormFields
(BookmarkToUpdate).Result
ActiveDocument.Bookmarks(BookmarkToUpdate).Select

Selection.InsertBefore strOldData
ActiveDocument.FormFields(BookmarkToUpdate).Result
= TextToUse
End If
End Sub
Private Sub CommandButton1_Click()
For x = 1 To 10
UpdateBookmark "Text1", CStr(x) & Chr(11)
Next
End Sub

Any advice? I know the obvious is to switch to 2000, but
we can't do that.

Thanks!

Hilary
 
P

Peter Hewett

Hi Hilary

I haven't been able to try this is I don't have Office 2003 installed yet. But try it
this way instead:

Sub UpdateBookmark(ByVal BookmarkToUpdate As String, _
ByVal TextToUse As String)
Dim ffItem As Word.FormField
Dim strOldData

Set ffItem = ActiveDocument.FormFields(BookmarkToUpdate)
If ffItem.Result = vbNullString Then
ffItem.Result = TextToUse
Else
strOldData = ffItem.Result
ActiveDocument.FormFields(BookmarkToUpdate).Result = strOldData & TextToUse
End If
End Sub
Public Sub CommandButton1_Click()
Dim lngX As Long
For lngX = 1 To 10
UpdateBookmark "Text1", CStr(lngX) & Chr(11)
Next
End Sub

HTH + Cheers - Peter
 

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