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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check a Checkbox in Word 2010 VBA 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,548
US

I can place a Checkbox in my document in VBA - which is the same as going to Developer tab, Controls, and select Checkbox. I have several of them in my document. By default they come up as Unchecked.

I know I can do this:

Code:
Dim ctrl As ContentControl
For Each ctrl In ActiveDocument.ContentControls
    If ctrl.Type = wdContentControlCheckBox Then
        ctrl.Checked = False
    End If
Next

but that makes all of them Checked.

How do I refer to a particular Checkbox in code?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 


Might this work?

Control 5...
Code:
  ActiveDocument.ContentControls(5).Checked = False

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
I'd approach the problem differently, bu setting the checked state at the time of creation. For example:
Code:
Dim CCtrl As ContentControl
Set CCtrl = ActiveDocument.ContentControls.Add(Type:=wdContentControlCheckBox, Range:=Selection.Range)
CCtrl.Checked = True
This approach allows you to set their checked/unchecked state as they're created. Of course, since the default is unchecked, you really only need the extra code for those you want to make checked.

Cheers
Paul Edstein
[MS MVP - Word]
 
How are ya Andrzejek . . .

The only two ways left to identify a control is their index (sample by [blue] SkipVought[/blue]), or the control name (I'm sure you already know this):
Code:
[blue]   Dim ctrl As ContentControl, ctlNames As String
   
   [purple][b]ctlNames[/b][/purple] = "Name1Name2Name3 ... Namex"
   
   For Each ctrl In ActiveDocument.ContentControls
      If ctrl.Type = wdContentControlCheckBox Then
         [purple][b]If InStr(ctlNames, ctrl.Name) > 0 Then[/b][/purple]
            ctrl.Checked = False
         End If
      End If
   Next[/blue]

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
I am sure the other ways would work, too, but I went with macropod's sollution.

Thank you.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andrzejek . . .

Confusing. In your post origination you present a [blue]For each[/blue] loop which can only refer to an [purple]existing[/purple] collection. Then you ask
Andrzejek said:
[blue]How do I [purple]refer to a particular[/purple] Checkbox in code?[/blue]
Again referencing something [purple]existing[/purple] ... and finally selecting [purple]creation code![/purple]

You really got me on this one ...

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Sorry about the confusion. Trying to help my co-worker and I put something together as ‘logical’ as I could at that time. Then co-worker found the solution that matched macropod's post before I had a chance to look at the replies.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andrzejek . . .

Roger That ...

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top