Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Fill-In Form

Status
Not open for further replies.
Sep 12, 2006
111
US
I have a Word template that opens up a fill-in form for user to complete. The form has a series of text fields and about 9 check boxes. I am having trouble figuring out the vb code so when one of the check boxes is checked, and the "OK" button is clicked, the checkbox that appears on the word document are then checked.

Checkboxes have bookmarks assigned to them, checkbox1 - checkbox9. I am assuming an IF/THEN statement is what I need but I am unable to get it to work correctly.

The input fields for the text work great, I just need to get the checkboxes to work.

Any help is appreciated.


Private Sub cmdOK_Click()
If checkbox1 = True Then checkbox1 = True
If checkbox2 = True Then checkbox2 = True
If checkbox3 = True Then checkbox3 = True
If checkbox4 = True Then checkbox4 = True
If checkbox5 = True Then checkbox5 = True
If checkbox6 = True Then checkbox6 = True
If checkbox7 = True Then checkbox7 = True
If checkbox8 = True Then checkbox8 = True
If checkbox9 = True Then checkbox9 = True
End If
Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("Date").Range.Text = txtDate.Value
.Bookmarks("Company").Range.Text = txtCompany.Value
.Bookmarks("Attn").Range.Text = txtAttn.Value
.Bookmarks("CC").Range.Text = txtCC.Value
.Bookmarks("ProjectNumber").Range.Text = txtProjectNumber.Value
.Bookmarks("SentBy").Range.Text = txtSentBy.Value
End With
Application.ScreenUpdating = True
Unload Me
End Sub
 
Also, the check boxes are setup on the form in Frames:

Frame1: Frame2:
U.S MAIL FOR YOUR APPROVAL
U.P.S FOR YOUR FILE AND USE
EXPRESS FOR YOUR COMMENT
DELIVERY PER YOUR REQUEST
PICKUP

The user will check off one of the methods from each of the frames.
 
Please do not cross post. I just posted an reply to your same thread in the Office forum. Now...why should I write it again?

As this is a code thread, this is the correct forum.

I will simplify my response.

WHAT is your question????? It would help if you gave full details.

Your code (If checkbox9 = True Then checkbox9 = True). What do you think it does? Does it not look like it is making reference to the SAME thing? checkbox9.

If the checkboxes IN the document are formfields (you mention bookmarks, but that tells me that they are likely formfields), then to make them checked you have to identify them.

Are the formfields using the same names as the checkboxes on your userform? This is not a bad idea, in fact, it is a good idea - although using the default names (eg. checkbox9) is NOT a good idea.

Except...the default name for formfield checkboxes is "Check", as in "Check1", "Check2".

The default name for checkboxes on a userform is "Checkbox", as in "Checkbox1", "Checkbox2".

Please give full details. Once you do that, the solution is quite easy.

faq219-2884

Gerry
My paintings and sculpture
 
Correct, the checkboxes IN the document are Formfields and I have assigned each one a bookmark value of, "Check1", Check2" etc.

When the Fill-In dialog box opens up, I would like my users to select the checkbox(es) that they would like checked IN the document.

So for shipping method, I have a checkbox for "U.S MAIL" assigned "Check1". When the user checks that on the form and clicks "OK" the checkbox will be checked in the document for "Check1".

The checkboxes in the actual form are labeled, "checkbox1" "checkbox2" etc.

Let me know if more information is available.

Thank You much.
 
Use better names.

" I have assigned each one a bookmark value of, "Check1", Check2" etc."

No...you have not. YOU did not assign anything. The default name is "Check1", and Word assign it...not you.

Code:
If Checkbox1 = True Then _
   ActiveDocument.FormFields("Check1") _
       .CheckBox.Value = True

Checkbox1 is the checkbox on the userform, Check1 is the formfield in the document.

If you ARE going to use default names (Check1, Checkbox1), then you could do it like this:
Code:
Sub CommandButton1_Click()
Dim ctl As Control
Dim ctlName As String
Dim ctlNum As Long
For Each ctl In Me.Controls
   If TypeOf ctl Is MSForms.CheckBox Then
      If ctl = True Then
         ctlName = Left(ctl.Name, 5) ' gets Check
         ctlNum = Right(ctl.Name, 1) ' gets number
         ActiveDocument.FormFields(ctlName & ctlNum) _
            .CheckBox.Value = True
      End If
   End If
Next
Unload Me
End Sub
But, IMO, this is ugly coding due to poor use of names. Note, the number will only work up to 9, as it is getting only ONE digit.

In any case, as you did not really ask a question very well, the answer I think you want is:

You have to actually point to an object to do anything with it. You want to make a checkbox formfield checked? Then you have to point to it and tell it...you are checked.

That is what:

ActiveDocument.FormFields(name).CheckBox.Value = True

does. It points to the checkbox in the document, and sets its value to be True (checked).

Also, just to help you in later posts, do not use terms like "the Fill-In dialog box". It is called a userform. You, in fact, had to Insert....UserForm when you made...the userform.

UserForms may, or may not, be used for filling in things. It may be a "Fill in dialog" for you, in this case, but "userform" will tell us immediately what you are talking about.

faq219-2884

Gerry
My paintings and sculpture
 
Note, the number will only work up to 9
Gerry, what do you think about this ?
Code:
For Each ctl In Me.Controls
   If TypeOf ctl Is MSForms.CheckBox Then
      ActiveDocument.FormFields(Replace(ctl.Name, "box", "")) _
        .CheckBox.Value = ctl.Value
   End If
Next
 
ActiveDocument.FormFields(name).CheckBox.Value is what I need.

A IF/ELSE statment worked just for what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top