FormField within hidden text

F

Francois Piette

Given a document possibly having a FormField named "DocType", I want to
write a macro which get the formfield value:

01 If ActiveDocument.Bookmarks.Exists("DocType") = True Then
02 DocType = ActiveDocument.FormFields("DocType").Result
03 Else
04 DocType = "Unknown"
05 End If

This code works very well except when the FormField is within a hidden text
region. In that case, DocType is empty.
The question is: how to get the formfield value even when into a hiident
text block ?

Thanks for any help.
 
P

Peter Hewett

Hi Francois Piette

You attach an OnEntry and OnExit macro to the hidden FormField and add the following code
to the template:

Public Sub AAAOnEntry()
With ActiveDocument
.Unprotect
.FormFields("Text2").Range.Font.Hidden = False
.Protect wdAllowOnlyFormFields, True
End With
End Sub

Public Sub AAAOnExit()
With ActiveDocument
.Unprotect
.FormFields("Text2").Range.Font.Hidden = True
.Protect wdAllowOnlyFormFields, True
End With
End Sub

Attach the macro AAAOnEntry as the ONENtry macro and AAAOnExit as the OnExit macro. This
will keep the FormField hidden until you actually tab into it. It will then hide it again
when you tab out. In the above code the FormField being unhidden/hidden is named "Text2"
and you'll need to change this to whatever you've named the FormField.

HTH + Cheers - Peter
 
A

Alex Ivanov

This should work:

If ActiveDocument.Bookmarks.Exists("DocType") Then
With ActiveDocument.FormFields!DocType.Range
.TextRetrievalMode.IncludeHiddenText = True
DocType = .Text
End With
Else
DocType = "Unknown"
End If
End Sub
 
F

Francois Piette

You attach an OnEntry and OnExit macro to the hidden FormField

That's exactly what I don't want to do because the macros can't be attached
to the document (documents are from another system). They must reside in
normal.dot.

Anyway thanks.
 
P

Peter Hewett

Hi Francois Piette

That being the case it would have saved me wasting my time if you had taken the trouble to
explain that in the first place.

Is your problem that you can't see the FormField (and consequently the data entered into
it) or you are having trouble retrieving the data from the FormField.

Cheers - Peter


That's exactly what I don't want to do because the macros can't be attached
to the document (documents are from another system). They must reside in
normal.dot.

Anyway thanks.

HTH + Cheers - Peter
 
F

Francois Piette

Alex,

Your code work only when the document is unprotected.
Once the document is protected, the line
DocType = .Text
trigger an error: "This method or property is not available because the
object reference a protected field in a document" (Free translation from
french: I used a localized Word).

I should have said that the FormField is in a protected document and part of
a hidden text block.
Any idea ?
 
F

Francois Piette

Is your problem that you can't see the FormField (and consequently the
data entered into
it) or you are having trouble retrieving the data from the FormField.

I see the formfield, that is the execution goes to line 02 (see below). But
the value copied to DocType at line 02 is an empty string when DocType
formfield is part of a hidden text bloc.

01 If ActiveDocument.Bookmarks.Exists("DocType") = True Then
02 DocType = ActiveDocument.FormFields("DocType").Result
03 Else
04 DocType = "Unknown"
05 End If
 
P

Peter Hewett

Hi Francois Piette

The problem here is that a hidden FormFields Result property *always* returns
vbNullString. So you have to work around it, like this:

Public Sub GetHiddenFFResult()
With ActiveDocument

' The document must be unprotected to retrieve the result
.Unprotect
With .FormFields("Text2").Range

' This statement is only required if you can't see the hidden text
.TextRetrievalMode.IncludeHiddenText = True

' Retrieve the Result using the FormFields Range object
MsgBox "This is the FormField result: " & .Text
End With

' Make the FormFields usable again
.Protect wdAllowOnlyFormFields, True
End With
End Sub

Change the name ("Text2") of the FormField in the above example to suit your requirements.

HTH + Cheers - Peter
 
F

Francois Piette

Peter,

Thank you for you code. It work as expected.

FYI, here is what I've actually done.
This works regardless of document protection and hidden text.

If ActiveDocument.Bookmarks.Exists("DocType") Then
ProtType = ActiveDocument.ProtectionType
If ProtType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

ActiveDocument.FormFields("DocType").Range.TextRetrievalMode.IncludeHiddenTe
xt = True
DocType = ActiveDocument.FormFields("DocType").Range.Text
If ProtType <> wdNoProtection Then
ActiveDocument.Protect ProtType, True
End If
Else
DocType = "Unknown"
End If
 
P

Peter Hewett

Hi Francois Piette

I'd wrap your code in With blocks, it's more efficient:

Const cFFName As String = "DocType"

With ActiveDocument
If .Bookmarks.Exists(cFFName) Then
ProtType = .ProtectionType
If ProtType <> wdNoProtection Then .Unprotect

With .FormFields(cFFName).Range
.TextRetrievalMode.IncludeHiddenText = True
DocType = .Text
If ProtType <> wdNoProtection Then .Protect ProtType, True
End With
Else
DocType = "Unknown"
End If
End With

I'd strongly suggest that you comment the code otherwise when you come back to it you'll
never know why you're doing what you're doing!

HTH + Cheers - Peter
 
C

Charles Kenyon

Peter,

There is a problem with Word and hidden formfields. I am sure Word wasn't
written to handle hidden formfields and I've seen other anomolies. Try
formatting a text field as a date and hiding it. Then protect the form and
type in a date and tab out. You'll get a format error even though the date
is properly formatted.
 
P

Peter Hewett

Hi Charles Kenyon

Lets face it the idea of a hidden FormField is a bit daft! Who *really* wants an input
field they can't see unless view all formatting marks or show hidden text is on? It's
probably outside the ambit of Words developers and testers. I'd never have considered it.

I guess the reason FormFields requiring specific format data fail validation, is as I said
in another branch of this thread <The problem here is that a hidden FormFields Result
property *always* returns vbNullString.>.

After time on the benches in these NG's nothing anyone tries to do with Word surprises me
any more!

Cheers - Peter


Peter,

There is a problem with Word and hidden formfields. I am sure Word wasn't
written to handle hidden formfields and I've seen other anomolies. Try
formatting a text field as a date and hiding it. Then protect the form and
type in a date and tab out. You'll get a format error even though the date
is properly formatted.

HTH + Cheers - Peter
 
C

Charles Kenyon

I agree, it is daft. I ran into it when I designed a billing template for
another lawyer. It has three input sections, time, disbursements, and
payments. The lawyer decided that he didn't want the payments or
disbursement sections displayed unless there actually was a payment or
disbursement to report. So I intercept the print command and hide those
sections (tables) if the total for them is 0. Simple enough.

However, then he tried to add a payment to a bill already printed. That is
where I learned that the date field wouldn't take a date if the field was
hidden. (I told him to start a new bill if he has new information to add.)
Since the bill grabs data from the previous bill when created, this is not
really hard to do.
 

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