PDA

View Full Version : view formfield type from protected document



OTWarrior
10-16-2007, 07:18 AM
I have a document from an external source that uses form fields, and is protected. I need to be able to uniquely identify this version with other versions (which are similar, apart from a few extra formfields in different places) so so thought I could read the type of form field Activedocument.formfields(8) is.

unfortunately, the code:

MsgBox (ActiveDocument.FormFields(i).TextInput.Type)

doesn't work on a protected document.
Is there another way to read the type of form field?
or to read the protected text before the form field?

NB: I do not have access to the password for this document

TonyJollans
10-16-2007, 09:00 AM
unfortunately, the code:

MsgBox (ActiveDocument.FormFields(i).TextInput.Type)

doesn't work on a protected document.

Fortunately, it isn't the code you want. The TextInput object is only relevant for a Text FormField, and its Type is a design-time consideration.

To determine the FormField Type (as opposed to the Text Field Type), use:
MsgBox (ActiveDocument.FormFields(i).Type)

This will display a number - the constants are:

wdFieldFormTextInput = 70
wdFieldFormCheckBox = 71
wdFieldFormDropDown = 83

Given that there are only three types, however, I am somewhat surprised that you think you can uniquely identify a version from it.

OTWarrior
10-16-2007, 09:21 AM
Sorry, by type (format??) I meant date or number or text, which is why i had the TextInput.Type.

the problem is i want to identify the document, and formfield(8) on one type of the document is a number, but on the other version it is a date.
This is currently the only way i can think of to uniquely identify the differences in the document (that I can get word to read).

TonyJollans
10-16-2007, 12:27 PM
You can't do it directly, but you can do ...

ActiveDocument.Range.Copy
Documents.Add
ActiveDocument.Range.Paste
Debug.Print ActiveDocument.FormFields(8).TextInput.Type
ActiveDocument.Close wdDoNotSaveChanges

(sorry pressed Enter too soon, editing ...)

Copy the document to a new one, check the formfield, and then throw away the temporary document. I'm sure you can tidy it up a little :)

OTWarrior
10-17-2007, 03:54 AM
unfortunately, I could not get that code to work.

however, i am currently trying:

If ActiveDocument.FormFields(13).Range.Find.Text = Chr(47) = True Then
MsgBox "Date of birth"
Else
MsgBox "Tel number"
End If

but the if statement is always false. :(
I did try using selection and range, but they only work on an unprotected document.

is there a way of finding a certain character (such as Chr(47)) that is contained in a string value, and if it is found, outputting an answer of true?

TonyJollans
10-17-2007, 04:19 AM
What went wrong? - and what version of Word (I don't think it should make a difference but best to be sure)?

OTWarrior
10-17-2007, 04:46 AM
word 2002, but don't worry as i was getting confused, as after it runs i can't view the code (it says project unavailable, and I have to shut down word). The immediate window still shows, and looks a start to the solution

Thank you

fumei
10-17-2007, 09:16 AM
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim strTest As String
Set ThisDoc = ActiveDocument
Set ThatDoc = Documents("The quick.doc")

strTest = ThatDoc.FormFields("Text3").Result
If InStr(1, strTest, Chr(47)) > 0 Then
MsgBox "Yes, text3 has a slash."
Else
MsgBox "No, Text3 does NOT have a slash."
End If"The quick.doc" is an open document, but NOT the active one. It is protected for forms. You can get the string Result without unprotecting, and then test stuff on that string.

And of course you can, as you have done, use the index number of the formfield.

strTest = ThatDoc.FormFields(3).Result

will work fine.