Autotext in a macro

B

Brenda A. Reid

Help please. The following is part of my letterhead template. I have
autotext entries to select the lawyer for the letterhead. I now have some
lawyers with 4 numbers. 4 numbers will not work but any with 3 numbers
will. Can anyone tell me why an autotext entry like Lawyer8888 will not
work but Lawyer888 will?


Private Sub Document_New()

Dim LawyerNo As String
Dim WrongNumber As Long
Dim Finished As Boolean
Dim MyAutotext As AutoTextEntry
Dim CurDoc As Document

Set CurDoc = ActiveDocument
Finished = False

With CurDoc
Do While Not Finished
'To make sure LawyerNo can be tested
'If Null value, error is generated
'So assign invalid value to start loop
LawyerNo = "A"
'Make sure user typed a number
Do While Not IsNumeric(Trim(LawyerNo))
LawyerNo = InputBox("Type lawyer number.", "Lawyer Details")
If Not IsNumeric(Trim(LawyerNo)) Then
MsgBox "You must type a number.", vbExclamation, "Number
required"
End If
Loop
'Make sure number correponds to existing autotext
For Each MyAutotext In .AttachedTemplate.AutoTextEntries
If MyAutotext.Name = "Lawyer" & LawyerNo Then
Finished = True
Exit For
End If
Next MyAutotext
If Finished Then
'If autotext exists, insert it
.AttachedTemplate.AutoTextEntries("Lawyer" _
& LawyerNo).Insert Where:=.Bookmarks("LawyerDetails") _
.Range, RichText:=True
Else
'If autotext does not exist, ask user to try again or not
OK = 1
cancel = 2
WrongNumber = MsgBox("The number you entered is not valid." _
& vbCrLf & vbCrLf & "Try again?", _
vbExclamation + vbOKCancel, "Invalid number")
If WrongNumber = 2 Then Exit Sub
End If
Loop
End With
End Sub
 
G

Greg

Brenda,

Works for me. Are you sure that you have correctly defined AutoText
entries for Lawyer888 and Lawyer8888?

While I am not saying that my proposed changes are best or even better,
I did take the liberty to condense your code a bit.

Private Sub Document_New()
Dim LawyerNo As String
With ActiveDocument
Retry:
'Make sure user typed a number
Do
LawyerNo = InputBox("Type lawyer number.", "Lawyer Details")
If Not IsNumeric(Trim(LawyerNo)) Then
MsgBox "You must type a number.", vbExclamation, "Number
required"
End If
Loop Until IsNumeric(Trim(LawyerNo))
'Insert the AutoText Entry
On Error GoTo Handler
.AttachedTemplate.AutoTextEntries("Lawyer" _
& LawyerNo).Insert Where:=.Bookmarks("LawyerDetails") _
.Range, RichText:=True
End With
Exit Sub
Handler:
If MsgBox("The number you entered is not valid." _
& vbCrLf & vbCrLf & "Try again?", _
vbExclamation + vbOKCancel, "Invalid number") = vbOK Then
Resume Retry
End If
End Sub

Rather than check every single AutoText entry, I figure just try to
insert it and let the error that will be generated if it doesn't exist
redirect the user to the Input box or Cancel.

Actually there are two things in the code that code cause the error.
The AutoText Entry doesn't exist or the Bookmark doesn't exist. With
this being an Auto_New macro I don't figure there is much chance of the
bookmark being deleleted by the user.

I will watch this string in hopes a really bright VBA guy or gal comes
along to school us both in even more refinements.

HTH
 

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