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

Re: loop validation by Helmut

Helmut
Wed Apr 02 08:06:19 PDT 2008

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

Re: loop validation by Jake

Jake
Wed Apr 02 10:59:01 PDT 2008

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
"Helmut Weber" wrote:

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