Solved

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

Posted on 2010-09-03
10
1,288 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 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