Solved

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

Posted on 2011-03-09
4
1,806 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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 shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

730 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