How to reference in VBA an HTML Select control in a Word document

I have been supplied with a Word 2003 document that has a number of HTML Select drop down lists within it. This document will be used by a process within Access 2007 to merge data, creating a Word 2003 document within Word 2007 using DOCPROPERTY fields.

I would like to be able to set the values within the HTML Select controls using VBA within the Access merge process that I have created. This process uses the Word object model and the required document that is acting as the template (in the English sense of the word, not a .dot file)

The supplied document is owned by an organisation that wishes to keep control over the document structure and layout so I have been trying to reference the HTML Select controls from within VBA in order to set the required value.

Other options already investigated include:

1. Replacing the HTML Select control with a DOCPROPERTY, but this removes the ability to manually adjust the data

2. Replacing the control with a combo box in Word but this will probably not be acceptable to the owner of the original document

3. Searching here and generally for a solution

I've tried setting a reference to the Microsoft HTML Object Library and the Microsoft Forms 2.0 Object Library but seem to be going round in circles trying to reference the controls from within VBA.

I've been playing around in the Word VBA window with the document open and can loop through the controls as Fields within the ActiveDocument Fields collection but this doesn't allow me to see the HTMLSelect control properties such as Name and DisplayValues.

I'm probably missing something obvious here but I'm going round in circles trying to solve this one now.

Thanks in advance.
Tony SimmonsAsked:
Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
But this works

Sub GetHTMLSelection()
    Dim objSel As HTMLSelect
    Set objSel = ActiveDocument.InlineShapes(1).OLEFormat.Object
    Debug.Print objSel.MultiSelect
End Sub
Sounds like a strange environment.

If I understand you... the problem is that you are unable to get a Document Object Model from the Word and then you are unable to reference directly de objects to interact with them.

I see two possible solutions here:

1) The dirty way:
Get all the text in the word document and save it as a html file, load the html file and try to access to the Document Object Model from it.

2) The messy way:
Get all text on the word document. Use regular expressions and string treatment to alter the code directly.
I didn't think that it was possible to place HTML controls on a document. If it is, and you are willing to change the controls, you could try using ActiveX controls which can be addressed via the Word object model. You can't use Content controls if you want to maintain 2003 compatibility, and Forms controls require forms protection, which is probably unacceptable.

Could you post the document (edited if necessary), please?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tony SimmonsAuthor Commented:
Thanks Bardobrave and Graham,

For Bardobrave:

I think we can almost ignore the Access element and start from the Word Object Model.

You're right that I'm unable to reference the HTMLSelect control from within the Word Object Model.

The final output needs to be a Word document that will be submitted electronically and so needs to be indistinguishable from the original with the exception of the data being filled in.

I'm not sure that I totally understand your suggestions but I'm supposing that the structure of the original Word document would be lost.

1) I've tried saving the word document as an HTML file but would still want to manipulate it using VBA which means that the object model is still the same, doesn't it?

2) I'm not sure that this would keep the original structure?

Graham - I've posted a new document with a couple of edited elements from the one of the originals included.

BardobraveConnect With a Mentor Commented:
Hi Accessaddict.

If you require to maintain the same structure I supose that my first idea won't be able, as when you save the word document as html you're modifying it already and changing it's structure, even if you revert it later to Word, probably it won't be the same as originally.

My second option could be truly messy, but I think it could work in your environment. Create a string variable on VB and load all word's content on it, then, search on the string looking for pieces like "<select>" "<option>" and so on.

Your string will be filled with lots of word markup (wich usually is very messy), but anywhere inside it should be the HTML of the select. You can get to it through regular expressions, modify it and reintegrate to the string, them you only need to inject your string again on the word document et voilá.

I supose that this will be very troublematic if your word have an encoding different from the html one, or if the html code is injected in the word through Active X or OLE objects, on those cases probably GrahamSkan option will be better than mine.
I don't know how far you have got.

This gets as far as the last line, but crashes Word when that line is executed.
Sub GetHTMLSelection()
    Dim objSel As HTMLSelect
    Set objSel = ActiveDocument.InlineShapes(1).OLEFormat.Object
    Debug.Print objSel.HTMLName
End Sub

Open in new window

Tony SimmonsAuthor Commented:

I see you what you mean. Effectively it would be replacing the text behind the document structure where the drop down lists occur and then the document should be the same when opened in Word. It might well work but I would hope it would be a last, if somewhat ingenious, resort! After all that's what Object Models should help to avoid. I probably won't be able to investigate that option before next week now.


Investigating the other properties from your sub shows that some of the properties are readable and some crash Word. I've used the property names listed in the Properties dialog in Word Design Mode.

The sub now produces the following in the debug window:

Start Property List
Name Property: DefaultOcxName541211
DisplayValues property causes crash
Height Property: 18
HTMLName property causes crash
MultiSelect Property: False
Selected Property:
Size Property: 0
Values property causes crash
Width Property: 75
End Property List

It's odd that some of the properties cause a crash.

There seems to be no way of seeing what the selected value is or of setting the selected value and I'm a bit stumped as to how to set it with the properties available. Maybe the answer is that it can't be done.

We now have someway of referencing the object though and I have to say that I wouldn't have worked out the InlineShapes collection as being useful so thank you Graham.

Sub GetHTMLSelection()
    Dim objSel As HTMLSelect
    Set objSel = ActiveDocument.InlineShapes(1).OLEFormat.Object
    Debug.Print "Start Property List"
    Debug.Print "Name Property: " & objSel.Name
    Debug.Print "DisplayValues property causes crash" 'objSel.DisplayValues
    Debug.Print "Height Property: " & objSel.Height
    Debug.Print "HTMLName property causes crash" ' objSel.HTMLName
    Debug.Print "MultiSelect Property: " & objSel.MultiSelect
    Debug.Print "Selected Property: " & objSel.Selected
    Debug.Print "Size Property: " & objSel.Size
    Debug.Print "Values property causes crash" 'objSel.Values
    Debug.Print "Width Property: " & objSel.Width
    Debug.Print "End Property List"
End Sub

Open in new window

It seems to be far too flaky to use. It doesn't seem to do anything that a combobox doesn't do.

Google's only findings seem to boil down to the fact that it exists. I suspect that Microsoft abandoned maintenance soon after it was first distributed.
Tony SimmonsAuthor Commented:
It seems odd that it should appear in a Word document. I wonder if the drop down lists were created in another application then copied into the Word documents that use it. The best option may be to replace the drop down lists with my own and duplicate the values, hoping that it is close enough to the original not to cause any concern.

I think your 'dirty solution', Bardobrave, may end up being a bit too dirty :-)

I'd like to thank you both for your contribution and will share the points as follows

Bardobrave : 200
GrahamSkan: 300

if you both think that's fair? I'll award in due course if I have no objections to that split.
Tony SimmonsAuthor Commented:
Although we haven't found a solution to this issue, identifying that there doesn't appear to be a solution can be as important as finding one where it exists.

I'll close this with the awarding of points as posted before and thanks to Bardobrave and many thanks to GrahamSkan for their help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.