PDA

View Full Version : Solved: How to auto populate all textboxes on a userform



michielh
04-21-2006, 01:38 PM
Hi there,

I am pretty experienced in using VBA with Access but using Word VBA is still very new to me.

I have a userform that shows some doc-properties in textboxes and gives the user a chance to change them. I use the on activate event of the document to open the form and the on click event of an ok button to close it again. Works fine until ....

Until someone asked me to change the forms functionality. When the form opens, not all doc-properties should be shown. Every doc-property having a string-value starting with < and ending with > should not be shown. I tried the following code but the "for each object in collection" part does not work. What am I doing wrong here?



Private Sub UserForm_Activate()

TextBox1.Value = ActiveDocument.CustomDocumentProperties("<companyname>").Value
TextBox2.Value = ActiveDocument.CustomDocumentProperties("<address>").Value
TextBox7.Value = ActiveDocument.CustomDocumentProperties("<attention>").Value
'and lots more but not shown here...

TextBox4.Value = ActiveDocument.BuiltInDocumentProperties("Title").Value
TextBox5.Value = ActiveDocument.BuiltInDocumentProperties("Subject").Value
TextBox6.Value = ActiveDocument.BuiltInDocumentProperties("Author").Value
'and lots more as well...

Dim xxx As FormField
For Each xxx In ActiveDocument.FormFields

With ActiveDocument.FormFields(xxx)

If .Type = wdFieldFormTextInput Then

If Left(.Result, 1) = "<" And Right(.Result, 1) = ">" Then
.Result = " "
End If
End If

End With
Next xxx

End Sub

TonyJollans
04-22-2006, 02:33 AM
Hi michielh,

Welkom bij VBAX!For Each xxx in ActiveDocument.FormFieldsmakes each xxx a FormField object so you don't need, or want, to use it as an index into the collection again. This should do what you wantFor Each xxx in ActiveDocument.FormFields
With xxx
' etc.
' etc.

michielh
04-22-2006, 03:26 AM
Thanks for your help Tony,

I never noticed that index mistake. However the problem is still there. A little debugging shows that VBA doesn't find any members in the collection that I loop through, allthough my userform is packed with textboxes (wrong collection?).

the piece of code now looks like this:



MsgBox "Het aantal formfields: " & ActiveDocument.FormFields.Count
Dim xxx As FormField
For Each xxx In ActiveDocument.FormFields

With xxx

MsgBox .Name

If .Type = wdFieldFormTextInput Then
If Left(.Result, 1) = "<" And Right(.Result, 1) = ">" Then
.Result = ""
End If
End If

End With
Next xxx
End Sub


The 1st msgbox tells me that there are 0 formfields in the formfields collection. I have this funny feeling that I'm not using the correct collection. What is the difference betweeen the formfields collection and the controls collection. Shouldn't I loop through controls?

I was thinking of using something like:


Dim xxx as control
For Each xxx In Me.Controls
etc...

TonyJollans
04-22-2006, 04:27 AM
Sorry, I should have read your post instead of concentrating on the first error I spotted :)

FormFields are textboxes, checkboxes, etc. in a document - added from the Forms Toolbar - components of what is called a Word Form or Online Form or similar (terminology seems a bit inconsistent).

UserForms are completely different VBA Objects - similar in some ways to (but not exactly the same as) unbound Access Forms. They have a Controls collection but it does not belong to the document (at least not directly). In code in the UserForm's code module, Me refers to the current instance of the userform and so your loop through Me.Controls should get you all the controls on the userform.

michielh
04-22-2006, 05:07 AM
Hee ha, I got it working,
:bigdance2
thanks for helping me out.



Private Sub UserForm_Activate()

blahdiblah fill out text boxes with default values and dummies

Dim xxx As Control
For Each xxx In Me.Controls

With xxx

If Left(.Name, 4) = "Text" Then
If Left(.Value, 1) = "<" And Right(.Value, 1) = ">" Then
.Value = ""
End If

End If

End With
Next xxx

End Sub

does the trick exactly the way I want it to.

Why is it that when I search the VBA Word help files I can only find information on the controls collection, and not on the control object which has a property called .value which I desperately needed. Obvious maybe.... However I couln't find it anywhere. Thanks for the explanation on formfields btw...

Michiel

TonyJollans
04-22-2006, 07:22 AM
Graag gedaan!

All I can say about help - if you're using 2003 - is that it ought to be better in the next release.

michielh
04-23-2006, 04:50 AM
Thanks again,

One last question. Is there any special reason why i can't mark this thread "solved"? By using the thread tools I get a warning telling me that my account may not have sufficient privileges to perform this type of action.

lucas
04-23-2006, 07:28 AM
Mark solved is not working yet because of the board upgrade. It's been marked solved for you.