Curent FormField

  • Thread starter Francois Piette
  • Start date
F

Francois Piette

I have a Word document with a lot of form fields.
From a macro triggered by a keystroke, I would like to know the name of the
form field having the focus.

Thanks.
 
J

Jonathan West

Hi Francois

Selection.FormFields(1).Name should return the name of the formfield.
 
F

Francois Piette

I have a Word document with a lot of form fields.
Selection.FormFields(1).Name should return the name of the formfield.

Thank for your answer. Unfortunately, this doesn't work when the form is
protected. I get the error "The collection member doesn't exists"
(translated from french: I use a localized Word version). If the form is not
protected, then it works as expected but this is not what I need. The forms
are always protected to that the user can only access the fields.

To test, I used this code:

Sub ProtopSearch()
MsgBox (Selection.FormFields(1).Name)
End Sub

And associated the macro with a keyboard shortcut.

More ideas ?
 
F

Francois Piette

Maybe I should better explain what I want to do:
I want to provide a context sensitive and dynamic help to the user working
with Word forms. When entering data into a form, the user should have the
possibility to enter some value in any field, then enter a keyboard
short-cut mapped to a macro. This macro has to figure out which field is the
active one and pass the field name and field value to a COM object which
will query some information system, possibly poping up a new window, and
return some information to fill the field.

For example: the field is used to enter a "part name". The user enter a
partial part name in the field, then hit ALT-1 (or whatever needed to
trigger the macro I'm trying to write), he then see a new popup window
(built by an existing COM object) with all the part names starting with the
partial name he entered, then he select the part name he was looking for and
click OK. Then the popup window closes and the complete part name replace
the partial value he entered, the cursor pass to the next field and so on...
 
C

Cindy M -WordMVP-

Hi Francois,

There's an article on the word.mvps.org site that describes how to do this.
You need to get the bookmark name when the doc is protected. (A formfield name
is also a bookmark) Roughly:

Selection.Bookmarks(1).Name
Thank for your answer. Unfortunately, this doesn't work when the form is
protected. I get the error "The collection member doesn't exists"
(translated from french: I use a localized Word version). If the form is not
protected, then it works as expected but this is not what I need. T

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

Francois Piette

Hi Cindy,
Selection.Bookmarks(1).Name

This works better. But doesn't work when formfield is _not_ a bookmark.
A formfield name is also a bookmark

Not true. There are formfields which are not bookmarks.
I can mail you a document like that if you want to see it by yourself.
 
D

Doug Robbins - Word MVP

Sure you can delete the bookmark name from the formfield, but then the
formfield does not have a name and if you use VBA to return the name of the
formfield, an error message will result.

The only .Name that a formfield has is the name of the bookmark that is
assigned to it.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Cindy M -WordMVP-

Hi Francois,
This works better. But doesn't work when formfield is _not_ a bookmark.


Not true. There are formfields which are not bookmarks.
I can mail you a document like that if you want to see it by yourself.
In my experience, this can only happen if form fields are being inserted
using AutoText or Copy/Paste, and the form field names already exist in
the document. This is, as you're finding out, an extremely undesirable
state.

There's an article by "Ibby" on word.mvps.org that tells you how you can
give such form fields names (and thus bookmarks). I advise you to take
this approach and make sure all the form fields have valid names.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

Francois Piette

Not true. There are formfields which are not bookmarks.
In my experience, this can only happen if form fields are being inserted
using AutoText or Copy/Paste, and the form field names already exist in
the document.

No. Using Word2000, insert a text formfield, name it TextField1. Then use
menu/insert/bookmark to remove the bookmark. It remains a formfield with the
given name and no bookmark.

When working with both bookmarks and formfields, user like to have bookmark
shown and formfield not displayed with the bookmark brackets, without
counting the fact that formfields with bookmarks could make the bookmark
list annoyingly long for the user interested by the [true] bookmarks.
 
F

Francois Piette

Hi Doug,
Sure you can delete the bookmark name from the formfield, but then the
formfield does not have a name and if you use VBA to return the
name of the formfield, an error message will result.

Sorry but the formfield has a name independently of the bookmark. It just
happend that Word2000 (maybe later versions) create simultaneously both a
bookmark and a formfield with the same name as the bookmark.
The only .Name that a formfield has is the name of the bookmark
that is assigned to it.

Wrong. See the document at http://www.overbyte.be/arch/FormFieldsTest.rtf.
It contains two formfields, one with a bookmark and one without. Both have a
name.

--
(e-mail address removed)
Author of ICS (Internet Component Suite, freeware)
Author of MidWare (Multi-tier framework, freeware)
http://www.overbyte.be
 
C

Cindy M -WordMVP-

Hi Francois,
No. Using Word2000, insert a text formfield, name it TextField1. Then use
menu/insert/bookmark to remove the bookmark. It remains a formfield with the
given name and no bookmark.
OK, sure, get rid of the manually. But I never would...
When working with both bookmarks and formfields, user like to have bookmark
shown and formfield not displayed with the bookmark brackets, without
counting the fact that formfields with bookmarks could make the bookmark
list annoyingly long for the user interested by the [true] bookmarks.
I'm not sure I follow all your thinking... For one thing, why would a user
working in a form care about any bookmarks? They really can't be used in a
protected form?

In any case, to get back to your original question (as we run the danger of a
circular discussion, or one that digresses):

"I have a Word document with a lot of form fields.
From a macro triggered by a keystroke, I would like to know the name of the
form field having the focus."

Leave the bookmarks with the form fields. Just rename them (field and
bookmark) to begin with an underscore, so that they're "hidden". You have to
realize that forms were designed to work with the bookmarks in place, so
you're going to run into problems if you remove them.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

Francois Piette

I'm not sure I follow all your thinking... For one thing, why would a user
working in a form care about any bookmarks? They really can't be used in a
protected form?

Because the same document is used by different user. Some of them use the
document as a protected form, some other will use the document unprotected.
"I have a Word document with a lot of form fields.
From a macro triggered by a keystroke, I would like to know the name of the
form field having the focus."

Leave the bookmarks with the form fields. Just rename them (field and
bookmark) to begin with an underscore, so that they're "hidden".

I was not aware of such feature.
I tried, but this time Selection.Bookmarks(1).Name fails. So this is not a
solution either.
You have to realize that forms were designed to work with the
bookmarks in place, so you're going to run into problems
if you remove them.

Seems what I have to do. The problem is that there are already a lot of
forms designed without bookmarks and this is a lot of work to change that.

Anyway, thanks for you help. I learned a few things despite the fact I have
yet to find the solution for my initial problem.

--
(e-mail address removed)
Author of ICS (Internet Component Suite, freeware)
Author of MidWare (Multi-tier framework, freeware)
http://www.overbyte.be


Cindy M -WordMVP- said:
Hi Francois,
yourself.


No. Using Word2000, insert a text formfield, name it TextField1. Then use
menu/insert/bookmark to remove the bookmark. It remains a formfield with the
given name and no bookmark.
OK, sure, get rid of the manually. But I never would...
When working with both bookmarks and formfields, user like to have bookmark
shown and formfield not displayed with the bookmark brackets, without
counting the fact that formfields with bookmarks could make the bookmark
list annoyingly long for the user interested by the [true] bookmarks.
I'm not sure I follow all your thinking... For one thing, why would a user
working in a form care about any bookmarks? They really can't be used in a
protected form?

In any case, to get back to your original question (as we run the danger of a
circular discussion, or one that digresses):

"I have a Word document with a lot of form fields.
From a macro triggered by a keystroke, I would like to know the name of the
form field having the focus."

Leave the bookmarks with the form fields. Just rename them (field and
bookmark) to begin with an underscore, so that they're "hidden". You have to
realize that forms were designed to work with the bookmarks in place, so
you're going to run into problems if you remove them.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M -WordMVP-

Hi Francois,
I was not aware of such feature.
I tried, but this time Selection.Bookmarks(1).Name fails. So this is not a
solution either.
Funny, it works here. Are you certain you had forms protection turned back
on?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

Francois Piette

I tried, but this time Selection.Bookmarks(1).Name fails. So this is not
a
Funny, it works here. Are you certain you had forms protection turned back
on?

Yes. Just tried again. I confirm it doesn't work (Word2K-SP3)

Here after is a short macro I use. This macro is assigned to ALT-1. When
ALT-1 is used on a formfield with "_" prefix, Selection.Bookmarks.Count is
0. Without "_", Selection.Bookmarks.Count is 1.

Sub ProtopSearch()
Dim Protop As Object

If Selection.Bookmarks.Count > 0 Then
Set Protop = CreateObject("protopserver.protop")
Protop.FieldName = Selection.Bookmarks(1).Name
Protop.FieldValue =
Trim(ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Result)
Protop.Search
Set Protop = Nothing
End If
End Sub
 
J

Jonathan West

You need to include the following line at the start of your macro

ActiveDocument.Bookmarks.ShowHidden = True

otherwise the Bookmarks collection excludes hidden bookmarks whose name
begins with an underscore.

I'd recommend you also add this at the end to restore norml operation


ActiveDocument.Bookmarks.ShowHidden = False
 

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