Trying to get name inserted if a checkbox is checked

B

Bill

Hi, I have two check boxes in a user form. If one checkbox
is checked I want a certain name inserted at a bookmark.
If the other check box is checked I want a different name
inserted at the bookmark.

How do I query the checkbox and insert the correct code?

Here is my code:

Private Sub CheckForm_Click()
Set vCompanyName = "My Company"
If ActiveDocument.FormFields("CheckForm").CheckBox.Value =
True Then
.Bookmarks("bkWhichCompany").Range _
.InsertBefore vCompanyName
End If
End Sub

Private Sub OtherCheckForm_Click()
Set vOtherCompanyName = "Other Company"
If ActiveDocument.FormFields
("OtherCheckForm").CheckBox.Value = True Then
.Bookmarks("bkWhichCompany").Range _
.InsertBefore vOtherCompanyName
End If
End Sub
 
B

Bill

Sorry, I've been cleaning the code up and this is the
current version of the code. When I run it I get a Run
Time error 5941:

Private Sub CheckForm_Click()
If ActiveDocument.FormFields("CheckForm").CheckBox.Value =
True Then
With ActiveDocument
.Bookmarks
("bkWhichCompany").Range.InsertBefore "My Company"
End With
Else
With ActiveDocument
.Bookmarks
("bkWhichCompany").Range.InsertBefore "Other Company"
End With
End If
End Sub
 
D

Doug Robbins - Word MVP

Hi Bill,

You will have to unprotect the document to do that:

ActiveDocument.Unprotect



ActiveDocument.Protect wdAllowOnlyFormFields, NoReset


Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
B

Bill

Hi Doug et al, I inserted the code into the VB code as
outlined below and saved the document. I ask to create a
new document (File - New) from the templated document but
when I click on the check box and enter the password I
still get the same Run Time 5941 Error.

Any suggestions? I thought I might be querying the wrong
document with the "ActiveDocument" part but that is
obviously not the reason.

My code now looks like this:

Private Sub CheckForm_Click()
ActiveDocument.Unprotect
If ActiveDocument.FormFields("CheckForm").CheckBox.Value =
True Then
With ActiveDocument
.Bookmarks
("bkWhichCompany").Range.InsertBefore "My Company"
End With
Else
With ActiveDocument
.Bookmarks
("bkWhichCompany").Range.InsertBefore "Other Company"
End With
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
End Sub
 
B

Bill

By the way, I'm getting the error at the "If
ActiveDocument" line and the exact error message is "Run
Time Error 5941 - The requested member of the collection
does not exist".

Thanks
 
B

Bill

I got it working by simply removing the
ActiveDocument.FormFields("") in the IF statement. This
advice came from Microsoft. It was explained that since
the checkbox is queried as soon as it is checked, there
was no need to tell the code where the check box is
located.

Thanks for the 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