Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Autotext in a macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brenda A. Reid - 05 Jan 2006 13:21 GMT
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
Greg - 05 Jan 2006 14:49 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.