Solved

How to loop with VBA on all controls placed in a Word doc ?

Posted on 2011-03-09
4
1,473 Views
Last Modified: 2012-08-14
Hi

I'm working on a Word template with controls, which I chose from the Control Toolbox (NOT from the "Forms" toolbar). It all works well until I try, in a kind of checking routine, to loop through all controls in the document creted through the template.

I can refer, in the VBA code, to single controls in the document with "Me.<Control name>", but I can't, for the life of me, find the syntax to go through all controls (to check their contents) in a generic loop.

I've tried to loop on Fields and FormFields, but I don't get what I want. The only thing I can find out is that I have 42 "fields" (which are my controls, in fact) and their type (combobox, textbox, listbox, ..), but I am unable to find how to get the controls' name and content in a generic "For next" loop. As I can't believe that this is not possible, I'm asking you for help :-)

Bernard
0
Comment
Question by:bthouin
  • 2
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 35089307
Yes, I think that the ability to add ActiveX controls to Word documents to a document was a bit of a misjudgement. It has led users to think of a word document as a reasonable substitute for a Windows interactive form.

Word's main objective is to produce a nicely formatted printed documents. Fields, controls, bookmarks, etc. are intended as steps toward that goal.

There isn't a single collection of ActiveX controls in the Word object model. They are entries within the InlineShapes and the Shapes collection
Sub ListActiveXControls()
    Dim ilsh As InlineShape
    Dim sh As Shape
    Dim ob As Object
    For Each ilsh In ActiveDocument.InlineShapes
        If ilsh.Type = wdInlineShapeOLEControlObject Then
            Set ob = ilsh.OLEFormat.Object
            Debug.Print ob.Name
        End If
    Next ilsh
    For Each sh In ActiveDocument.Shapes
        If sh.Type = msoOLEControlObject Then
            Set ob = sh.OLEFormat.Object
            Debug.Print ob.Name
        End If
    Next sh
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:bthouin
ID: 35094558
Hi

Brilliant ! Works like a breeze, thanks a lot. I would never have guessed that these controls were ActiveX, and even less that they are to be searched as inline shapes !

>>It has led users to think of a word document as a reasonable substitute for a Windows interactive form<<
I fully agree. The irony is, I HAVE already developed such a complete and sophisticated form in an Access front-end GUI, but my "clients" think that it'll be too complicated for the users... :-((
That's why I'm trying out the Word route. Is using Excel any better in that area ?

Two more small questions then (you get the points anyway):
- Is it a bug or a feature that, when one clicks in such an ActiveX control in Word and starts typing, the first caracter typed is NOT showing up, but all the following typed characters are OK ? To avoid this, I have to click twice in a control, then it doesn't "swallow" the first character !
- Is it a bug or a feature of ActiveX controls in Word that one cannot move from control to next control with either Tab or Return ? The cursor just doesn't move at all and stays in the field, one has to click in the next control. Very annoying

Thanks for your answers
Regards
Bernard
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35094951
The are held in Inline shapes by default. They can be formatted to be 'floating', they would then be in the Shapes collection, which explains the second loop.

As with form fields, ActiveX controls work best when the document is protected for forms. The tab does work then.

I can't reproduce the first symptom, with or without forms protection (Word 2003).
0
 
LVL 1

Author Comment

by:bthouin
ID: 35095109
Ah, I'll try protecting the doc, then.

Should I rather choose Excel to do the same thing ? Is it better ?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now