Error 5941 with Bookmarked Formfield

D

Dylan

Error 5941 - The requested member of the collection does not exist.

I get this error trying to apply a calendar result to a bookmarked
formfield. The macro pauses and the following line is highlighted in yellow:

With rng.Bookmarks(1)
.Range.FormFields(1).Result = Format(ctrlCal.Value, "dd MMM yyyy")

Here is the full code I'm using:

Private Sub ctrlCal_Click() 'Calendar Control
Dim rng As Range
Dim bProtected As Boolean

Set rng = Selection.Range

'Unprotect
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

With rng.Bookmarks(1)
.Range.FormFields(1).Result = Format(ctrlCal.Value, "dd MMM yyyy")
End With

Unload Me

'.Range.FormFields("Text19").Select

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If

End Sub
 
J

Jean-Guy Marcil

Dylan said:
Error 5941 - The requested member of the collection does not exist.

I get this error trying to apply a calendar result to a bookmarked
formfield. The macro pauses and the following line is highlighted in yellow:

With rng.Bookmarks(1)
.Range.FormFields(1).Result = Format(ctrlCal.Value, "dd MMM yyyy")

Here is the full code I'm using:

Private Sub ctrlCal_Click() 'Calendar Control
Dim rng As Range
Dim bProtected As Boolean

Set rng = Selection.Range

'Unprotect
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

With rng.Bookmarks(1)
.Range.FormFields(1).Result = Format(ctrlCal.Value, "dd MMM yyyy")
End With

Unload Me

'.Range.FormFields("Text19").Select

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If

End Sub

Just to make sure... How have you ascertained that
Selection.Range
does in fact contain a bookmark?

What is "Selection.Range"?
 
D

Dylan

Selection.Range is the selected formfield. The formfield contains an on-enter
field which opens the calendar.
 
D

Dylan

It works if I use
With rng.Cells(1)
But, I have a formfield that isn't in a table.
 
D

Diane

Group,
I could run a script that would change the font - and that might be my only
solution, but what I really want /need to do, is find the error message
"Error, Bookmark not defined" within MSWORD and change the font there,
avoiding running any scripts to change this font.
Diane
 
D

Dylan

Graham, To apply this (code, below) directly to a formfield would I need to
have many calendars within the document to address each field?

Also, I have a table which automatically adds new rows, containing
formfields, to the end of the table. How would I reference the new fields as
they are added.

Private Sub Calendar1_Click()
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
oFld("Text2").Result = Format(Calendar1.Value, "dd mmmm yyyy")
Unload Me
End Sub

If I knew how to apply this to a selected field?
 
D

Diane

wrong post.....I'll try again.

Diane said:
Group,
I could run a script that would change the font - and that might be my only
solution, but what I really want /need to do, is find the error message
"Error, Bookmark not defined" within MSWORD and change the font there,
avoiding running any scripts to change this font.
Diane
 
D

Dylan

The Formfield is bookmarked and the formfield properties contains the
bookmark name.

How do I check that selection.range contains the bookmark?
 

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