Selecting Form Fields

  • Thread starter Christopher Deel
  • Start date
C

Christopher Deel

I am having trouble selecting a form field using VBA code.

This is based on the article "The best way to select a form field
using VBA" on the MVP website, written by Mark Tangard.
http://word.mvps.org/FAQS/TblsFldsFms/GotoFormField.htm

He suggests using:
ActiveDocument.Bookmarks("Text4").Range.Fields(1).Result.Select

rather than:
ActiveDocument.FormFields("Text4").Range.Select
or
ActiveDocument.FormFields("Text4").Select
or
Selection.GoTo What:=wdGoToBookmark, Name:="Text4"

When I incorporated that line into my "FieldNav" macro (to alter the defalt
order in which form fields are tabbed into) everything _appeared_ to work
well (i.e. whole field turned dark, no screen "jumping", etc.)

But when I tried using the form, I found that the *first character* I typed
after moving to a form field with that macro *did not appear*. The text
only started appearing from the 2nd character typed in.

Did I do something wrong? Or is there no good solution for this?
Has anyone observed the same problem?
 
J

Jean-Guy Marcil

Christopher Deel was telling us:
Christopher Deel nous racontait que :
I am having trouble selecting a form field using VBA code.

This is based on the article "The best way to select a form field
using VBA" on the MVP website, written by Mark Tangard.
http://word.mvps.org/FAQS/TblsFldsFms/GotoFormField.htm

He suggests using:
ActiveDocument.Bookmarks("Text4").Range.Fields(1).Result.Select

rather than:
ActiveDocument.FormFields("Text4").Range.Select
or
ActiveDocument.FormFields("Text4").Select
or
Selection.GoTo What:=wdGoToBookmark, Name:="Text4"

When I incorporated that line into my "FieldNav" macro (to alter the
defalt order in which form fields are tabbed into) everything
_appeared_ to work well (i.e. whole field turned dark, no screen
"jumping", etc.)
But when I tried using the form, I found that the *first character* I
typed after moving to a form field with that macro *did not appear*. The
text only started appearing from the 2nd character typed in.

Did I do something wrong? Or is there no good solution for this?
Has anyone observed the same problem?

Probably! :)

The line you posted above
ActiveDocument.Bookmarks("Text4").Range.Fields(1).Result.Select
used on its own will not exhibit the behaviour you describe.

So, there must be something else going on, and since you did not post the
code you are using to navigate to the fields, it is a little bit difficult
to offer any help...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Christopher Deel

Apologies.
Here is my FieldNav macro:

--------------------------------
Sub FieldNav()
'
' Field Navigation Macro
' Macro created 2/13/2006 by Christopher Deel
' Based on example from Dian Chapman on TechTrax website
'
Dim strCurrBookmark As String

'determine what type of form field you're currently using
If Selection.FormFields.Count = 1 Then
'if field is not a textbox but a check- or listbox, capture that name
strCurrBookmark = Selection.FormFields(1).Name
'if selection is a text field, capture that name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
'add whatever the bookmark name is, to the designated variable
strCurrBookmark = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

Select Case strCurrBookmark
'if the cursor is currently located in the field that contains the
'bookmark name in the "case" statement, the lines below it will
'be enacted, thereby jumping to the field you've indicated.
Case "To_person"
' This one is selected differently because it is a drop-down box
ActiveDocument.FormFields("delivery_method").Select
Case "delivery_other"
' If the FAX fields are enabled, this code navigates "back up" the
page to them
If ActiveDocument.FormFields("fax_NumPages").Enabled = True Then
ActiveDocument.Bookmarks("fax_NumPages").Range.Fields(1).Result.Select
End If
Case "fax_NumPages"
ActiveDocument.Bookmarks("fax_Destination").Range.Fields(1).Result.Select
End Select

End Sub
-----------------------------------

This macro is fired as on OnExit macro ONLY from those fields that need to
have non-default tab behavior. And the only fields in which I observe the
"no-first-character" problem are the ones selected by this macro (i.e.
fax_NumPages and fax_Destination).

Thanks for any help you can offer!
Christopher Deel
 
C

Christopher Deel

Upon further experimentation, I think there is something else wrong with my
file, but the macro is not at fault. I was not able to reproduce the error
on another file.
Sorry; thanks for your help.
 
J

Jezebel

Your code is not doing anything like what your comments say --

'determine what type of form field you're currently using
If Selection.FormFields.Count = 1 Then
'if field is not a textbox but a check- or listbox, capture that name
strCurrBookmark = Selection.FormFields(1).Name

This is testing if there is exactly one form field in the selection. It's
not checking what type it might be.

'if selection is a text field, capture that name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0
Then

This is testing if there are NO form fields but at least one bookmark. Which
is not test for a text field.
 
J

Jay Freedman

Since Christopher found that the problem was something other than the
macro code, the thread is moot, but this side issue is worth
exploring.

The code does do what the comments say, because it works around a bug
in VBA. It's explained at
http://word.mvps.org/FAQS/TblsFldsFms/GetCurFmFldName.htm.

When you know that the Selection does contain a form field (because
the macro is the exit macro for fields in a protected form), and
you're trying to get the field's bookmark name, you'd think that the
expression

strCurrBookmark = Selection.FormFields(1).Name

would always work. It does work for a checkbox or dropdown form field,
but you get the bogus result Selection.FormFields.Count = 0 if the
Selection contains a text form field, and the expression
Selection.FormFields(1).Name causes an error.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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