Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-09
4
Medium Priority
?
2,319 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 1000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

604 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