Word 2002/2003 ActiveX Control Name property

H

Hobs

I've added 4 form controls on to a Word 2002 SP3 document 2 Label(s) and 2
CommandButton(s) and assigned them names. I have then coded some logic on
the command button to return the text of label 1 and 2 respectively.

This works fine but when I open the document up in Word 2003 SP3, save it
and reopen the document in Word 2002 the 'Name' property has been randomly
transposed to different items so that what use to be called 'label1' is now
attached to a CommandButton and a CommandButton will have the name 'label2',
etc.

I have researched as much as possible but have found very little on this
topic. One URL
http://msdn.microsoft.com/en-us/library/aa140269(office.10).aspx says

'The VBA properties of ActiveX controls are not always stable. For example,
the Name property, given by Word upon insertion of the control, may change
unexpectedly. Again, later versions of Word are more stable than earlier
ones, especially Word 97. '

If this is the case how can I uniquely identify the controls on my document
if I can't rely on the 'Name' field being immutable?
 
J

Jean-Guy Marcil

Hobs said:
I've added 4 form controls on to a Word 2002 SP3 document 2 Label(s) and 2
CommandButton(s) and assigned them names. I have then coded some logic on
the command button to return the text of label 1 and 2 respectively.

This works fine but when I open the document up in Word 2003 SP3, save it
and reopen the document in Word 2002 the 'Name' property has been randomly
transposed to different items so that what use to be called 'label1' is now
attached to a CommandButton and a CommandButton will have the name 'label2',
etc.

I have researched as much as possible but have found very little on this
topic. One URL
http://msdn.microsoft.com/en-us/library/aa140269(office.10).aspx says

'The VBA properties of ActiveX controls are not always stable. For example,
the Name property, given by Word upon insertion of the control, may change
unexpectedly. Again, later versions of Word are more stable than earlier
ones, especially Word 97. '

If this is the case how can I uniquely identify the controls on my document
if I can't rely on the 'Name' field being immutable?

You can encase them in bookmarks and use the Bookmark(Index).Range to access
the control within...
 
H

Hobs

I've set the bookmark to the entire document by Ctrl-A and then inserting a
bookmark. It seems to indicate that the BookMark object is correct because
the start and end properties show 0 and 201 respectively (the end of the
document). Inspecting the Range property of the bookmark I cannot see the
ActiveX controls, I've looked at the Range interface and nothing seems to
expose it other than FormFields property but this count is 0.

Can you give me some more guidance on how I would achieve this?
 
H

Hobs

Ok so I've realised I should have been using the 'Field' property instead of
'FormField'. I can now access and see the ActiveX Control, i.e. Label and
CommandButton.

However I still have the issue that if my command button randomly changes
name on save the associated code for the button won't fire because (as far as
I can see) it requires the code hook to be in the form <name>_Click(). Is
there another way to wire up the 'onClick' event of a button without this
dependency?

The other thought I had was to just enumerate the controls on the page and
set the name on the controls every time the document is open. This of course
would be dependent on the order of the items to be fixed, i.e. will it be
guaranteed that the order of the items in the Field list always be the same?
And I am presuming it's a left to right, top to bottom sequence.
 
J

Jean-Guy Marcil

:

The other thought I had was to just enumerate the controls on the page and
set the name on the controls every time the document is open. This of course
would be dependent on the order of the items to be fixed, i.e. will it be
guaranteed that the order of the items in the Field list always be the same?
And I am presuming it's a left to right, top to bottom sequence.

If you use that last approach, use individual bookmarks for each ActiveX.
The bookmark name will be the clue as to the name that you should assign to
the sole ActiveX control within the bookmark range.

Just have your code loop through the bookmark collection.
 
H

Hobs

Thanks for the help. I really needed a way to uniquely and dependably
reference the ActiveX controls and this certainly does the job.
 

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