M
Mizpah
Hi,
This is quite long, so sorry in advance. I just wanted to explain as
thoroughly as I could.
I used the following code in this form and it did work for awhile. This
was my first attempt at setting a range and only working with the
formfields it that range. I don't know if this is the best most
efficient way of doing it, but it worked until I started playing with
If - Then statements for protecting and unprotecting the form &
inserting code to ensure these formfields within this range were indeed
enabled.
This is what I started with:
Dim myRange As Range
'range of usually 4 non-record formfields which are: "bkCEntity, bkDBA,
bkStateI, bkDateI
Set myRange = ActiveDocument.Range( _
Start:=ActiveDocument.FormFields("bkCEntity").Range.Start, _
End:=ActiveDocument.FormFields("bkDateI").Range.End)
With myRange
Dim intFF, intCountA As Integer
intFF = .FormFields.Count
For intCountA = 1 To intFF
If .FormFields(intCountA).Range.Font.Hidden = False
Then
.FormFields(intCountA).Range.Font.Hidden = True
End If
Next intCountA
End With
This range usually has only 4 formfields - which I mentioned above, and
intFF was returning 4 like it should.
The whole reason I am doing this is because the user uses the word
template to either be populated via an Access database with the
corresponding client and then prints it off for production. Or the user
can use the same template to edit client records - update, new, or
delete - directly. When the user enters edit mode, there are 4
formfields that would not be used in the database, they are for
production purposes only. The user has a problem with the gray shaded
formfields being there because they tab through the documents 40 or so
formfields to change the record data. Since the user doesn't really
look at where the cursor goes after tabbing through, the wrong
formfields gets updated. So I am trying to hide completely the
non-record ones during editing so this doesn't happen.
Because there are different toolbar buttons & userform buttons that the
user uses for the various tasks of editing I have to unprotect or
protect the document to help. This is the code I use:
Sub Recall()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
UserForm2.Show
End Sub
or
'Lock form for editing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect wdAllowOnlyFormFields
End If
But then I noticed that when the form was protected. The 4 non-record
formfields where no longer hidden, but they were not usable. I couldn't
click on then. So then I added some code to ensure they were enabled. I
didn't do anything conditional statements or any loops since this time
because I wasn't sure if enabling them would work at the time. In the
end the whole section of code looked like this:
UserForm2.Hide
With myRange
Dim intFF, intCountA As Integer
intFF = .FormFields.Count
For intCountA = 1 To intFF
If .FormFields(intCountA).Range.Font.Hidden = False
Then
.FormFields(intCountA).Range.Font.Hidden = True
End If
Next intCountA
End With
MsgBox "This form can now be edited." & vbCrLf & _
"Information can only be entered into the grey fields." &
vbCrLf & _
"Any data entered on this form will be added to the
database.", vbInformation, "Create New Record"
Unload Me
End If
'Lock form for editing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect wdAllowOnlyFormFields
End If
With ActiveDocument
.FormFields("bkCEntity").Enabled = True
.FormFields("bkDBA").Enabled = True
.FormFields("bkStateI").Enabled = True
.FormFields("bkDateI").Enabled = True
End With
But this time intFF is now returning 8 instead of 4 and now I am
getting the error: Run-time error '5941 The requested member of the
collection doesn't exist. So why does intFF return 8 now instead of 4?
I have replaced intFF with 4 in the code and it works just fine. But I
am curious as to what I did wrong, or could do different.
Thanks in advance.
This is quite long, so sorry in advance. I just wanted to explain as
thoroughly as I could.
I used the following code in this form and it did work for awhile. This
was my first attempt at setting a range and only working with the
formfields it that range. I don't know if this is the best most
efficient way of doing it, but it worked until I started playing with
If - Then statements for protecting and unprotecting the form &
inserting code to ensure these formfields within this range were indeed
enabled.
This is what I started with:
Dim myRange As Range
'range of usually 4 non-record formfields which are: "bkCEntity, bkDBA,
bkStateI, bkDateI
Set myRange = ActiveDocument.Range( _
Start:=ActiveDocument.FormFields("bkCEntity").Range.Start, _
End:=ActiveDocument.FormFields("bkDateI").Range.End)
With myRange
Dim intFF, intCountA As Integer
intFF = .FormFields.Count
For intCountA = 1 To intFF
If .FormFields(intCountA).Range.Font.Hidden = False
Then
.FormFields(intCountA).Range.Font.Hidden = True
End If
Next intCountA
End With
This range usually has only 4 formfields - which I mentioned above, and
intFF was returning 4 like it should.
The whole reason I am doing this is because the user uses the word
template to either be populated via an Access database with the
corresponding client and then prints it off for production. Or the user
can use the same template to edit client records - update, new, or
delete - directly. When the user enters edit mode, there are 4
formfields that would not be used in the database, they are for
production purposes only. The user has a problem with the gray shaded
formfields being there because they tab through the documents 40 or so
formfields to change the record data. Since the user doesn't really
look at where the cursor goes after tabbing through, the wrong
formfields gets updated. So I am trying to hide completely the
non-record ones during editing so this doesn't happen.
Because there are different toolbar buttons & userform buttons that the
user uses for the various tasks of editing I have to unprotect or
protect the document to help. This is the code I use:
Sub Recall()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
UserForm2.Show
End Sub
or
'Lock form for editing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect wdAllowOnlyFormFields
End If
But then I noticed that when the form was protected. The 4 non-record
formfields where no longer hidden, but they were not usable. I couldn't
click on then. So then I added some code to ensure they were enabled. I
didn't do anything conditional statements or any loops since this time
because I wasn't sure if enabling them would work at the time. In the
end the whole section of code looked like this:
UserForm2.Hide
With myRange
Dim intFF, intCountA As Integer
intFF = .FormFields.Count
For intCountA = 1 To intFF
If .FormFields(intCountA).Range.Font.Hidden = False
Then
.FormFields(intCountA).Range.Font.Hidden = True
End If
Next intCountA
End With
MsgBox "This form can now be edited." & vbCrLf & _
"Information can only be entered into the grey fields." &
vbCrLf & _
"Any data entered on this form will be added to the
database.", vbInformation, "Create New Record"
Unload Me
End If
'Lock form for editing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect wdAllowOnlyFormFields
End If
With ActiveDocument
.FormFields("bkCEntity").Enabled = True
.FormFields("bkDBA").Enabled = True
.FormFields("bkStateI").Enabled = True
.FormFields("bkDateI").Enabled = True
End With
But this time intFF is now returning 8 instead of 4 and now I am
getting the error: Run-time error '5941 The requested member of the
collection doesn't exist. So why does intFF return 8 now instead of 4?
I have replaced intFF with 4 in the code and it works just fine. But I
am curious as to what I did wrong, or could do different.
Thanks in advance.