values of form fields

B

Beth

Hello.

I'm trying to figure out how to determine the values entered into fields on
my Word document.

I've seen lots of examples using the formFields collection, but for whatever
reason, my collection is empty and my controls appear in the fields
collection instead.

I'm using Word 2003 and added a command button to use Outlook to return the
document.

When I iterate through the fields collection, the type always comes back 87
even though I have text boxes and a combo box, so I can't use it to determine
the field type.

What I'd like to do is something like this:
Private Function buildBody() As String
Dim fld As Field
Dim sBody As String

sBody = ""
For Each fld In Me.Fields
With fld
sBody = sBody & fld.Index & ": " & fld.Result.Text & Chr$(13) &
Chr$(10)
End With
Next
buildBody = sBody
End Function

but I can't get that to work.
The only way I've found to get the entered field value out of VBA code is to
reference the control directly, like:
?txtpmin.Text
12345678

Is there a way to iterate through the controls on the Word document and
determine the filled-in values?

Thanks for any help,

-Beth
 
D

Doug Robbins - Word MVP

See the following page of fellow MVP, Greg Maxey's website:

http://gregmaxey.mvps.org/Extract_Form_Data.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Beth

Sorry, I can't use
.FormFields("Text1").Result
because my formFields collection is empty.
My fields collection contains an item for each control on my form.
 
D

Doug Robbins - Word MVP

If you use Alt+F9 to toggle on the display of field codes, what do you see?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Beth

CONTROL Forms.TextBox.1 \s
CONTROL Forms.ComboBox.1 \s
CONTROL Forms.TextBox.1 \s
CONTROL Forms.CommandButton.1 \s
CONTROL Forms.Label.1 \s

I gave the controls names in the properties window, but I'm not seeing them
reflected here. I see the names in the VBA code.

I'm wondering if it's because I took the text, combo, and label controls
from the control toolbox toolbar instead of the forms toolbar.

I'll try using the forms toolbar for the controls and see if that populates
my formFields collection.

Thanks for the help,

-Beth
 
B

Beth

OK, I've seen the controls from the Forms toolbar before.
They're too limited- I don't see them in my VBA code.

Is there a way to get the values from controls on the control toobox toolbar?
 
B

Beth

uuughleee...

I'm not liking this 'macro on exit or entry implemented in a module
disconnected from the control' idea at all.
I tried doing field validation in an exit macro:
Sub txtPMINExit()
Dim fld As FormField
Dim sVal As String

Set fld = ActiveDocument.FormFields("txtFormPMIN")
sVal = fld.Result
If Len(sVal) <> 8 Then
MsgBox ("Please enter a valid PMIN")
fld.Select
End If
End Sub

and the select method didn't even retain the focus.

We have security issues using Word ActiveX controls, anyways, so if I can't
get reading values from controls using a collection to work, I'll have to
give up on Word and try proving the concept in PDF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top