Userform and checkboxes

K

KHogwood-Thompson

Hi,

I have a template form in word that when opened shows the user a Userform to
complete. This form has several text boxes and several check boxes. I have
the following code, activated from the OK command button, to fill in the text
boxes based on the text keyed in to the Userform by the user:

Private Sub OK_Click()
With ActiveDocument
.Bookmarks("To").Range _
.InsertBefore ToTextBox
.Bookmarks("From").Range _
.InsertBefore FromTextBox
.Bookmarks("Subject").Range _
.InsertBefore SubjectTextBox
.Bookmarks("Text").Range _
.InsertBefore TextTextBox
End With

MemoDetails.Hide

End Sub


This works fine, the problem I have is filling in the check boxes on the
form based upon the checkboxes completed in the UserForm. Can someone assist
me with the coding. Here are the details:

Userform fields

ForInfoCheckBox
ForSafetyCheckBox
ForYellowCheckBox

Bookmarks on Word Form:

Check1
Check2
Check3

PS It is possible that more than one of the boxes is ticked.

Many thanks
 
G

Greg Maxey

Another method of filling the text bookmarks in your document is:

Dim oRng as Word.Range
Dim oBMs as Bookmarks
Set oBMs = ActiveDocument.Bookmarks
'Then for each BM
Set oRng = oBMs("To").Range
oRng.Text = ToTextBox.Text
oBMs.Add "To", oRng
'Repeat for other BMs

This writes the text "in" the bookmark and preserves the bookmark.

What are you using in the document to serve as Checkboxes?
 
K

KHogwood-Thompson

Ok thanks I will try that method.

The checkboxes are to capture instructions, the form itself is a template
for a memo and so the user would tick the checkboxes to indicate what the
receiver of the memo should do i.e for Information Only, to file etc
 
G

Greg Maxey

That doesn't really answer my question ;-)

Assuming that you are using FormField checkboxes in a protected Word
document you could use a method like this:

For i = 1 To 3
ActiveDocument.FormFields("Check" & i).CheckBox.Value =
Me.Controls("Checkbox" & i).Value
Next i

This assumes that your document formfield bookmarks for the formfield
checkboxes are named Check1, Check2, and Check3 and your UserForm
checkboxes are named Checkbox1, Checkbox2, Checkbox3
 
K

KHogwood-Thompson

Sorry about the lack of relevance there Greg,

I am not using a protected word for, the reason why is that when populating
it with the code that I attached in my original post, I get the error message
telling me that it cannot insert data into a protected area.

Is there any way around this, such as unprotecting the bookmarked areas?
--
K Hogwood-Thompson


Greg Maxey said:
That doesn't really answer my question ;-)

Assuming that you are using FormField checkboxes in a protected Word
document you could use a method like this:

For i = 1 To 3
ActiveDocument.FormFields("Check" & i).CheckBox.Value =
Me.Controls("Checkbox" & i).Value
Next i

This assumes that your document formfield bookmarks for the formfield
checkboxes are named Check1, Check2, and Check3 and your UserForm
checkboxes are named Checkbox1, Checkbox2, Checkbox3



Ok thanks I will try that method.

The checkboxes are to capture instructions, the form itself is a template
for a memo and so the user would tick the checkboxes to indicate what the
receiver of the memo should do i.e for Information Only, to file etc
--
K Hogwood-Thompson



Greg Maxey said:
Another method of filling the text bookmarks in your document is:
Dim oRng as Word.Range
Dim oBMs as Bookmarks
Set oBMs = ActiveDocument.Bookmarks
'Then for each BM
Set oRng = oBMs("To").Range
oRng.Text = ToTextBox.Text
oBMs.Add "To", oRng
'Repeat for other BMs
This writes the text "in" the bookmark and preserves the bookmark.
What are you using in the document to serve as Checkboxes?
On Jan 30, 10:59 am, KHogwood-Thompson
Hi,
I have a template form in word that when opened shows the user a Userform to
complete. This form has several text boxes and several check boxes. I have
the following code, activated from the OK command button, to fill in the text
boxes based on the text keyed in to the Userform by the user:
Private Sub OK_Click()
With ActiveDocument
.Bookmarks("To").Range _
.InsertBefore ToTextBox
.Bookmarks("From").Range _
.InsertBefore FromTextBox
.Bookmarks("Subject").Range _
.InsertBefore SubjectTextBox
.Bookmarks("Text").Range _
.InsertBefore TextTextBox
End With

End Sub
This works fine, the problem I have is filling in the check boxes on the
form based upon the checkboxes completed in the UserForm. Can someone assist
me with the coding. Here are the details:
Userform fields

Bookmarks on Word Form:

PS It is possible that more than one of the boxes is ticked.
Many thanks
 
G

Greg Maxey

Yes there is:

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
'Your code
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True

I've been playing around with another method that I like. Create two
autotext entries, one of a checked box symbole, and one of an empty box
symbol. (actually you could use any two symbols you like.) Be sure to store
the autotext entries in the template for your form (not Normal.dot).

Before actuallly saving the autotext entries, format the empty box sympbol
with font.color = wdColorAutomatic (will be black in the vast majority of
cases). Format the checked box symbol with font.color = wdColorBlack

Next place the empty box symbol (using Insert>Autotext) in the document at
each location a interactive empty/checked box needs to appear. Select at
bookmark the symbol CB1, then use CB2, etc.

In your UserForm, use a Checkbox to correspond with each CB in the document.

Private i As Long
Private Sub UserForm_Initialize()
Dim oBM As Bookmarks
Set oBM = ActiveDocument.Bookmarks
For i = 1 To 3 'or to however many checkboxes you are using.
If oBM("CB" & i).Range.Font.Color = wdColorAutomatic Then
Me.Controls("CheckBox" & i).Value = False
Else
Me.Controls("CheckBox" & i).Value = True
End If
Next i
End Sub

The initialize event above sets the UserForm Controls to match the "empty"
state of the checkboxes in any new document. If you use are allowing your
users to "edit" an existing document then the userform checkboxes will be
set to the checked or unchecked value of the CBs in the form.

This is the code to process the form:
Private Sub CommandButton1_Click()
Dim oRng As Word.Range
For i = 1 To 3
Set oRng = ActiveDocument.Bookmarks("CB" & i).Range
If Me.Controls("Checkbox" & i).Value = True Then
ActiveDocument.AttachedTemplate.AutoTextEntries("CB").Insert Where:=oRng,
RichText:=True
oRng.Font.Color = wdColorBlack
Else
ActiveDocument.AttachedTemplate.AutoTextEntries("UCB").Insert Where:=oRng,
RichText:=True
oRng.Font.Color = wdColorAutomatic
End If
ActiveDocument.Bookmarks.Add "CB" & i, oRng
Next i
Me.Hide
End Sub
 

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