Re: Forming a sentence from a checkbox list by Gregory
Gregory
Fri Jul 11 19:31:52 PDT 2008
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
Gregory K. Maxey wrote:
> 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
>
> trance4mason@gmail.com wrote:
>> 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?