Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2010-09-03
Medium Priority
Last Modified: 2013-11-27
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.
Question by:Tony Simmons
  • 4
  • 4
  • 2
LVL 19

Expert Comment

ID: 33596099
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.
LVL 77

Expert Comment

ID: 33596350
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?

Author Comment

by:Tony Simmons
ID: 33596444
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.

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

LVL 19

Assisted Solution

Bardobrave earned 800 total points
ID: 33597246
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.
LVL 77

Expert Comment

ID: 33597317
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

LVL 77

Accepted Solution

GrahamSkan earned 1200 total points
ID: 33597352
But this works

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

Author Comment

by:Tony Simmons
ID: 33597899

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

LVL 77

Expert Comment

ID: 33598608
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.

Author Comment

by:Tony Simmons
ID: 33600327
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.

Author Closing Comment

by:Tony Simmons
ID: 33602611
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.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
When we develop an application in Ms Access 2016 we should also try to protect the queries, macros and table links. I know I may not have a permanent solution but for novice users, they will not manage to break your application. Below is the detail …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question