Macro won't work, please help!

P

Poli

I have CheckBox "Check1" and a FormField "Duration". My macro is supposr to
put "48.6" in the FormField "Duration" when the checkbox has a check, and
when the checkbox is empty "Duration" will also be empty.
Windows XP, Word 2007
Can someone help me with this macro. The error message says "Block if
without end If"

My 'Check1" checkbox is suppose to run the macros on enter.

Here is my macro

Sub IsChecked()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "48.6"
Else
If ActiveDocument.FormFields("Check1").CheckBox.Value = False Then
ActiveDocument.FormFields("Duration").Result = ""
End If
End Sub
 
J

Jay Freedman

I have CheckBox "Check1" and a FormField "Duration". My macro is supposr to
put "48.6" in the FormField "Duration" when the checkbox has a check, and
when the checkbox is empty "Duration" will also be empty.
Windows XP, Word 2007
Can someone help me with this macro. The error message says "Block if
without end If"

My 'Check1" checkbox is suppose to run the macros on enter.

Here is my macro

Sub IsChecked()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "48.6"
Else
If ActiveDocument.FormFields("Check1").CheckBox.Value = False Then

The problem is here. By having the Else and the If on separate lines, you've
created what should an If...End If structure nested inside the Else clause of
the outer If...Else...End If structure. But this nested structure only has an If
without any End If.
ActiveDocument.FormFields("Duration").Result = ""
End If
End Sub

This would be more evident if you indent the code properly:

Sub IsChecked()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "48.6"
Else
If ActiveDocument.FormFields("Check1").CheckBox.Value = False Then
ActiveDocument.FormFields("Duration").Result = ""
' <== no End If here!
End If
End Sub

There are two ways to fix it: Either insert the missing End If statement, or
mash together the Else and the nested If into an ElseIf:

Sub IsChecked()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "48.6"
ElseIf ActiveDocument.FormFields("Check1").CheckBox.Value = False Then
ActiveDocument.FormFields("Duration").Result = ""
End If
End Sub

Even better, you can recognize that if the value isn't True then it must be
False, so you don't need the second If. This is completely equivalent to the
previous version:

Sub IsChecked()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "48.6"
Else
ActiveDocument.FormFields("Duration").Result = ""
End If
End Sub
 
P

Poli

That did work great, thank you so much, but I have another problem, maybe you
can help me or guide me to a place that will help.
I have about 18 checkboxs and I need the macro to uncheck the previous
checkbox when the next checkbox is selected. Here is what I have: It works
fine but when I select "Check2, I have to uncheck "Check1 for "Check 2" to
work. I want it to automatically uncheck "Check1", when I select "Check2"

Sub IsChecked1()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "59"
End If
If ActiveDocument.FormFields("Check2").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "1 Hr 8 minutes"
End If
If ActiveDocument.FormFields("Check3").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "2 Hr 3 minutes"
End If
End Sub
 
J

Jean-Guy Marcil

Poli said:
That did work great, thank you so much, but I have another problem, maybe you
can help me or guide me to a place that will help.
I have about 18 checkboxs and I need the macro to uncheck the previous
checkbox when the next checkbox is selected. Here is what I have: It works
fine but when I select "Check2, I have to uncheck "Check1 for "Check 2" to
work. I want it to automatically uncheck "Check1", when I select "Check2"

Sub IsChecked1()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "59"
End If
If ActiveDocument.FormFields("Check2").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "1 Hr 8 minutes"
End If
If ActiveDocument.FormFields("Check3").CheckBox.Value = True Then
ActiveDocument.FormFields("Duration").Result = "2 Hr 3 minutes"
End If
End Sub

You mean something like this:

With ActiveDocument.FormFields
If .Item("Check1").CheckBox.Value = True Then
.Item("Duration").Result = "59"
.Item("Check2").CheckBox.Value = False
.Item("Check3").CheckBox.Value = False
End If
If .Item("Check2").CheckBox.Value = True Then
.Item("Duration").Result = "1 Hr 8 minutes"
.Item("Check1").CheckBox.Value = False
.Item("Check3").CheckBox.Value = False
End If
If .Item("Check3").CheckBox.Value = True Then
.Item("Duration").Result = "2 Hr 3 minutes"
.Item("Check1").CheckBox.Value = False
.Item("Check2").CheckBox.Value = False
End If
End With


But, as you can see, if you have 18 boxesm or more, this will quickly become
a nightmare.

So try this instead:


Sub IsChecked1()

With ActiveDocument.FormFields

If .Item("Check1").CheckBox.Value = True Then
.Item("Duration").Result = "59"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
If .Item("Check2").CheckBox.Value = True Then
.Item("Duration").Result = "1 Hr 8 minutes"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
If .Item("Check3").CheckBox.Value = True Then
.Item("Duration").Result = "2 Hr 3 minutes"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
End With

End Sub

Private Sub TurnOffCheckBox(BoxName As String)

Dim i As Long

With ActiveDocument.FormFields
For i = 1 To .Count
If .Item(i).Type = wdFieldFormCheckBox Then
If .Item(i).Name <> BoxName Then
.Item(i).CheckBox.Value = False
End If
End If
Next
End With

End Sub

Note that to make this work 100%, the final click on the form must be in
some other fields that is not one of the checkboxes. Until the user does
that, there may be two "true" checkboxes (That is, assuming you are using the
checkbox "onExit" macro event).
 
P

Poli

Hey Jean, Thank you so much for the help, I got it to work but it works very
slow. Here is my Macro, is there any other way to make it work faster,
because I have 32 checkboxes that need to give a different value?
Sub CheckedIt()
With ActiveDocument.FormFields

If .Item("Check1").CheckBox.Value = True Then
.Item("Duration").Result = "1 hr 8 min"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
If .Item("Check2").CheckBox.Value = True Then
.Item("Duration").Result = "2 Hr 8 minutes"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
If .Item("Check3").CheckBox.Value = True Then
.Item("Duration").Result = "3 Hr 8 minutes"
End If
If .Item("Check4").CheckBox.Value = True Then
.Item("Duration").Result = "4 Hr 8 minutes"
End If
If .Item("Check5").CheckBox.Value = True Then
.Item("Duration").Result = "5 Hr 8 minutes"
End If
If .Item("Check6").CheckBox.Value = True Then
.Item("Duration").Result = "6 Hr 8 minutes"
End If
If .Item("Check7").CheckBox.Value = True Then
.Item("Duration").Result = "7 Hr 8 minutes"
End If
If .Item("Check8").CheckBox.Value = True Then
.Item("Duration").Result = "8 Hr 8 minutes"
End If
If .Item("Check9").CheckBox.Value = True Then
.Item("Duration").Result = "9 Hr 8 minutes"
End If
If .Item("Check10").CheckBox.Value = True Then
.Item("Duration").Result = "10 Hr 8 minutes"
End If
If .Item("Check11").CheckBox.Value = True Then
.Item("Duration").Result = "11 Hr 8 minutes"
End If
If .Item("Check12").CheckBox.Value = True Then
.Item("Duration").Result = "12 Hr 8 minutes"
End If
If .Item("Check13").CheckBox.Value = True Then
.Item("Duration").Result = "13 Hr 8 minutes"
End If
If .Item("Check14").CheckBox.Value = True Then
.Item("Duration").Result = "14 Hr 8 minutes"
End If
If .Item("Check15").CheckBox.Value = True Then
.Item("Duration").Result = "15 Hr 8 minutes"
End If
If .Item("Check16").CheckBox.Value = True Then
.Item("Duration").Result = "16 Hr 8 minutes"
End If
If .Item("Check17").CheckBox.Value = True Then
.Item("Duration").Result = "17 Hr 8 minutes"
End If
If .Item("Check18").CheckBox.Value = True Then
.Item("Duration").Result = "18 Hr 8 minutes"
End If
If .Item("Check19").CheckBox.Value = True Then
.Item("Duration").Result = "19 Hr 8 minutes"
End If
If .Item("Check20").CheckBox.Value = True Then
.Item("Duration").Result = "20 Hr 8 minutes"
End If
If .Item("Check21").CheckBox.Value = True Then
.Item("Duration").Result = "21 Hr 8 minutes"
End If
If .Item("Check22").CheckBox.Value = True Then
.Item("Duration").Result = "22 Hr 8 minutes"
End If
If .Item("Check23").CheckBox.Value = True Then
.Item("Duration").Result = "23 Hr 8 minutes"
End If
If .Item("Check24").CheckBox.Value = True Then
.Item("Duration").Result = "24 Hr 8 minutes"
End If
If .Item("Check25").CheckBox.Value = True Then
.Item("Duration").Result = "25 Hr 8 minutes"
End If
If .Item("Check26").CheckBox.Value = True Then
.Item("Duration").Result = "26 Hr 8 minutes"
End If
If .Item("Check27").CheckBox.Value = True Then
.Item("Duration").Result = "27 Hr 8 minutes"
End If
If .Item("Check28").CheckBox.Value = True Then
.Item("Duration").Result = "28 Hr 8 minutes"
End If
If .Item("Check29").CheckBox.Value = True Then
.Item("Duration").Result = "29 Hr 8 minutes"
End If
If .Item("Check30").CheckBox.Value = True Then
.Item("Duration").Result = "30 Hr 8 minutes"
End If
If .Item("Check31").CheckBox.Value = True Then
.Item("Duration").Result = "31 Hr 8 minutes"
End If
If .Item("Check32").CheckBox.Value = True Then
.Item("Duration").Result = "32 Hr 8 minutes"
End If
End With
End Sub

Private Sub TurnOffCheckBox(BoxName As String)

Dim i As Long

With ActiveDocument.FormFields
For i = 1 To .Count
If .Item(i).Type = wdFieldFormCheckBox Then
If .Item(i).Name <> BoxName Then
.Item(i).CheckBox.Value = False
End If
End If
Next
End With

End Sub
 
J

Jean-Guy Marcil

Poli said:
Hey Jean, Thank you so much for the help, I got it to work but it works very
slow. Here is my Macro, is there any other way to make it work faster,
because I have 32 checkboxes that need to give a different value?
Sub CheckedIt()
With ActiveDocument.FormFields

If .Item("Check1").CheckBox.Value = True Then
.Item("Duration").Result = "1 hr 8 min"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If
If .Item("Check2").CheckBox.Value = True Then
.Item("Duration").Result = "2 Hr 8 minutes"
TurnOffCheckBox Selection.Bookmarks(1).Range.FormFields(1).Name
End If


Name your checkboxes as follows:

Check01
Check02
etc.
Check32

Then use this instead:

Sub CheckedIt()

Dim strName As String
Dim strValue As String

With ActiveDocument.FormFields
strName = Selection.Bookmarks(1).Range.FormFields(1).Name
If .Item(strName).CheckBox.Value = True Then
strValue = Right(strName, 2)
.Item("Duration").Result = strValue & " Hr 8 minutes"
TurnOffCheckBox strName
End If
End With

End Sub


Private Sub TurnOffCheckBox(BoxName As String)

Dim i As Long

With ActiveDocument.FormFields
For i = 1 To .Count
If .Item(i).Type = wdFieldFormCheckBox Then
If .Item(i).Name <> BoxName Then
.Item(i).CheckBox.Value = False
End If
End If
Next
End With

End Sub
 
P

Poli

Jean, Thank you so much for your help, but I think that it is me that does
not know how to ask questions. Your macro worked fine, but I thought the
macro was straight forward and I could use anything I wanted in the value "1
hr. 8 min". I only used increments of 1 hr as an example. I really need a
macro where I can later type in whatever value I want to use.

Here is my problem: Here is my table (i have 30 more thicknesses)

Example Thicknesses in Inches 0.63 0.75
1.3 hr/inch minimum 1.30 = 0.81 0.98
1.5 hr/inch maximum 1.50 = 1.22 1.46
Calculation for 1/3hr/inch minimum is 1.30 X 0.63 thickness = 0.81 or 48.6
min.
Same for the 1.5 hr/inch maximum 1.50 X .063 thickness - 1.22 or 73.2 min.
and so on and so on
I have 16 thicknesses for the 1.3 hr and 16 thicknesses for the 1.5 hr for a
total of 32 thicknesses.

I thought that if I could figure out the macro I could then calculate my own
thicknesses for everthing and work it into the macro, but I can see that you
have the macro work out the thicknesses by 1 hr increments, just like I did
the example. Sorry, I did not make myself understood.

I will understand if you cannot help me. But if you can then I will be very
happy.

Thank you.
Poli
 
D

Doug Robbins - Word MVP

I think that rather than using checkboxes, you would probably be better off
using a DropDown type formfield that contains a list of the thicknesses and
then have a textbox formfield into which you enter the rate/inch and then
use a macro to multiply the selected entry in the DropDown by the entry in
the Rate/Inch formfield and display the duration as the .Result of the
Duration formfield.

It would of course really be better if you told us exactly what you are
starting with and what you want to achieve rather than the way in which you
have going about it as that way may not be the best way to achieve the
desired result.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Poli

Thank you so much Doug, here is what I changed to:
I have made two dropdown Fields:
Dropdown-"Thickness" contains all my thicknesses
Dropdown-"MinMax" contains two items to select-"1.3 hr/inch minimum" & "1.5
hr/inch maximum"
FormField -"Duration"

I need a macro that when you select a ListEntry in the dropdown field
"Thickness", and then select a ListEntry in the dropdown field "MinMax", if
you select ListEntry "1.3 hr/inch minimum" then you will multiply the
selected ListEntry from dropdown field "Thickness" by 1.30 and the value
should be put into Formfield "Duration", but if you select ListEntry "1.5
hr/inch maximum" then you multiply the selected ListEntry from dropdown field
"Thickness" by 1.50.

I tried to make the macro but did not do very well:

Sub Thickness()
With ActiveDocument
If .Formfield.DropDown.ListEntries = "1.3 hr/inch minimum" Then
.Formfield ("Duration").Result = "Thickness*1.30"
ElseIf
If .Formfield.DropDown = "1.5 hr/inch maximum" Then
.Item("Duration").Result = "Thickness*1.50"
End If
End With
End Sub

Again thank you Doug and I hope that you will help me.

Poli
 
D

Doug Robbins - Word MVP

Run a macro containing the following code on exit from both of the DropDown
formfields

Dim Rate As Long, Thick As Long
Dim strRate As String, strThick As String
Dim valRate As Double, valThick As Double
With ActiveDocument
With .FormFields("MinMax").DropDown
Rate = .Value
strRate = .ListEntries(Rate).Name
valRate = Val(Left(strRate, 3))
End With
With .FormFields("Thickness").DropDown
Thick = .Value
strThick = .ListEntries(Thick).Name
valThick = Val(strThick)
End With
.FormFields("Duration").result = valRate * valThick
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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