When you request an autotext entry, Word looks into the active template first, then in add in templates and finally in the normal template. This appears rather complex to arrange with vba. While you can insert an autotext entry using vba from the attached (active) template or from the normal template, if it is present relatively simply, provided you know its location and it is present. The problems arise when the location is not known or may not be present. The following macro first checks whether the document template is the normal template. If it is not, then the document template is checked for the entry. If it is or if the entry has not been found, the macro then checks all installed add-ins. Finally, if the normal template was not checked in the first step, it is now checked. If the entry is found in any of these locations the entry is inserted and the macro quits. If not the user is given a message to that effect. |
Sub InsertMyAutotext()
Dim oAT As AutoTextEntry
Dim oTemplate As Template
Dim oAddin As AddIn
Dim strText As String
Dim bFound As Boolean
'Define the required autotext entry
strText = "AutoText Name"
'Set the found flag default to False
bFound = False
'Ignore the attached template for now if the
'document is based on the normal template
If ActiveDocument.AttachedTemplate <> NormalTemplate Then
Set oTemplate = ActiveDocument.AttachedTemplate
'Check each autotext entry in the attached template
For Each oAT In oTemplate.AutoTextEntries
'Look for the autotext name
If oAT.name = strText Then 'if found insert it
oTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
'Set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
Exit Sub
End If
Next oAT
End If
Dim oAT As AutoTextEntry
Dim oTemplate As Template
Dim oAddin As AddIn
Dim strText As String
Dim bFound As Boolean
'Define the required autotext entry
strText = "AutoText Name"
'Set the found flag default to False
bFound = False
'Ignore the attached template for now if the
'document is based on the normal template
If ActiveDocument.AttachedTemplate <> NormalTemplate Then
Set oTemplate = ActiveDocument.AttachedTemplate
'Check each autotext entry in the attached template
For Each oAT In oTemplate.AutoTextEntries
'Look for the autotext name
If oAT.name = strText Then 'if found insert it
oTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
'Set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
Exit Sub
End If
Next oAT
End If
'Autotext entry was not found
If bFound = False Then
For Each oAddin In AddIns
'Check currently loaded add-ins
If oAddin.Installed = False Then Exit For
Set oTemplate = Templates(oAddin.Path & _
Application.PathSeparator & oAddin.name)
'Check each autotext entry in the current attached template
For Each oAT In oTemplate.AutoTextEntries
If oAT.name = strText Then 'if found insert it
oTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
'Set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
Exit Sub
End If
Next oAT
Next oAddin
End If
'The entry has not been found check the normal template
If bFound = False Then
For Each oAT In NormalTemplate.AutoTextEntries
If oAT.name = strText Then
NormalTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
bFound = True
Exit For
End If
Next oAT
End If
If bFound = False Then
For Each oAddin In AddIns
'Check currently loaded add-ins
If oAddin.Installed = False Then Exit For
Set oTemplate = Templates(oAddin.Path & _
Application.PathSeparator & oAddin.name)
'Check each autotext entry in the current attached template
For Each oAT In oTemplate.AutoTextEntries
If oAT.name = strText Then 'if found insert it
oTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
'Set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
Exit Sub
End If
Next oAT
Next oAddin
End If
'The entry has not been found check the normal template
If bFound = False Then
For Each oAT In NormalTemplate.AutoTextEntries
If oAT.name = strText Then
NormalTemplate.AutoTextEntries(strText).Insert _
Where:=Selection.Range
bFound = True
Exit For
End If
Next oAT
End If
'All sources have been checked and the entry is still not found
If bFound = False Then 'so tell the user.
MsgBox "Entry not found", vbInformation, "Autotext " _
& Chr(145) & strText & Chr(146)
End If
End Sub
If bFound = False Then 'so tell the user.
MsgBox "Entry not found", vbInformation, "Autotext " _
& Chr(145) & strText & Chr(146)
End If
End Sub
No comments:
Post a Comment