Forming a sentence from a checkbox list

T

trance4mason

Hiya - first post alert!

I'm try to get word to form a sentence from user selected checkboxes,
for example a user selects the items that they want and then word
forms a sentence such as "i want X, Y and Z" So that it inserts commas
and the word "and" in the correct place.

i tried having a count, for the items and then when the count is > 1
getting word to type ", " but this code never called.

any help please?
 
D

Doug Robbins - Word MVP

What type of form/checkboxes?

--
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
 
F

fumei via OfficeKB.com

And it may help if you posted some actual code.

Hiya - first post alert!

I'm try to get word to form a sentence from user selected checkboxes,
for example a user selects the items that they want and then word
forms a sentence such as "i want X, Y and Z" So that it inserts commas
and the word "and" in the correct place.

i tried having a count, for the items and then when the count is > 1
getting word to type ", " but this code never called.

any help please?
 
G

Gregory K. Maxey

Lets assume that you are talking about formfield checkboxes in a protected
document.

I created 8 checkboxes bookmarked "Check1" through "Check8"

Code something like this should do:

Sub ScratchMacro()
Dim oFFs As FormFields
Dim i As Long
Dim pStr As String
Dim pTempStr As String
Set oFFs = ActiveDocument.FormFields
For i = 1 To 8
If oFFs("Check" & i).CheckBox.Value = True Then
pStr = pStr & Choose(i, "A", "B", "C", "D", "E", "F", "G", "H") & ", "
End If
Next i
pStr = Left(pStr, Len(pStr) - 2)
i = InStrRev(pStr, ",")
pTempStr = Right(pStr, Len(pStr) - i)
pStr = Left(pStr, i - 1)
pStr = pStr & " and" & pTempStr
MsgBox pStr
End Sub
 
G

Gregory K. Maxey

Allow me to refine that code a bit. I discovered that the string
manipulation could be done with fewer steps but regardless it would throw an
error if only one box where checked. This seems to work better:

Sub ScratchMacroII()
Dim oFFs As FormFields
Dim i As Long
Dim pStr As String
Set oFFs = ActiveDocument.FormFields
For i = 1 To 8
If oFFs("Check" & i).CheckBox.Value = True Then
pStr = pStr & Choose(i, "A", "B", "C", "D", "E", "F", "G", "H") & ", "
End If
Next i
pStr = Left(pStr, Len(pStr) - 2)
On Error Resume Next
pStr = Left(pStr, InStrRev(pStr, ",") - 1) & " and" & Mid(pStr,
InStrRev(pStr, ",") + 1)
On Error GoTo 0
pStr = pStr
MsgBox pStr
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