loop 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
 
H

Helmut Weber

Hi Jake,

instead of trying to insert,
trapping a possible error and deleting afterwards,
you might check, whether such an autotextentry exists,

like that:

Sub Test6009()
Dim MyText As String
MyText = "MyAutoText"
Dim MyAuto As AutoTextEntry
For Each MyAuto In NormalTemplate.AutoTextEntries
If MyAuto.Name = "MyAutoText" Then
MsgBox "found"
End If
Next
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jake

Hi Helmut,
Great! Thanks, I thought of that, but wasn't sure how. I note that you can
also use
BuildingBlockEntries.Item to check if an AutoText entry exists in the new
Word 2007 template. I'm doing this for Word 2007.
Thanks again,
Jake
 

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