View Single Post
  #1   Report Post  
Leonard
 
Posts: n/a
Default Calling Cindy Meister

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