Finding the value of fields

E

EllenM

Hello,
I have embedded 4 numeric fields into a word template. I am trying to write
a macro that adds up the fields and displays the sum in a message box. Being
a novice at VB, I need to know how to determine the value of a field.
Unfortunately, I can not find the name of the 4 fields I inserted into the
template because Word does not seem to prompt you to name the field, only
insert a field. It is not immediately obvious to me how to find this
information in Word. If I knew this, I could probably find the field values
myself. I did find some code (below) that may help but I do not know how I
would use it. Any suggestions?



ActiveDocument.FormFields(i).Result = strvalue



Thanks, Ellen ;-D
 
H

Helmut Weber

Hi Ellen,
hmm...

for each formfield you insert,
an according bookmark is created.
For accessing the formfields result,
you may use the name of the bookmark,
or the numeric index of the formfield.

You may set the name-property of a formfield,
which results in renaming the bookmark, too.

Sub Test555bab()
MsgBox ActiveDocument.FormFields("Text1").Result
MsgBox ActiveDocument.FormFields(1).Result
' it is assumed, that FormFields("Text1")
' and FormFields(1) are identical
MsgBox ActiveDocument.FormFields(1).Name
ActiveDocument.FormFields(1).Name = "Test"
MsgBox ActiveDocument.FormFields(1).Name
MsgBox ActiveDocument.FormFields("Test").Result
End Sub

If you run the macro a second time,
you get an error, as FormFields("Text1")
doesn't exist any more.

For adding up the results of some formfields in a loop
it would be advisable to use the numeric index,
which requires the formfields to be created
after each other, and loop like this:

Beware, Pseudocode!

For lngC = 4 to 7
result = result + formfields(lngC).result
next

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

EllenM

Thanks, Helmut.

When I run your macro, it gets stuck on the first line below and when I
comment out the first line, it hangs up on the second. Both lines yield the
error messages below. It suggests that I have not created any fields when I
know otherwise.



Runtime error ‘5941’

Requested member of the collection does not exist.



MsgBox ActiveDocument.FormFields("Text1").Result

MsgBox ActiveDocument.FormFields(1).Result

Any idea why this is happening?
 
H

Helmut Weber

Hi Ellen,
When I run your macro, it gets stuck on the first line below and when I
comment out the first line, it hangs up on the second. Both lines yield the
error messages below. It suggests that I have not created any fields when I
know otherwise.
Runtime error ‘5941’

Requested member of the collection does not exist.

I understand that there may be no formfield "Text1".
I don't understand that formfields(1) does not return a value.
Are you sure, we are talking about formfields at all?

Check: Msgbox activedocument.formfields.count

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

EllenM

Thanks, Helmut. You're right, when I ran your last line of codes the number
of form fields returns zero hits. I guess I've inserted plain fields into the
template, which I did from the insert menu of Word.

Is there a way to access a name and value of a field? Sorry for the
confusion.

Ellen
 
H

Helmut Weber

Hi Ellen,

IMHO there is something fundamentally wrong in your (code) design.

Sub Testq()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If InStr(oFld.Code, "AUTHOR") Then
MsgBox oFld.Result
End If
If InStr(oFld.Code, "FILENAME") Then
MsgBox oFld.Result
End If
Next
End Sub

But there may be several fields which contain "AUTHOR" in the code.

May I leave this to other experts here,
who are more proficient in fields issues?

HTH, nevertheless
 
P

Peter Jamieson

No, in the general case, fields do not have names, as you have discovered.
You either have to know their numeric index, or you have to search the
collection of fields looking for the fields you want, using some criterion,
or, if they are SET fields, you can access their current value using the
bookmarks collection, e.g.

{ SET myvalue 123 }

creates a bookmark called myvalue, whose /text/ value you can get via

ActiveDocument.Bookmarks("myvalue").Range.Text

I don't know exactly how you are using these fields, but for example, if you
are using ASK fields to obtain values from users, ASK fields also set the
value of a bookmark.

Peter Jamieson
 
E

EllenM

Thanks to both Helmut and Peter. Your replies were quite helpful.

I now know that I don't have form fields in my document, and that I was
inserting individual fields all along. Are form fields used in the document
template or do form fields refer to a userform?
 
P

Peter Jamieson

There are many different types of "fields" in Word (have a look at Word Help
which describes almost all of them).

There are also several types of "form".

Word form fields are used in what are called "Online forms" in Word. Form
fields let you set up a data capture form where everything except the form
fields is protected and cannot be altered. If you enable Word's Forms
toolbar (e.g. via View|Toolbars) you can quickly find out roughly what they
do. NB, you have to "protect" the form using the lock button on that toolbar
before you can use it as it should be used. There's yet another type of form
you can set up where the data is entered directly into the Word document,
and it uses the "Control Toolbox" (with yet another toolbar). Although some
people have found them useful, I would avoid them.

In Word 2007 both these things have in effect been displaced by forms that
use "Content controls", which are new in Word 2007 and intended to help
exploit XML data stores.

All that said, in my view, if you want to capture data using a form, doing
it using this kind of form is only really worth it if it does exactly what
you need, or your forms requirements are small-scale and you have no other
suitable application. For example, to do data capture, you might be better
off with an HTML form (easy to deploy), or an Access form (easier to capture
the data) or - perhaps better suited if you're doing a lot of it, using
something like InfoPath.

If you don't really need to capture data inline in Word, you may be better
off designing a VBA Userform. You get better control and so on. If you need
to use the results in Word, you can stuff them in quite easily - for
example, using Document variables and { DOCVARIABLE } fields.

All these things have their place, but also require different types of
expertise to make them work.

Peter Jamieson
 

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