PDA

View Full Version : Help: VBA(word) - Get the name/ID of the field selected on the document



sk_uc04
09-20-2007, 09:47 AM
Hello Everybody,

I am new to VBA (word).
I have a word document/form with multiple fields. I am trying to get the name/ID of the field selected (or where the cursor is). The field can be a text box or a radio button.
I would really appreciate any kind of help. Thanks in advance.

Best Regards,
SK

TonyJollans
09-20-2007, 12:27 PM
Radio Buttons are ActiveX controls (from the Control Toolbox) so you don't have a straightforward Word Form (which uses controls from the Forms Toolbar). So, one thing at a time ...

FormField Text Boxes can have code run on Entry and, if they have a bookmark you can pick up its name from Selection.Bookmarks(1).

ActiveX controls have various events where you can easily pick up their name.

Where / when do you want to find the name(s)?

sk_uc04
09-20-2007, 01:01 PM
Thanks TonyJollans for the reply. I am not sure what you mean by when I want to find the name.
On my word document, I have bound a key to each textbox on the form. When a user clicks on the textbox to enter text, based on the name of the textbox selected, a tooltip/help window comes (when user hit those keys for eg. Alt+F2).
I am not sure if I am clear enough... :(

TonyJollans
09-20-2007, 02:06 PM
What type of Textboxes are they? And how are you setting up the key? Or am I just being stupid tonight?

fumei
09-20-2007, 07:23 PM
Yes please. Especially when mentioning "textboxes" you must always tell us EXACTLY what kind of textbox, and where you are using them.

People mention "textbox", but do not mention they are on a userform.

People mention "textbox", but do not mention they are in the document.

People mention "textbox", but do not mention they are formfield textboxes, in the document.

People mention "textbox", but do not mention they are ActiveX textboxes, in the document.

People mention "textbox", but do not mention they are the textboxes made from Insert > Textbox.

These are different.

If you are using formfield textboxes, then they have a built-in Help message function. You can put your own text into them, and the user presses F1 to get it.

Formfield F1 Help is available for all the formfields, but you can make individual Help message for each one.

So....try again and tell us what is happening. For example, I am not sure what you mean by you "bound a key to each textbox". If they are ActiveX textboxes (in the document) you could use the some event to display a help message.

You are not being clear. So start with:

1. how did you make the textbox?
2. how did you "bind" a key?
3. why do you need the name anyway?

sk_uc04
09-21-2007, 11:37 AM
Thanks Gerry, for you help and comments. I am sorry for not being too clear about my post.
The textboxes are form fields text boxes on the document. I have written a sub in which I am using keybinding to bind Alt+F2 with another sub where I get the fieldName ID of the field selected.
I need the field ID/name because based on the fieldID/name, user should get the HELP text for that field when they hit Alt+F2. I am trying to create a dynamic HELP page which populates the information based on the field, Help is requested for. Since I have lots of fields, so I can't use the buit-in help function because on the same document few fields might have the same kind of help information.
I am not sure if thats the right approach of working on this problem. Any kind of suggestions are apprciated.
I have one more issue, I want to use F1 as the key to get the HELP info per each field, but somehow, F1 is not working on the current document (where as it does work for a test harness which I created). If anybody can help me with that... that will be great. Thanks for everybody for all their kind help!
I hope, I explained it well this time...

TonyJollans
09-21-2007, 12:29 PM
So did you try what I suggested in the first place?

Selection.Bookmarks(1)

sk_uc04
09-21-2007, 01:26 PM
Yes I did. Thanks Tony. I used the following:

Dim sL as Selection
Dim fName as String
Set sL = Selection
fName = sL.Bookmarks.Item(1).name

this gives me the name of the field selected