Calling Cindy Meister

L

Leonard

Hello Ms. Cindy Meister. When I ran your following macro (via a MACROBUTTON)
in a protected form and I typed a search phrase, I got a Microsoft Visual
Basic error message that said "Run-time error '5941'; The requested member of
the collection does not exist." When I clicked Debug, the line "Set ffld
= doc.FormFields(Selection.Bookmarks(1).Name)" within the macro was hi-lited
in yellow.

Ms. Meister, why was I doing wrong?
_______________________

Sub FindInForm()
Dim doc As Word.Document, ffld As Word.FormField
Dim szFindTerm As String, lPos As Long
Dim szSearchString As String, rng As Word.Range
Dim rngStart As Word.Range, lNumFfld As Long
Dim lFfldCounter As Long

Set doc = ActiveDocument
szFindTerm = InputBox("Enter the phrase you wish to find:")
lNumFfld = doc.FormFields.Count
lFfldCounter = 0
Set ffld = doc.FormFields(Selection.Bookmarks(1).Name)
If Len(szFindTerm) = 0 Then Exit Sub
Set rngStart = Selection.Range
Set rng = rngStart.Duplicate
rng.End = ffld.Range.End
szSearchString = rng.Text
Debug.Print szSearchString
lPos = InStr(ffld.Result, szFindTerm)
If lPos > 0 Then
Selection.MoveStart wdCharacter, lPos - 1
Selection.MoveEnd wdCharacter, Len(szFindTerm)
Exit Sub
End If
Do
Set ffld = ffld.Next
If ffld Is Nothing Then
Set ffld = ActiveDocument.FormFields(1)
End If
If rngStart.InRange(ffld.Range) Then
MsgBox "All form fields were searched; the phrase was not found."
Exit Sub
End If
ffld.Select
Selection.Collapse
Set rng = Selection.Range
rng.End = ffld.Range.End
szSearchString = rng.Text
Debug.Print szSearchString
lPos = InStr(ffld.Result, szFindTerm)
If lPos > 0 Then
Selection.MoveStart wdCharacter, lPos - 1
Selection.MoveEnd wdCharacter, Len(szFindTerm)
Exit Sub
End If
lFfldCounter = lFfldCounter + 1
Loop Until lFfldCounter = lNumFfld
End Sub
 
L

Leonard

Anne, thanks for your contribution which I have always appreciated in the
past. Hmmmm...I used the Form toolbar to create all of the text fields,
therefore a bookmark was automatically assigned by Word 2002 to each of the
text fields. I could recheck wach text field to see if any of them does not
have a bookmark assigned to it. So, it's possible Cindy's macro stopped
running because all text fields have to have a bookmark assigned to it in
order for a user to perform a word/phrase find?

I had thought that I could not protect the form with a password or that I
had to use beginning and ending quotatioj marks to surround the word or
phrase, but I still got the same error message.

What was I doing wrong? I would love to have this macro run properly for my
customers would love to be able to conduct a Find on their completed form
fields.
 
J

Jay Freedman

Hi Leonard,

This is, like Anne's reply, just a guess...

Yes, Cindy's macro assumes that every form field has an associated
bookmark. Normally, when you create a form field, Word automatically
assigns a bookmark to serve as its name.

If you used the button on the Forms toolbar to create one form field,
but then you copied that field and pasted to make more copies of it,
the copies won't have bookmarks associated with them. This is a side
effect of the fact that Word allows only one occurrence of any
bookmark name per document. As you do each paste, that field grabs the
bookmark and the older ones are left nameless.

Look in the Properties dialog of each form field. If the Bookmark box
is blank, enter a unique name. Then reprotect the document and try the
macro.
 

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