Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

861 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