Solved

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

Posted on 2010-09-03
10
1,298 Views
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.
0
Comment
Question by:Tony Simmons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 19

Expert Comment

by:Bardobrave
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
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?
0
 

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.


Example.doc
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 200 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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
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

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 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
0
 

Author Comment

by:Tony Simmons
ID: 33597899
Bardobrave:

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.

Graham:

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

0
 
LVL 76

Expert Comment

by:GrahamSkan
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.
 
0
 

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.
0
 

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.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

710 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