So something like 40 lines of code is better than On Error Resume Next,
which in some circumstances at least can be a perfectly acceptable
method of dealing with a situation, or better than making use of the
specific Error Number that is returned to perform a specific action if
that error occurs?
You have not said what that action should be and rather than cast
aspersions on the original programmer who used the On Error Resume Next,
I would sit back and think about what your successor will think of you
when he comes across those 40 odd lines of code.
--
Hope this helps,
Doug Robbins - Word MVP
Please reply only to the newsgroups unless you wish to obtain my
services on
a paid professional basis.
Graham,
Thanks very much for this. It's just what I was looking for. I really
appreciate it! (Thank you to Lene too for your input)
Keith
While I suspect Doug's approach will be the more realistic, your
question
raises a question that might be worth exploring further.
I take it we can assume Word before 2007, as Word 2007 moved the
goalposts
with respect to autotext entries with the introduction of building
blocks?
When you request an autotext entry, Word looks into the active
template
first, then in addin templates and finally in the normal template.
This
appears rather more complex to arrange with vba (though if better
programmers than I are watching they will no doubt jump in).
While you can insert an autotext entry using vba from the attached
(active) template or from the normal template if it is present, it is
not
possible to do so from an addin template without making that template
the
active template. You could therefore check for the presence of the
entry
in the active template then attach each add-in template in turn and
check
for the presence of the autotext entry before restoring the original
attached template. Finally checking the normal template. If the entry
is
found in any of these locations the macro quits. If not the user is
given
a message to that effect.
Dim oAT As AutoTextEntry
Dim strAttach As String
Dim strFilename As String
Dim addInPath As String
Dim oAddIn As Template
Dim bFound As Boolean
strAttach = Templates(1).Path _
& Application.PathSeparator _
& ActiveDocument.AttachedTemplate
addInPath = Options.DefaultFilePath(wdStartupPath)
strFilename = Dir$(addInPath & "\*.dot")
bFound = False
For Each oAT In ActiveDocument.AttachedTemplate.AutoTextEntries
If oAT.name = "klr" Then
ActiveDocument.AttachedTemplate.AutoTextEntries("klr").Insert _
Where:=Selection.Range
bFound = True
Exit For
End If
Next oAT
If bFound = False Then
While Len(strFilename) <> 0
ActiveDocument.AttachedTemplate = addInPath _
& Application.PathSeparator _
& strFilename
For Each oAT In ActiveDocument.AttachedTemplate.AutoTextEntries
If oAT.name = "klr" Then
ActiveDocument.AttachedTemplate.AutoTextEntries("klr").Insert _
Where:=Selection.Range
bFound = True
Exit For
End If
Next oAT
strFilename = Dir$()
Wend
ActiveDocument.AttachedTemplate = strAttach
End If
If bFound = False Then
For Each oAT In NormalTemplate.AutoTextEntries
If oAT.name = "klr" Then
NormalTemplate.AutoTextEntries("klr").Insert _
Where:=Selection.Range
bFound = True
Exit For
End If
Next oAT
End If
If bFound = False Then
MsgBox "Autotext entry not found"
End If
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site
www.gmayor.com
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Well I tried this in the Access.FormsCoding newsgroup and got nothing
so
I'm giving it a shot here.
I'm cleaning up some code from a previous programmer who liked to put
"On
Error Resume Next" in places where he didnt' know how to test things
in
lines above code that might crash. For example, there are lines like
the
following:
Selection.InsertAfter "krh"
On Error Resume Next
Selection.Range.InsertAutoText
I really want to take otu the "On Error Resume Next" and replace it
with
a
test to see if the AutoTextEntry exists as follows:
Selection.InsertAfter "krh"
If <autotext entry("krh")> exists Then
Selection.Range.InsertAutoText
End If
But I have no idea how to do that. I know I can use NormalTemplate in
some
way but that is a problem in and of itself because we're nto using
the
Normal Template all the time. I need to search that and another as
well.
Anyway, some specific info on how to do this would be wonderful.
Thanks,
Keith