check boxes

F

Fuzzhead

I have the following macro that will update a check box. My document has 25
check boxes. Is there a way to write one macro that will update all the boxes
instead of coping the following 25 times?

If frm.cb1.Value = True Then
ActiveDocument.FormFields("Check25").CheckBox.Value = 1
Else
ActiveDocument.FormFields("Check25").CheckBox.Value = 0
End If
 
G

Greg Maxey

Try

Dim i As Long

If frm.cb1.Value = True Then
For i = 1 to 25
ActiveDocument.FormFields("Check" & i).CheckBox.Value = 1
Next i
Else
For i = 1 to 25
ActiveDocument.FormFields("Check" & i).CheckBox.Value = 0
Next i
End If
 
G

Greg Maxey

Fuzzhead,

Not sure I understand exactly what you are trying to do, but if "frm" is
UserForm and "Check1" "Check2" etc., are Userform checkboxes you can iterate
through them as follows:

For i = 1 to 25
Msgbox Me.Controls("Check" & i). Value
Next i
 
G

Greg Maxey

Larry,

Provided you are using the default names for the Userform and Formfield
checkboxex, something like this should do:

Private Sub CommandButton1_Click()
Dim oFF As FormFields
Dim i As Long
Set oFF = ActiveDocument.FormFields
ActiveDocument.Unprotect
For i = 1 To 25
If Me.Controls("CheckBox" & i).Value = True Then
oFF("Check" & i).CheckBox.Value = True
Else
oFF("Check" & i).CheckBox.Value = False
End If
Next i
ActiveDocument.Protect wdAllowOnlyFormFields, True
Me.Hide
End Sub
 
F

Fuzzhead

Greg,

I'm not sure how to add what you said to my macro , so here it is. I also
see that you added the unprotect/protect document in your macro. My question
is if I protect my document how do I get my macros (like the following) to
run when the document needs to be edited?

Sub Plan()
Dim frm As frmplan
Dim MyDate
MyDate = Date
Set frm = New frmplan
On Error Resume Next
With ActiveDocument
frm.Text1.Value = .FormFields("Text39").Result
frm.Text2.Value = MyDate
frm.Text3.Value = .FormFields("Text5").Result
frm.Text4.Value = .FormFields("Text40").Result
frm.Check1.Value = .FormFields("Check1").Result
frm.Check2.Value = .FormFields("Check2").Result
frm.Check3.Value = .FormFields("Check3").Result
frm.Check4.Value = .FormFields("Check4").Result
End With
frm.Show
If frm.bIsOK Then
With ActiveDocument
ActiveDocument.FormFields("Text39").Result = frm.Text1.Text
ActiveDocument.FormFields("Text56").Result = frm.Text2.Text
ActiveDocument.FormFields("Text5").Result = frm.Text3.Value
ActiveDocument.FormFields("Text40").Result = frm.Text4.Value

If frm.Check1.Value = True Then
ActiveDocument.FormFields("Check1").CheckBox.Value = 1
Else
ActiveDocument.FormFields("Check1").CheckBox.Value = 0
End If
If frm.Check2.Value = True Then
ActiveDocument.FormFields("Check2").CheckBox.Value = 1
Else
ActiveDocument.FormFields("Check2").CheckBox.Value = 0
End If
If frm.Check3.Value = True Then
ActiveDocument.FormFields("Check3").CheckBox.Value = 1
Else
ActiveDocument.FormFields("Check3").CheckBox.Value = 0
End If
Fields.Update
End With
Else
ActiveDocument.Saved = True
End If
Set frm = Nothing
End Sub
 
G

Greg Maxey

Larrry,

First I would break your existing code up and put some of it in the UserForm
Initilize event and some in a UserForm CommandButton_Click event. Something
like this:

In your calling macro use:

Sub Plan()
Dim frm As frmPlan
Set frm = New frmPlan
frm.Show
Unload frm
Set frm = Nothing
End Sub

In the UserForm add a CommandButton (I captioned it with OK) and use:

Option Explicit
Private oFF As FormFields
Private Sub CommandButton1_Click()
Dim i As Long
Set oFF = ActiveDocument.FormFields
'If frm.bIsOK Then (I don't know what this is)
oFF("Text39").Result = Text1.Text
oFF("Text56").Result = Text2.Text
oFF("Text5").Result = Text3.Value
oFF("Text40").Result = Text4.Value
For i = 1 To 4
If Me.Controls("Check" & i).Value = True Then
oFF("Check" & i).CheckBox.Value = True
Else
oFF("Check" & i).CheckBox.Value = False
End If
Next i
'Fields.Update (I don't know what you want to do with this
ActiveDocument.Saved = True
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Dim MyDate
Set oFF = ActiveDocument.FormFields
MyDate = Date
Text1.Value = oFF("Text39").Result
Text2.Value = MyDate
Text3.Value = oFF("Text5").Result
Text4.Value = oFF("Text40").Result
Check1.Value = oFF("Check1").Result
Check2.Value = oFF("Check2").Result
Check3.Value = oFF("Check3").Result
Check4.Value = oFF("Check4").Result
End Sub

I have never heard of anyone using formfields in an unprotected document
;-). How are you runing your macro now? You could assign the macro "Plan"
to toolbar button or call if from a MacroButton field in the document itself
e.g., { Macrobutton Plan "Double click here to edit"}
 
G

Greg Maxey

Larry,

Checking out for a while, so if you still have questions someone else will
have to step in or I will reply when I am back online.
 
F

Fuzzhead

Greg,

That worked thank you. You guys and gals sure help in understanding how
macros are worded and work.

Yes my formfields are in a protected document and my question should be is
when the document is opened it can be viewed but if you try to edit a field
it would run a macro depending on what section of the document you are in.

Larry
 
G

Greg Maxey

Larry,

Still not exactly what scheme you are up to here ;-). Why don't you just
let the user update the fields directly in the protected form?

Anyway, to do what I think you want to do you will need to define an OnEntry
macro to run on entry to any of your formfields. The macro will determine
what section the field is in and run the appropriate macro to that section:

Sub MyOnEntry()
Select Case Selection.Information(wdActiveEndSectionNumber)
Case Is = 1
Plan 'Runs Plan macro
Case Is = 2
Test 'Runs Test macro
Case Is = 3
RunThisMacro ' Runs RunThis Macro
End Select
End Sub
 

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