Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control array on Word doc? 1

Status
Not open for further replies.

TimGoff

Technical User
Jul 16, 2002
166
GB
Hi,

I have 20 or so text boxes on a Word doc - named

txtTextBox1
txtTextBox2
txtTextBox3

etc

Firstly - can you set them up as a control array as you can in VB?

If not, is it possible to reference them through a FOR loop rather than having to reference each explicitly, eg

For i = 1 to 20

Msgbox (txtTextBox & 1)

Next i

?

Thanks in advance

Tim
 
Take a look at the InlineShapes collection of the Document object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

I've had a look through Inlineshapes - would you be able to talk me through how i could use it?

Thanks
Tim
 
Be very careful here. The Word object model gets rather strange in this area. You state you have a number of textboxes, and you have them named. Could you be very specific on how you created the textboxes, and how you named them? The reason I ask is the following:

1. From Word menu, select Insert > TextBox. Type "111111".

2. From Word menu, select Insert > TextBox. Type "222222".

3. From Word menu, select Insert > TextBox. Type "333333".

You have inserted three (3) textboxes. Run the following code:

Code:
MsgBox "InLine Shapes Count = " & ActiveDocument.InlineShapes.Count & vbCrLf _
& "Shapes Count = " & ActiveDocument.Shapes.Count

It displays the number of InLine Shapes, and the number of Shapes.  Result?

InLine Shapes = 0
Shapes = 3

Running further code to extract the names of these textboxes returns:

"Canvas 3" - the first textbox
"Canvas 6" - the second
"Canvas 9" - the third.

These are, in fact, part of the CanvasShapes Collection.
Another strange thing.  You can not record a macro to insert a textbox.  You can [b]run[/b] code to create a textbox, but you can not record it.

Anyway, I just want to clarify, precisely, how you created the textboxes, and named them.
Because there are textboxes...and then there are other textboxes.


Gerry
 
Assuming that you do not need to access them by name via a name in string then try creating an array of controls.

Module level
Private txtbox(0) a Textbox

' Execute Once somewhere
if txtbox(0) is nothing then
SetTxtBox txtbox1, txtbox2, txtbox3, ....txtbox20)
End if
End Sub
Private Sub SetTxtBox(paramArray ptxtbox())
Redim txtbox(Ubopund(pTxtBox))
Dim i as Integer
For i = 0 to Ubound(ptxtBox)
Set txtbox(i) = ptxtbox(i)
next
End Sub

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
That is what I am trying to find out. Are these controls, on a form, or are they textboxes in the document?

If they are controls, it makes it much easier.

Gerry
 
Thanks for your posts guys.

How do you create an array of controls in word?

Gerry - answer to your question - I create the text boxes in design mode within word, and then simply copied and pasted the rest of the text boxes.

To then use them I protect the document for Forms - so confuses me a little - I would of said they are textboxes - but the document has been protected for Forms?

Confused!

Tim
 
I am still confused. If they require protection, then they are formfields - and they are in the document itself.

Usually, when someone refers to design mode, I think VBA.

However, this does not sound like VBA.

So. You state "copied and pasted" the rest. Let me ask again. Precisely, how did you make the first textbox.

Did you, go through the Insert, Textbox route?
Did you, use the Forms toolbar, and make a textbox.

Perhaps this may help. The textboxes - with no text in them, are they visible in the document in Print Layout View ( or LayoutView...but I am NOT asking about Print Preview!)? Are they shaded rectangles, right in the document?

If so, then they are FormField textboxes.

If you click on them and they show "handles" for resizing, then they are NOT FormFields.

Once we get this clear we should be able to move on. It will make it much much easier if they are FormField textboxes. You mentioned names - again, how did you name the?

Anyway, if they are FormField textboxes, then yes, you certainly can create an array of them.





Gerry
 
Just in case they ARE in fact formfields, here is some code that you can play around with. What it does is:

1. Loops through all the formfields in the document. If it finds a textbox formfield, it dumps the name of the field into an array.

2. keeps a counter of both total number of fields, and number of textbox fields.

3. loops through array of textbox formfield names, retrieves the result (value)

4. appends each field name, and value, to a string. String includes total number of fields, and total number of textboxes

5. displays message.

You could easily adjust this to what use you may have for the array.

Code:
Sub TextFFArray()

Dim mFormField As FormField
Dim i As Integer
Dim intFFCount As Integer
Dim intTextFFCount As Integer
Dim TextFormFields() As String
Dim sMessage As String
Dim var

For Each mFormField In ActiveDocument.FormFields()
[COLOR=red]' increase total count of fields[/color red]
    intFFCount = intFFCount + 1
    If mFormField.Type = wdFieldFormTextInput Then
[COLOR=red]' increase total count of text fields[/color red]
        intTextFFCount = intTextFFCount + 1
        ReDim Preserve TextFormFields(i)
        TextFormFields(i) = mFormField.Name
        i = i + 1
    End If
Next

[COLOR=red]  ' may as well use this again[/color red]
i = 0
[COLOR=red]  ' loop through array getting field name &
  ' using it to get resulting value[/color red]
For var = 1 To intTextFFCount
    sMessage = sMessage & _
      "Field name:=  " & TextFormFields(i) & "   " & _
      "Value:= " & ActiveDocument.FormFields(TextFormFields(i)).Result & _
        vbCrLf
        i = i + 1
Next
[COLOR=red]' display message with:
' total number of fields,
' total number of textboxes
' each textbox name and result[/color red]
MsgBox "There are " & intFFCount & " formfields in this " & _
    "document, including " & intTextFFCount & " textboxes." & _
    vbCrLf & " The contents of those textboxes are:" & vbCrLf & _
    sMessage
End Sub

Hope this helps. But really, gotta know if you have formfield textboxes, or not.

Gerry
 
Gerry - again, thanks for your posts.

There are definitely text boxes!

It looks like it makes life easier using Form Fields rather than text boxes on a word document ?

Tim
 
YES! Hate "textboxes". Formfield textboxes, on the other hand, are easy to use, at least in terms of gathering user input. The "other" textboxes are Word's rather pathetic way of pretending to be Quark XPress.

If you want to have get user input information, use formfields textboxes. The "other' textboxes seem to me a misnomer - even Word comes up with "Create your drawing here" when you use Insert > Textbox. Drawing? After selecting Insert > Textbox? Excuse me? Say what?

BTW: I posted some useful FormField code recently. In it, I changed the code above (in this post) from using bookmarks, to using text formfields. Much better.

I am going to write a FAQ as well. Regarding the formfields, just remember you MUST use Protection to make formfields work!

Gerry
 
Gerry, thanks for all your help. Will take it away and have a good look through.

One reason why I used text boxes instead of forms fields was that I wanted to restrict certain character input in the field - and perform other events when data was inputed - such as -

- Capatalising the text
- only allowing numbers upto 6

The formfields didn't appear to have the Keypress event associated in VBA...
 
Could you send me any code you have for doing that?

I wanted to restrict certain character input in the field - and perform other events when data was inputed - such as -

- Capatalising the text
- only allowing numbers upto 6

You are correct, formfields do not have a keypress event. You could do error trapping on numbers up to 6...after the fact. However, I am very curious about your use of textboxes. Would you be willing to share that? Maybe a file?

Gerry
 
Gerry, here you go. Yes, no problem seeing the file. Just give me your email address

Private Sub txtProgrammeTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = fNoPunctuation(fCapsAscii(KeyAscii))

End Sub

Private Function fCapsAscii(ByRef KeyAscii As MSForms.ReturnInteger)

If KeyAscii >= 97 And KeyAscii <= 122 Then
fCapsAscii = KeyAscii - 32
Else
fCapsAscii = KeyAscii
End If

End Function

Private Function fNoPunctuation(ByVal KeyAscii As Integer)

If KeyAscii = 34 Or KeyAscii = 59 Then
fNoPunctuation = 0
Else
fNoPunctuation = KeyAscii
End If

End Function
 
Send it to: gerrykn@telus.net

I will look at the file, but I have to ask immediately about the following.

Private Sub txtProgrammeTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = fNoPunctuation(fCapsAscii(KeyAscii))

End Sub

How is this Sub called? I can see how it could be, on a form. But I do not see how it is called on a textbox. Remember, we are NOT talking about textboxes on forms. We are talking about textboxes in a document. How do you call the sub? Heck I can not find any events at all with document textboxes, you can't even record macros to access them...so...well I will see hopefully when I get the file.

Thanks! I will look forward to that.




Gerry
 
Hi Gerry,
Sorry for the delay in replying...the sub is called when someone enters data into the textbox on the document (when it is protected).

Will email the document in the morning
Cheers
Tim
 
You are confusing me. You state "someone entering data into the textbox on the document (when it is protected)."

Protected textboxes are formfields, protection ususally refers to formfields. Yet you stated earlier they are NOT formfields.

So, again, if you are using textboxes (NOT formfield textboxes - I hate these ambiguous terms);

1. how are you naming them??? You have txtProgrammeTitle - how are you naming them?

2. What exactly, and I mean exactly, are you doing to make "them", these 'Textboxes".

3. If they are not formfields, then how, and what, are doing to "protect"...whatever they are?

Gerry
 
OK, I got the file, which help to unconfuse me. I did not realize that you have a combination of text formfields (which need to be protected), and control textboxes - which do not. I thought you were also talking about those (yuk) Insert > Textbox, which are a third kind of textbox.

Whew. That is some form you have there. I'm impressed.

I will write back to you directly. If we come up with a good resultg then we can post something back here. Maybe a FAQ or something.

Gerry
 
OK. To repeat, that is one hell of a form...pardon my language.

In my opinion, it is not a good idea to mush up text formfields with ActiveX textboxes. Why does Office Tel use a text formfield (therefore part of the FormFields collection); but Room Number use an ActiveX texbox control? Seems bizarre to me. I realize the advantage of using the ActiveX text control, in that you CAN use events with them, however, the code has to be set in design mode. you don't have access to it at run-time. I mean it will run, but you can not change the code at run-time. I think, but I could be wrong.

In any case...here it is. The key is OLEFormat - it is the only way to access the ActiveX control objects.

Oh, and considering you have 78 (!!!) controls, I suggest that you declare your document an object. There are hundreds of pieces of code "ActiveDocument".blah blah. it is so much easier to use something like aDoc - makes for less typing.

Code:
Sub myTextBoxArray()
' this array will get the names
' once you have the names you can call the control
' by the array index
Dim aDoc as Document
Dim TextBoxNames() As String
Dim myObj As Object
Dim i As Integer
Dim var

Set aDoc = ActiveDocument
i = 1
For var = 1 To aDoc.InlineShapes.Count
[COLOR=red]' activate each object
' independently, populate the array[/color red]
    With aDoc.InlineShapes(i).OLEFormat
        .Activate
        Set myObj = .Object
    End With
    ReDim Preserve TextBoxNames(i)
    TextBoxNames(i) = myObj.Name
    [COLOR=red]' release object[/color red]
    Set myObj = Nothing
    i = i + 1
Next
[COLOR=red]' destroy doc object[/color red]
Set aDoc = Nothing
End Sub

Have fun!

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top