formfield text fields

B

brad rail

Hi

I have created a table in which formfield text entry
I loop through and read the value of the form field

If it is blank i am getting a text string of " "
Firstly is there a better way of detecting when a form field is blank

Secondly how do i test this and then write an entry back to the formfield
ie. "Blank Entry"

Many thanks
Brad
 
J

Jay Freedman

Hi

I have created a table in which formfield text entry
I loop through and read the value of the form field

If it is blank i am getting a text string of " "
Firstly is there a better way of detecting when a form field is blank

Secondly how do i test this and then write an entry back to the formfield
ie. "Blank Entry"

Many thanks
Brad

The default for a text form field's "empty" value is five nonbreaking
spaces. The Trim function will drop any leading and/or trailing spaces
from a string, so if it's all spaces the result will have length 0.

If you want to check each field as the user exits from it, use exit
macros like this -- one for each field:

Sub ExitText1()
With ActiveDocument.FormFields("Text1")
If .TextInput.Valid Then
If Len(Trim$(.Result)) = 0 Then
.Result = "Blank Entry"
End If
End If
End With
End Sub

If you want to hit all of them at once, you can call a macro like this
from, say, a macro that intercepts the SaveAs or Print command:

Sub CheckTextFields()
Dim oFld As FormField

For Each oFld In ActiveDocument.FormFields
With oFld
If .Type = wdFieldFormTextInput Then
If .TextInput.Valid Then
If Len(Trim$(.Result)) = 0 Then
.Result = "Blank Entry"
End If
End If
End If
End With
Next oFld
End Sub
 
B

brad rail

The default for a text form field's "empty" value is five nonbreaking
spaces. The Trim function will drop any leading and/or trailing spaces
from a string, so if it's all spaces the result will have length 0.

If you want to check each field as the user exits from it, use exit
macros like this -- one for each field:

Sub ExitText1()
With ActiveDocument.FormFields("Text1")
If .TextInput.Valid Then
If Len(Trim$(.Result)) = 0 Then
.Result = "Blank Entry"
End If
End If
End With
End Sub

If you want to hit all of them at once, you can call a macro like this
from, say, a macro that intercepts the SaveAs or Print command:

Sub CheckTextFields()
Dim oFld As FormField

For Each oFld In ActiveDocument.FormFields
With oFld
If .Type = wdFieldFormTextInput Then
If .TextInput.Valid Then
If Len(Trim$(.Result)) = 0 Then
.Result = "Blank Entry"
End If
End If
End If
End With
Next oFld
End Sub


Thanks very much jay i will give it a go...
by the way there is no way use a detetection routine for a blank formfield
is there ... u have to do both the test

1 for a formfield text object
and then 1 for a nul entry after trimming..

seems a little short sighted by microsoft on that one..

thanks again

Brad
 
J

Jay Freedman

brad said:
Thanks very much jay i will give it a go...
by the way there is no way use a detetection routine for a blank
formfield is there ... u have to do both the test

1 for a formfield text object
and then 1 for a nul entry after trimming..

seems a little short sighted by microsoft on that one..

thanks again

Brad

Hi Brad,

If you control the template, and you can be sure that the text field exists
(e.g., hasn't been deleted or renamed by the user), then you can omit the
test

If .TextInput.Valid Then

and its matching End If. Everything else is required. In the second macro,
looping through the FormFields collection, any given FormField object could
be a text field, a check box, or a dropdown. Since you want to validate only
the text fields, you have to do the .Type test.

An alternative (but not a good one in my opinion) is to use an On Error
statement to trap any attempt to get the .Result when it isn't valid. The
trouble with On Error is that it traps all kinds of errors, so then you have
to look at Err.Number to see if it's an "expected" error or something
unknown. That can get pretty hairy -- better to do some work up front to
prevent errors as much as possible.
 

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