Check Boxes in a UserForm (Word 2007)

G

Guest

I have a form I need to make in Word 2007 that requires check boxes. Each
check box represents a specific person/address that will populate a certain
part of the document. I don't want to have to use an external table. If
possible I would like the code itself to contain the information that will
populate the document, based on your checkbox selection. Hopefully this
makes sense as I have no idea.

Thanks in advance for your help.

Brian
 
D

Doug Robbins - Word MVP

Your code will be easier to maintain, if the names and addresses are stored
in an external table, rather than in the code itself.

The following may give you a few ideas:

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

You could run a macro something along the lines of the following on exit
from the checkbox.

Sub OnExitCheckBox1()
'fills text field based on content of _
checkbox field

Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
Select Case oFld("Check1").CheckBox.Value
Case Is = True
oFld("Text1").Result = "This is what is entered in Text1 field if the
checkbox is checked"
Case Is = False
oFld("Text1").Result = "This is what is entered in Text1 field if the
checkbox is unchecked"
Case Else
'Do nothing
End Select
End Sub

see also http://www.gmayor.com/SelectFile.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Guest

How would I have to set up my table to insert addresses? Basically I want it
to highlight and individuals name, and then at the proper bookmark in the
document, insert their address information in this format:

Name
Street 1
Street 2
City, State, Zip
Telephone

Thanks again for your prompt responses, I appreciate it.
 
G

Guest

Is there a way to call the information from an excel spreadsheet? I
appreciate your time and help in this so far. What you have here makes sense
to me, except the table part. Does every item need to be in a seperate
portion of the table, such as column 1 would be the name of the individual,
column 2 would be street address 1, column 3 would be street address 2, and
column 4 would be city, state, zip?

Thanks again.

Brian
 
D

Doug Robbins - Word MVP

It's a lot easier if you have all of the information in Word as it saves
interoperability issues, even though they are not major, with transferring
information from one application to another.
The code that I gave you is written to deal with the information being in
multiple columns as you suggest, though it does not have to be and the code
is written to deal with any number of columns, though to get the
configuration that you want, e.g. city, state and zip on one line, a slight
modification would be required.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Guest

Well it wouldn't be a problem for me to seperate the city, state and zip, and
I could make it in a Word table, thats not a problem either. I guess i would
place a bookmark in the document for each column from the table? Also I
guess the script does its search of the table based on the name on the list
(so they would have to match I'm guessing). Again I appreciate your help as
a newbie to this.

Brian
 

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