Please explain S FF Ct

R

rob p

What is selection.formfields.count? I call this function from a macro on
exit from different checkboxes that are in different cells in a table. It is
supposted to tell me what cell it is. Seems that no matter which checkbox I
exit from - the first time, the count is 1. After that it is 0 and then I
get an error on the name. Here it is:

Function FormfieldName() As Variant
MsgBox "S FF CT " & Selection.FormFields.Count
If Selection.FormFields.Count = 1 Then
FormfieldName = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0
Then
FormfieldName = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
MsgBox FormfieldName
End Function

Thanks.
 
J

Jay Freedman

rob said:
What is selection.formfields.count? I call this function from a
macro on exit from different checkboxes that are in different cells
in a table. It is supposted to tell me what cell it is. Seems that no
matter which checkbox I exit from - the first time, the count is 1.
After that it is 0 and then I get an error on the name. Here it is:

Function FormfieldName() As Variant
MsgBox "S FF CT " & Selection.FormFields.Count
If Selection.FormFields.Count = 1 Then
FormfieldName = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count
FormfieldName =
Selection.Bookmarks(Selection.Bookmarks.Count).Name End If
MsgBox FormfieldName
End Function

Thanks.

Hi Rob,

I don't know whether you got that code directly from
http://word.mvps.org/FAQs/TblsFldsFms/GetCurFmFldName.htm or from a post in
the newsgroups, but I'm pretty sure that was the ultimate source.

This code is meant to work around a long-standing bug in VBA.

If the cursor is in any kind of form field *except* a text form field, then
VBA will tell you that Selection.FormFields.Count returns the value 1 --
that is, there is one form field in the Selection (or, more accurately, that
some part of one form field is in the selection, since the selection really
is inside the form field). Then VBA will let you use the expression
Selection.FormFields(1).Name. Formally the expression
Selection.FormFields(1) means "the first form field in the Selection" but we
already know that the Count is 1, so the first form field is the only form
field.

The bug is that when the Selection is in a text form field,
Selection.FormFields.Count returns the value 0. Worse than that, the
expression Selection.FormFields(1).Name causes a runtime error, because VBA
says there is no such thing as Selection.FormFields(1). It's wrong, but MS
has never fixed it.

The workaround is to check the number of bookmarks in the selection. Since
this is an exit macro in a protected form, the selection must be in some
kind of form field. The bookmark name associated with that form field will
be Selection.Bookmarks(Selection.Bookmarks.Count).Name.

There are probably a few pathological situations where this logic would
fail, but not if your template is properly constructed.
 

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