looping validation

J

Jake

I have a form that accepts AutoText entries. If the AutoText entry doesn't
exist I prompt the user with an Inputbox function to enter the entry again.
But there is a line that deletes the previous entry: Selection.Delete. If
the user keeps entering the wrong AutoText entry more is deleted than is
required.
Here is sample code that runs when user clicks OK on form:

On Error Resume Next
Dim x As String
Dim y As String
Selection.GoTo What:=wdGoToBookmark, Name:="MyTest"
Selection.Text = frmAutoTextTest.txtFirstAutoText
Selection.Range.InsertAutoText
Do While Err.Number <> 0
Err.Number = 0
Selection.Delete
frmAutoTextTest.txtFirstAutoText.SetFocus
frmAutoTextTest.txtFirstAutoText = ""
x = InputBox("Enter First AutoText again", "Re-enter AutoText")
Selection.Text = x
If x = vbCancel Then
Exit Sub
End If
Selection.Range.InsertAutoText
Loop
frmAutoTextTest.txtSecondAutoText.SetFocus
Selection.GoTo What:=wdGoToBookmark, Name:="AnotherTest"
Selection.Text = frmAutoTextTest.txtSecondAutoText
Selection.Range.InsertAutoText
Do While Err.Number <> 0
Err.Number = 0
Selection.Delete
frmAutoTextTest.txtSecondAutoText.SetFocus
frmAutoTextTest.txtSecondAutoText = ""
y = InputBox("Enter Second AutoText again", "Re-enter AutoText")
Selection.Text = x
If x = vbCancel Then
Exit Sub
End If
Selection.Range.InsertAutoText
Loop
Unload Me

Thanks for any help,
Jake
 
P

Per Jessen

Hello Jake

I assume you only want to delete selection once. If you want to allow
several delete actions you need to apply a counter.

Try this:

On Error Resume Next
Dim x As String
Dim y As String
Dim sDel As Boolean
Selection.GoTo What:=wdGoToBookmark, Name:="MyTest"
Selection.Text = frmAutoTextTest.txtFirstAutoText
Selection.Range.InsertAutoText
sDel = False
Do While Err.Number <> 0
Err.Number = 0
If sDel = False Then
Selection.Delete
sDel = True
End If
frmAutoTextTest.txtFirstAutoText.SetFocus
frmAutoTextTest.txtFirstAutoText = ""
x = InputBox("Enter First AutoText again", "Re-enter AutoText")
Selection.Text = x
If x = vbCancel Then
Exit Sub
End If
Selection.Range.InsertAutoText
Loop
sDel = False
frmAutoTextTest.txtSecondAutoText.SetFocus
Selection.GoTo What:=wdGoToBookmark, Name:="AnotherTest"
Selection.Text = frmAutoTextTest.txtSecondAutoText
Selection.Range.InsertAutoText
Do While Err.Number <> 0
Err.Number = 0
If sDel = False Then
Selection.Delete
sDel = True
End If
frmAutoTextTest.txtSecondAutoText.SetFocus
frmAutoTextTest.txtSecondAutoText = ""
y = InputBox("Enter Second AutoText again", "Re-enter AutoText")
Selection.Text = x
If x = vbCancel Then
Exit Sub
End If
Selection.Range.InsertAutoText
Loop
Unload Me

Regards,
Per
 

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