VBA: Returning the index of a formfield?

How do I return the index of a selected formfield in VBA?
SteveWAsked:
Who is Participating?
 
tureCommented:
SteveW,

This code shows the name and index of the currently selected form field. I had to use a loop to run through all FormFields, because I found no 'ActiveFormField' or similar way to determine which field is currently selected.

Sub ShowIndexAndNameOfSelectedFormField()
  Dim lngSelStart As Long
  Dim lngSelEnd As Long
  Dim lngIndex As Long
  Dim ff As FormField
 
  lngSelStart = Selection.Start
  lngSelEnd = Selection.End
 
  For lngIndex = 1 To ActiveDocument.FormFields.Count
    Set ff = ActiveDocument.FormFields(lngIndex)
    If ff.Range.Start <= lngSelStart And ff.Range.End >= lngSelEnd Then
      MsgBox Title:="Index", Prompt:=lngIndex
      MsgBox Title:="Name", Prompt:=ff.Name
    End If
  Next lngIndex
End Sub

Ture Magnusson
Karlstad, Sweden
0
 
NoggyCommented:
Selection.FormFields(i).Name will return the name of the i-th Form Field in the selection (relative to the selection too - and not the document).

I presume that you will want the index of the Form Field with respect to the start of the document?
0
 
NoggyCommented:
SteveW, here's a little function that I knocked up for you. It will return the index number of the FIRST form field that it finds in a range (relative to the start of the document). If there are no form fields, it will return 0.

Public Function FormFieldIndex(ByVal rng As Range) As Long
    Dim fldName As String
    Dim lCount As Long
   
    With rng
        If .FormFields.Count = 0 Then
            FormFieldIndex = 0
            Exit Function
        End If
   
        fldName = .FormFields(1).Name
    End With
     
    With ThisDocument
        For lCount = 1 To .FormFields.Count
            If .FormFields(lCount).Name = fldName Then
                FormFieldIndex = lCount
                Exit Function
            End If
        Next lCount
    End With
End Function

So, to use it with a Selection class, you will need to call the function in your VB code with: FormFieldIndex(Selection.Range)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
NoggyCommented:
Ture - He, he :-) I beat you to it this time....
0
 
tureCommented:
Noggy...
Yes, you were quicker :0(
But not better :o)

Your function will not return the index number of the currently selected form field.

If I set the 'Run macro on entry' for the form fields to 'Test' (which uses your function), it will always return 0.
If I set 'Run macro on entry' to my procedure (ShowIndex), it will correctly show the index of the form field when the cursor moves to it.

Sub Test()
  MsgBox FormFieldIndex(Selection.Range)
End Sub

Public Function FormFieldIndex(ByVal rng As Range) As Long
    Dim fldName As String
    Dim lCount As Long
     
    With rng
        If .FormFields.Count = 0 Then
            FormFieldIndex = 0
            Exit Function
        End If
     
        fldName = .FormFields(1).Name
    End With
       
    With ThisDocument
        For lCount = 1 To .FormFields.Count
            If .FormFields(lCount).Name = fldName Then
                FormFieldIndex = lCount
                Exit Function
            End If
        Next lCount
    End With
End Function


Sub ShowIndex()
  Dim lngSelStart As Long
  Dim lngSelEnd As Long
  Dim lngIndex As Long
  Dim ff As FormField
   
  lngSelStart = Selection.Start
  lngSelEnd = Selection.End
 
  For lngIndex = 1 To ActiveDocument.FormFields.Count
    Set ff = ActiveDocument.FormFields(lngIndex)
    If ff.Range.Start <= lngSelStart And ff.Range.End >= lngSelEnd Then
      MsgBox Title:="Index", Prompt:=lngIndex
    End If
  Next lngIndex
End Sub

/Ture
0
 
tureCommented:
SteveW,
Please don't forget to grade this question or ask again if the posted suggestions didn't help.
/Ture
0
 
NoggyCommented:
Ture - Aaah, I was assuming that the complete field would be in the selection. Yours indeed is a bit better than mine in that you can have the selection as only part of the form field. It all depends on what SteveW means by "selected formfield" in his q.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.