![]() |
|
|||||||
|
|
Thread Tools | Display Modes |
|
#4
|
||||
|
||||
|
Andrew
Your point about insertion order is a good one, and the code could indeed be reduced along the lines you suggest, but could be harder for a beginner to follow. The real problem with this approach is that the inserted text is not going to be 'Sentence1', 'Sentence2' etc., which is fairly easy to accommodate (see below); but real world sentences are not. We then have to consider the use of arrays to hold the texts, which adds more complexity - thus: Code:
Private Sentence(8) As Variant
Private Sub CommandButton1_Click()
Dim aCtl As Control, sResult As String
Dim sText As String
Sentence(0) = "This is the first sentence."
Sentence(1) = "This is the second sentence."
Sentence(2) = "This is the third sentence."
Sentence(3) = "This is the fourth sentence."
Sentence(4) = "This is the fifth sentence."
Sentence(5) = "This is the sixth sentence."
Sentence(6) = "This is the seventh sentence."
Sentence(7) = "This is the eighth sentence."
Sentence(8) = "This is the ninth sentence."
For Each aCtl In Controls
If aCtl.Name Like "CheckBox*" And aCtl = True Then
sText = GetNum(aCtl.Name)
sResult = Concat(sResult, CStr(Sentence(Val(sText) - 1)))
End If
Next aCtl
'MsgBox sResult
FillBM "BookMark_Name", sResult
Unload Me
End Sub
Private Function Concat(s1 As String, s2 As String, Optional sJoin As String = vbCr)
If s1 = "" Then
Concat = s2
ElseIf s2 = "" Then
Concat = s1
Else
Concat = s1 & sJoin & s2
End If
End Function
Private Function GetNum(sText As String) As String
Dim i As Integer
For i = 1 To Len(sText)
If Mid(sText, i, 1) >= "0" And Mid(sText, i, 1) <= "9" Then
GetNum = GetNum + Mid(sText, i, 1)
End If
Next
lbl_Exit:
Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Field Code: Show a value only if bookmark is not empty
|
Cosmo | Word | 7 | 08-24-2018 01:46 PM |
Removing spaces in activedocument after empty bookmarks
|
faustino909 | Word VBA | 2 | 08-03-2018 01:34 PM |
VBA, Place Sentence in its own Line
|
ilcaa72 | Word VBA | 6 | 04-28-2017 07:01 AM |
Trying to read CSV file and place values into word bookmarks
|
Philip1 | Word VBA | 5 | 10-27-2016 12:37 AM |
Excel vba to check to check if two columns are empty
|
subspace3 | Excel Programming | 5 | 07-09-2015 04:45 PM |