VB Runtime Error 91

G

gouved

I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 
M

macropod

Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.
 
G

gouved

Thank you for your response. I changed the code as follows and it worked:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range

Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
MsgBox ("2" & CStr(StrVal))
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
End If


End Sub

When filling out the form, the document is protected, and it generates an
error on trying to assign text to the bookmark. While the macro correctly
reads the chkbox and assigns the value to a bookmark, once the form is
protected after the write, the box is unchecked. Can you suggest a solution
for this?

macropod said:
Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 
M

macropod

Hi gouved,

Did you try running the code as supplied?

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
Thank you for your response. I changed the code as follows and it worked:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range

Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
MsgBox ("2" & CStr(StrVal))
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
End If


End Sub

When filling out the form, the document is protected, and it generates an
error on trying to assign text to the bookmark. While the macro correctly
reads the chkbox and assigns the value to a bookmark, once the form is
protected after the write, the box is unchecked. Can you suggest a solution
for this?

macropod said:
Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 
G

gouved

I guess you mean the code I supplied. Yes I ave been running it. It works.
It just keeps unchecking the chkbox after reprotecting the document. I need
to see if there is code to set the default on the checkbox before the form is
reprotected.

macropod said:
Hi gouved,

Did you try running the code as supplied?

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
Thank you for your response. I changed the code as follows and it worked:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range

Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
MsgBox ("2" & CStr(StrVal))
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
End If


End Sub

When filling out the form, the document is protected, and it generates an
error on trying to assign text to the bookmark. While the macro correctly
reads the chkbox and assigns the value to a bookmark, once the form is
protected after the write, the box is unchecked. Can you suggest a solution
for this?

macropod said:
Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.

--
Cheers
macropod
[MVP - Microsoft Word]


I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 
M

macropod

Hi gouved,

The reason your code is losing the checkbox state is that you're unprotectting the document, then reprotecting it without telling
Word to retain the field status. You can preserve their state via:
NoReset:=True

Try this:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim P_Status
Dim strPassword As String
strPassword = "Password"
With ActiveDocument
P_Status = .ProtectionType
If .ProtectionType <> wdNoProtection Then .Unprotect (strPassword)
.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
If P_Status > -1 Then .Protect Type:=P_Status, Noreset:=True, Password:=strPassword
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
I guess you mean the code I supplied. Yes I ave been running it. It works.
It just keeps unchecking the chkbox after reprotecting the document. I need
to see if there is code to set the default on the checkbox before the form is
reprotected.

macropod said:
Hi gouved,

Did you try running the code as supplied?

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
Thank you for your response. I changed the code as follows and it worked:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range

Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
MsgBox ("2" & CStr(StrVal))
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
End If


End Sub

When filling out the form, the document is protected, and it generates an
error on trying to assign text to the bookmark. While the macro correctly
reads the chkbox and assigns the value to a bookmark, once the form is
protected after the write, the box is unchecked. Can you suggest a solution
for this?

:

Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.

--
Cheers
macropod
[MVP - Microsoft Word]


I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 
G

gouved

Brilliant! The switch does the job. And your code shows off a real shortcut
by using a with statement for Active.document. Much appreciated. Again,
Thank you.
Gouved

macropod said:
Hi gouved,

The reason your code is losing the checkbox state is that you're unprotectting the document, then reprotecting it without telling
Word to retain the field status. You can preserve their state via:
NoReset:=True

Try this:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim P_Status
Dim strPassword As String
strPassword = "Password"
With ActiveDocument
P_Status = .ProtectionType
If .ProtectionType <> wdNoProtection Then .Unprotect (strPassword)
.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
If P_Status > -1 Then .Protect Type:=P_Status, Noreset:=True, Password:=strPassword
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


gouved said:
I guess you mean the code I supplied. Yes I ave been running it. It works.
It just keeps unchecking the chkbox after reprotecting the document. I need
to see if there is code to set the default on the checkbox before the form is
reprotected.

macropod said:
Hi gouved,

Did you try running the code as supplied?

--
Cheers
macropod
[MVP - Microsoft Word]


Thank you for your response. I changed the code as follows and it worked:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range

Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
MsgBox ("2" & CStr(StrVal))
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyFormFields
End If


End Sub

When filling out the form, the document is protected, and it generates an
error on trying to assign text to the bookmark. While the macro correctly
reads the chkbox and assigns the value to a bookmark, once the form is
protected after the write, the box is unchecked. Can you suggest a solution
for this?

:

Hi gouved,

If you want to update a formfield, as your code suggests, use something like:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Fields(1).Result.Text = TextToUse
End Sub

You shouldn't need to use Text = CStr(TextToUse), since you've already specified that 'TextToUse' must be a string.

Neither should you need to unprotect/reprotect the document.

--
Cheers
macropod
[MVP - Microsoft Word]


I copied a procedure from an article cited in this discussion group on how to
assign values to a field. The routine is:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Set aDoc = ActiveDocument
Dim BMRange As Range
BMRange.Text = CStr(TextToUse)
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
Else
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
aDoc.Protect Type:=wdAllowOnlyRevisions, Password:=strPassword
End If

I then wrote code to translate a checkbox as either true or false and call
the procedure above to set the value of the field accordingly:

Sub SetIfBenefitBox()
'
' SetIfBenefitBox Macro
' Macro created 12/1/2008 by Edgar Velazquez
'
Dim StrVal As String
With ActiveDocument
If .FormFields("IfBenefit").CheckBox.Value = True Then
MsgBox ("chkbox is checked")
StrVal = True
Else
MsgBox ("chkbox is not checked")
StrVal = False

End If
UpdateBookmark "Benefit", CStr(StrVal)
End With
End Sub


When I pass the checkbox, the checkbox macro sends the msgbox, but on
calling the subroutine, I get the VB Runtime Error 91, with the editor
identifying the problem on assigning the BMRange.text for:

BMRange.Text = TextToUse

and: BMRange.Text = CStr(TextToUse)

Can anyone please help?
 

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