Solved

How to create dynamic check boxes in VBA

Posted on 2014-03-26
8
557 Views
Last Modified: 2014-04-06
I have a userform that will contain text strings.  The text strings will be kept in a separate document so the strings can be added to, deleted or modified.

I won't know how many of them there will be or how many the user will want to choose, so I want to create a check box with one of the strings next to it, and continue that until all text strings and check boxes have been created.

That will allow the user to choose as many text strings as they need.

What I need is the code to accomplish the creation of the text boxes with the associated strings

Thanks for the help
0
Comment
Question by:c7c4c7
  • 4
  • 2
  • 2
8 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39958200
The scenario wasn't too precise but to demonstrate I have made some assumptions:
The strings documdent might be open but if not open it for the purpose
The strings document is a defined document i.e. not necessarily the active one
The document containing the macro is the current one
The strings are entered as paragraphs

On this assumption then try:

Sub Q_28398555()
Dim doc As Document
Dim bolFound As Boolean
Dim arrStrings As Variant
Dim frm As UserForm
Dim intCount As Integer
Const strDocName = "Q_28398555.docx"
Const strdocPath = "C:\"

    bolFound = False
    For Each doc In Application.Documents
        If LCase(doc.Name) = LCase(strDocName) Then
            bolFound = True
            Exit For
        End If
        Set doc = Nothing
    Next
    If Not bolFound Then Set doc = Application.Documents.Open(strdocPath & strDocName)
    arrStrings = Split(doc.Range.Text, vbCr)
    Load UserForm1
        For intCount = LBound(arrStrings) To UBound(arrStrings)
            If arrStrings(intCount) <> "" Then
                With UserForm1.Controls.Add("Forms.CheckBox.1", "Checkbox_" & intCount, True)
                    .Top = 20 * intCount
                    .Caption = arrStrings(intCount)
                End With
            End If
        Next
    UserForm1.Show
End Sub

Open in new window


Chris
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39958209
Hi c7c4c7 - Rename zip to dotm.
UserFormExample--II.zip
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39958217
Hi Chris,

Hope you're well. You must have posted just as I started :-)   I think this question relates to an earlier question.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39958231
Hi yourself, I have seen you around a few times but Feb/March tends to be a quiet period for me.

I take your point that it was an ongoing question but there was no mention to this in the question and as a passing comment I understood the number of lines of text was an unknown that will vary in use therefore your solution looks (to my interpretation) as though it doesn't quite hit the spot ;o)

Chris
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Expert Comment

by:DrTribos
ID: 39958248
I only mentioned it to give some BG info, I agree with your assessment - mine is a bit to the left of center.

With clarificaiton from the OP I'm thinking a userform could work if it is driven by a macro that loops through the rows in a table. Or tables in a document.

Just providing a starting spot.

Cheers,
0
 

Author Comment

by:c7c4c7
ID: 39959265
I haven't had a chance to work with the responses.  Dr. Tribos is correct it is an extension of the previous question.  

I won't have time to work on this until later today but thanks for the posts
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39960273
No worries - let us know how you go
0
 

Author Closing Comment

by:c7c4c7
ID: 39981413
Thanks for the help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now