Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA form with checkbox's

Status
Not open for further replies.

Willie78

Technical User
Jun 4, 2004
67
0
0
GB
I have a simple bit of code.

All it’s doing is checking a check box for a tick if it has one then insert a bookmark from a file see below.

My question is the structure. Yes this works can you do it in a better way? I’m sure you can


Private Sub CommandButton1_Click()

Select Case testing

Case CheckBox1 = False
Selection.InsertFile FileName:="test list.docx", Range:="test1", _
ConfirmConversions:=False, Link:=False, Attachment:=False

End Select

Select Case testing2
Case CheckBox2 = False
Selection.InsertFile FileName:="test list.docx", Range:="test2", _
ConfirmConversions:=False, Link:=False, Attachment:=False

End Select


End Sub


Thank you in advance

Paul
 
Select Case is best for multiple possible values for the same variable.

Using it for ONE case is, ummmm, I don't want to say pointless, but I fail to see the point. Really, all you are testing with ONE case, is ONE case. So an If statement works fine. It is either True, or False.

Are you using Option Explicit? I notice that testing, and testing2 are not declared. What are they????

It could possibly be structured better if I knew what you are trying to do.

BTW: it is better to not keep default names, like Checkbox1, Checkbox2. These names mean nothing. It is better (in the long run) to give objects names that mean something.

So, questions. Will one OR the other checkbox be checked? Can both of them be blank? Both of them checked? It is knowing THAT logic that may help to improve your structure.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks for your response Gerry

It was a little test to see if i can do what I want. Which is

Have a front user form for selecting sections of a static document and creating a new document from the selection.

test1 and test2 are bookmarks in the testlist document.

There will be a fair few check box's one for each subject in the original document. If the appropriate box is checked then insert that section check the next box if ticked then insert that section if not move on.

I hope this makes more sense


Paul
 
Then you can loop through the controls, like this:
Code:
Dim ctl As Control
For Each ctl In Me.Controls
  If TypeOf ctl Is MSForms.Checkbox Then
     If Me.Controls(ctl.Name).Value = True Then
         ' your stuff
     End If
  End If
Next
Here is where precise and thoughtful naming comes to the fore.
Code:
Dim ThisDoc As Document
Dim oBM As Bookmark
Dim ctl As Control
Dim strName As String
Set ThisDoc = ActiveDocument
For Each ctl In Me.Controls
  If TypeOf ctl Is MSForms.Checkbox Then
     If Me.Controls(ctl.Name).Value = True Then
         strName = Right(ctl.Name, Len(ctl.Name) - 3
         Set oBm = ThisDoc.Bookmarks(strName)
         ThisDoc.Selection.InsertFile 
              FileName:="test list.docx", 
              Range:=oBM.Range, _
              ConfirmConversions:=False, _
              Link:=False, Attachment:=False
     End If
  End If
Next
The checkbox on the userform is chkTest2 (for example); the vookmark name is Test2.


So IF the checkbox is checked (True), then grab the name (chkTest2), strip off the first three characters (so = Test2); use that name to set a bookmark object (bookmark is named Test2), insert the file at the bookmark object (oBM) range.

This would run through all the checkboxes and change the variables appropriately. If they are named well.

faq219-2884

Gerry
My paintings and sculpture
 
Hi, Gerry.
Why this:
strName = Right(ctl.Name, Len(ctl.Name) - 3
instead of simply this ?
strName = Mid(ctl.Name, 4)
 
Because I am still grab old crap code from when I was just learning?

Willie78, always pay attention to anything PHV posts.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top