Check Box Array

F

Fabio Carvalho

I have setup a Word document with 9 checkboxes on it, each of which will be
executing very similar code. The only change in the code is actually the
number I have assigned to the corresponding objects that the given checkbox
affects. I was hoping to use an array to avoid having to put excess similar
code behind all 9 checkboxes. Any suggestions you can give me on how I can
use an array for these checkboxes will be greatly appreciated. I have
included the routine below. Thank you in advance for your assistance.

*************************************************************
Private Sub chkAppr1_Click()
On Error GoTo Err_chkAppr1_Click

If chkAppr1.Value = True Then
If chkReject1.Value = True Then
chkReject1.Value = False
End If
imgAppr1.Picture = LoadPicture("H:\Signature.bmp")
lblAppr1.Caption = Format(DateTime.Date, "m/d/yy")
imgAppr1.BackColor = &H8000000F
Else
imgAppr1.Picture = LoadPicture("")
lblAppr1.Caption = ""
imgAppr1.BackColor = &H8000000F
End If

Exit_chkAppr1_Click:
Exit Sub

Err_chkAppr1_Click:
If Err.Number = 76 Then
MsgBox "Your signature file could not be found. Please make sure
that your H:\ drive" _
& " is available and that a signature file is present within it. If
your H:\ drive is not" _
& " available, please reboot & relogin to your PC. If you do not
have a signature" _
& " file, please see your system administrator.", vbCritical +
vbOKOnly, "Signature Not Found"
Else
MsgBox Err.Description
End If
Resume Exit_chkAppr1_Click

End Sub
*********************************************************************
 
J

Jay Freedman

Hi, Fabio,

Although you didn't say, I assume your checkboxes and other controls were
inserted in the body of the document from the Control Toolbox toolbar,
rather than on a UserForm. If that's true, then all those controls are
members of the document's InlineShapes collection, and each has a .Type
property value of wdInlineShapeOLEControlObject. By looping through all the
members of the collection and comparing the names of the OLE objects to the
expected names, you can assign an Object to represent each control in the
current group. Then you can use the controls' properties.

It's easier to understand this by looking at the code than by talking about
it...

Private Sub chk_Click(i As Integer)
' generic click procedure for all checkboxes,
' called from the "real" procedures below
Dim oShp As InlineShape
Dim oChkCtrl As Object, oImgCtrl As Object
Dim oLblCtrl As Object, oRejCtrl As Object
Dim strChkName As String, strImgName As String
Dim strLblName As String, strRejName As String
Dim strObjectName As String

' construct names for current set of controls
strChkName = "chkAppr" & i
strImgName = "imgAppr" & i
strLblName = "lblAppr" & i
strRejName = "chkReject" & i

On Error GoTo Err_chk_Click

For Each oShp In ActiveDocument.InlineShapes
' assign objects to match names
If oShp.Type = wdInlineShapeOLEControlObject Then
strObjectName = oShp.OLEFormat.Object.Name

If strChkName = strObjectName Then
Set oChkCtrl = oShp.OLEFormat.Object
ElseIf strImgName = strObjectName Then
Set oImgCtrl = oShp.OLEFormat.Object
ElseIf strLblName = strObjectName Then
Set oLblCtrl = oShp.OLEFormat.Object
ElseIf strRejName = strObjectName Then
Set oRejCtrl = oShp.OLEFormat.Object
End If
End If
Next oShp

If oChkCtrl Is Nothing Then
' should never get here
MsgBox strChkName & " not found"
Exit Sub
End If

If oChkCtrl.Value = True Then
If oRejCtrl.Value = True Then
oRejCtrl.Value = False
End If
oImgCtrl.Picture = LoadPicture("c:\temp\usaflag.gif")
oLblCtrl.Caption = Format(DateTime.Date, "m/d/yy")
oImgCtrl.BackColor = &H8000000F
Else
oImgCtrl.Picture = LoadPicture("")
oLblCtrl.Caption = ""
oImgCtrl.BackColor = &H8000000F
End If

Exit_chk_Click:
Exit Sub

Err_chk_Click:
If Err.Number = 76 Then
MsgBox "Your signature file could not be found." _
& " Please make sure that your H:\ drive" _
& " is available and that a signature file is" _
& " present within it. If your H:\ drive is not" _
& " available, please reboot & relogin to your PC." _
& " If you do not have a signature" _
& " file, please see your system administrator.", _
vbCritical + vbOKOnly, "Signature Not Found"
Else
MsgBox Err.Description
End If
Resume Exit_chk_Click

End Sub

Private Sub chkAppr1_Click()
chk_Click i:=1
End Sub

Private Sub chkAppr2_Click()
chk_Click i:=2
End Sub

.... and similar _Click procedures for the rest of the checkboxes.
 
F

Fabio Carvalho

Hi Jay,

Thank you very much for your assistance. Once I took a look at your code,
everything made sense. Your assumption that my controls were from the
toolbox was correct and the code works perfectly. Thanks again.

Best Regards,
Fabio Carvalho
---------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top