PDA

View Full Version : Solved: Else without If?



BonnyeLiz
08-26-2005, 07:50 AM
Hi Gang,

OK - I'm willing to accept that there is something amazingly simple here that I am missing. Throw cottonballs at will ;)

I have a userform where the user selects one of three buttons to identify what documents are then imported into the active document to complete a package. When I test it, selection of the first button imports the correct docs and works fine. However, any other button selection does nothing. When debugging, I get the "Else without If" error. I don't see it? Do you?


If OptDeputy.Value = True Then
With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "DWageGunOath.doc"
ElseIf OptCo.Value = True Then
With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "COWageOath.doc"
Else: OptNa.Value = True Then
ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True

Tommy
08-26-2005, 08:03 AM
Looks like it is here

Else: OptNa.Value = True Then


Also I do not see any End With

TonyJollans
08-26-2005, 08:09 AM
Hi Bonnye,

You can't intermingle Ifs and Withs like that.
It's also safer to avoid using Else:
and you can't have Then after Else

Try something like ..

With Selection
If OptDeputy Then
' bla bla bla
ElseIf OptCo Then
' bla bla bla some more
Else
' whatever
End If
End With

BonnyeLiz
08-26-2005, 08:09 AM
Adjusted according to your suggestion - same error repeats. However, here is the entire Sub:


Private Sub CmdGenerate_Click()
Dim docpath As String
docpath = ActiveDocument.Path
With ActiveDocument

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
.Unprotect

.FormFields("Name1").Result = newname.Value
.FormFields("name2").Result = newname.Value
.FormFields("name3").Result = newname.Value
.FormFields("name4").Result = newname.Value
.FormFields("name5").Result = newname.Value


If OptDeputy.Value = True Then
With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "DWageGunOath.doc"

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True


ElseIf OptCo.Value = True Then

With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "COWageOath.doc"

ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True


Else: OptNa.Value = True

ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True


End If
End With
End If
End With
End If
End With
Me.Hide
End Sub

BonnyeLiz
08-26-2005, 08:11 AM
Tony,

Thanks! I will edit per your suggestion and report back shortly!

:)
Bonnye

BonnyeLiz
08-26-2005, 08:22 AM
Thank You! Problem solved and the form/template is working wonderfully :friends:

:bow:
Bonnye

Tommy
08-26-2005, 08:23 AM
Hi BonnyeLiz,

I think this is what you are trying to do. As Tony says mixing the withs and ifs mixs things up :)


Private Sub CmdGenerate_Click()
Dim docpath As String
docpath = ActiveDocument.Path
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then ActiveDocument.Unprotect

ActiveDocument.FormFields("Name1").Result = newname.Value
ActiveDocument.FormFields("name2").Result = newname.Value
ActiveDocument.FormFields("name3").Result = newname.Value
ActiveDocument.FormFields("name4").Result = newname.Value
ActiveDocument.FormFields("name5").Result = newname.Value
If OptDeputy.Value = True Then
With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCallsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "DWageGunOath.doc"
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ElseIf OptCo.Value = True Then
With Selection
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "FRSCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "CJSTCCOsworn.doc"
.EndKey 6
.InsertBreak 2
.InsertFile docpath & "\" & "COWageOath.doc"
End With
ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True
Else
OptNa.Value = True
ActiveDocument.Protect wdAllowOnlyFormFields, noreset:=True
End If
Me.Hide
End Sub