Form field frustration

T

TKovacs

I am having a couple of issues programmatically accessing form fields. I
have assigned “bookmark†names to these fields and here is how I can use VBA
to see what has been entered in the field.

Dim FieldText as string
FieldText = Word.ActiveDocument.Bookmarks("LastName").Range.Text

The problems :
A) I cannot detect when the field is empty
B) Empty fields have text value of 5 spaces, but return FALSE when compared
to being equal to 5 spaces
C) Trim seemingly has no effect, as “trim(FormField)†still = FormField = 5
spaces
D) Any attempt to erase the text in a form field by setting = Ҡerases the
form field, setting equal to 5 spaces also does not work.

I need to be able to detect an empty form field, and be able to erase text
in form fields. Any ideas?
 
J

Jean-Guy Marcil

TKovacs was telling us:
TKovacs nous racontait que :
I am having a couple of issues programmatically accessing form
fields. I have assigned "bookmark" names to these fields and here is
how I can use VBA to see what has been entered in the field.

Dim FieldText as string
FieldText = Word.ActiveDocument.Bookmarks("LastName").Range.Text

The problems :
A) I cannot detect when the field is empty
B) Empty fields have text value of 5 spaces, but return FALSE when
compared to being equal to 5 spaces
C) Trim seemingly has no effect, as "trim(FormField)" still =
FormField = 5 spaces
D) Any attempt to erase the text in a form field by setting = ""
erases the form field, setting equal to 5 spaces also does not work.

I need to be able to detect an empty form field, and be able to erase
text in form fields. Any ideas?

You cannot have a totally empty formfield, at the very least you need one
space.
You are right that the five default spaces are not regular spaces, they are
ChrW(8194).

So, if you want to remove those five spaces, you first need to detect them,
when you do, either replace them with a single space, or format the field as
..Font.Hidden = True. But to do this, you need to unprotect first.

In any case, here is how you can detect those five spaces:

If the text formfield has those five spaces, it means the content was
deleted, or there was never any content. In either case, the Result property
returns an empty string.
'_______________________________________
Dim fldTarget As FormField

Set fldTarget = ActiveDocument.FormFields("Text1")

With fldTarget
If CStr(.Result) = "" Then
MsgBox "It is empty"
.Result = " "
End If
End With
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

The content of an "empty" text form field is five _nonbreaking_
spaces. You can make a constant in your macro -- just press
Ctrl+Shift+spacebar five times between a pair of double quotes,

Dim emptyField As String
emptyField = " " ' 5 nonbreaking spaces

and compare the field's text to that constant.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
T

TKovacs

That got me part way. Thanks. The other part is how can I erase what is in
a field without erasing the field? I tried setting the text to 5
non-breaking spaces, but it still disappeared.

Word.ActiveDocument.Bookmarks("PreferredName").Range.Text = ChrW(8194) &
ChrW(8194) & ChrW(8194) & ChrW(8194) & ChrW(8194)

Do you have a solution to that? Thanks!
 
J

Jean-Guy Marcil

TKovacs was telling us:
TKovacs nous racontait que :
That got me part way. Thanks. The other part is how can I erase
what is in a field without erasing the field? I tried setting the
text to 5 non-breaking spaces, but it still disappeared.

Word.ActiveDocument.Bookmarks("PreferredName").Range.Text =
ChrW(8194) & ChrW(8194) & ChrW(8194) & ChrW(8194) & ChrW(8194)

Do you have a solution to that? Thanks!

If you are going to work with formfields, use the formfield object, not the
Bookmark one.

Your code above essentially replaces the range of the formfield with spaces,
therefore deleting it, but you would have to unprotect the document first..

In your original post, you wrote that you wanted to detect if a formfield
was made of those 5 default spaces, and if so, delete the content.
Now you are asking about how to insert those 5 spaces....
I am a bit confused.

To delete a formfield content, just use:

ActiveDocument.FormFields("Text1").Result = ""

This will automatically insert the 5 spaces you are writing about.

Are you sure you are working with formfields and a protected document?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
T

TKovacs

When I attempt to set it = "" I get an error that reads "You are not allowed
to edit this selection because it is protected". So I tried this:

ActiveDocument.Unprotect
Word.ActiveDocument.Bookmarks("PreferredName").Range.Text = ""
ActiveDocument.Protect wdAllowOnlyFormFields, True 'Re-protect

The result: That form field is gone...... Grrr
 
J

Jay Freedman

You completely ignored what Jean-Guy told you. DO NOT set the
Bookmarks("PreferredName").Range.Text. That usage is why you're being
forced to unprotect/reprotect and why your field is being deleted.

Instead,

will clear the field and leave it ready to accept new text -- and you
DO NOT need to unprotect the document to do this.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
T

TKovacs

You are right. I overlooked that. Thank you!

Jay Freedman said:
You completely ignored what Jean-Guy told you. DO NOT set the
Bookmarks("PreferredName").Range.Text. That usage is why you're being
forced to unprotect/reprotect and why your field is being deleted.

Instead,


will clear the field and leave it ready to accept new text -- and you
DO NOT need to unprotect the document to do this.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Top