Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2007

Tip: Looking for answers? Try searching our database.

Userform and checkboxes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KHogwood-Thompson - 30 Jan 2007 15:59 GMT
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

Signature

K Hogwood-Thompson

Greg Maxey - 30 Jan 2007 16:17 GMT
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
<KHogwoodThomp...@discussions.microsoft.com> wrote:
> Hi,
>
[quoted text clipped - 41 lines]
> --
> K Hogwood-Thompson
KHogwood-Thompson - 30 Jan 2007 16:31 GMT
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
Signature

K Hogwood-Thompson

> Another method of filling the text bookmarks in your document is:
>
[quoted text clipped - 58 lines]
> > --
> > K Hogwood-Thompson
Greg Maxey - 30 Jan 2007 16:57 GMT
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

On Jan 30, 11:31 am, KHogwood-Thompson
<KHogwoodThomp...@discussions.microsoft.com> wrote:
> Ok thanks I will try that method.
>
[quoted text clipped - 66 lines]
> > > --
> > > K Hogwood-Thompson- Hide quoted text -- Show quoted text -
KHogwood-Thompson - 31 Jan 2007 08:03 GMT
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?
Signature

K Hogwood-Thompson

> That doesn't really answer my question ;-)
>
[quoted text clipped - 82 lines]
> > > > --
> > > > K Hogwood-Thompson- Hide quoted text -- Show quoted text -
Greg Maxey - 31 Jan 2007 10:25 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Sorry about the lack of relevance there Greg,
>
[quoted text clipped - 93 lines]
>>>>> --
>>>>> K Hogwood-Thompson- Hide quoted text -- Show quoted text -

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.