AutoText

A

Anne P.

I have some questions about creating AutoText entries. This is going to be long-winded so bear with me, because there is a very specific reason for doing this.



I have been using the following code using two input boxes to create AutoText entries, and either assign that entry to an existing category (such as Mailing Instructions) or create a new category (such as Stamps).



Public Sub CreateNewAutoText()
Dim strATName As String
Dim strATCategory As String

'Get the AutoText Name
Dim Message, Title
Message = "Enter the name for the AutoText Entry" ' Set prompt.
Title = "Create AutoText Entry" ' Set title.
strATName = InputBox(Message, Title)



'Get the AutoText Category
Dim atMessage, atTitle
Message = "Enter the category for the AutoText Entry" ' Set prompt.
Title = "Create AutoText Entry" ' Set title.
strATCategory = InputBox(atMessage, atTitle)



Selection.CreateAutoTextEntry strATName, strATCategory
End Sub



This has worked very well, using input boxes. I have now set it up with a userform (which basically works the same as the above). Following is the code for the userform:



Public strATCategory As String
Public strATName As String

Private Sub cmdCancel_Click()
Unload Me
End Sub



Private Sub cmdOK_Click()
Selection.CreateAutoTextEntry strATName, strATCategory
Unload Me
End Sub



Private Sub txtATCategory_Change()
strATCategory = txtATCategory.Text
End Sub



Private Sub txtATName_Change()
strATName = txtATName.Text
End Sub



Using either of the above methods, an AutoText entry is created in Normal.dot. This is fine for most users on the network, however, I would like to extend the macro for IT staff so that when they create a new AutoText entry that should be firmwide, it can be created in their global template (currently SKGlobal.dot) and distributed to users, without overwriting the user's Normal.dot.



I know that most consultants recommend locking down Normal.dot (making it read only), but if you do this, you disable any customization on the user's part. They cannot create new AutoText, formatted AutoCorrect, Macros, or custom toolbars. This slows productivity on their part. What I do for my clients is create a new template that all new documents are based on (i.e., SKBlank.dot). The File, New menu option and the blank document icon on the standard toolbar (and the Word icons on the desktop or Programs menu) are remapped so that all new documents not based on another template (such as letter, memo, fax, etc) are based on this template rather than on Normal.dot. This way, the users can have access to store customizations of their own in Normal.dot.



I would like to extend the above userform for the IT staff at the company to create new AutoText entries in their global template (in this case, SKGlobal.dot) rather than in Normal.dot.



I have done some testing with the above procedures. They both work very well for creating AutoText entries in Normal.dot, with the name you specify and creating a new category if needed. All new entries created are stored in Normal.dot. However, if I then try to copy the new entries to the SKGlobal.dot, they are copied in a different way. Each entry is copied with the name of the AutoText entry as a category and as an AutoText entry under that category with the same name. For example, I previously created AutoText entries named Draft and Draft Autodate under the category Stamps, which was stored in Normal.dot. If I use the Organizer to copy those entries into SKGlobal.dot, then choose Insert, AutoText, I will see a category in the drop down list named Draft, which has an AutoText entry named Draft and a Category named Draft Autodate, which has an AutoText entry named Draft Autodate.



Is there anyway when using either of the above methods of creating new AutoText entries that I can specify the template that the AutoText should be stored in?



Sorry for the long-winded message, but I am desparate here.



Anne
 
J

Jay Freedman

Hi Anne,

The answer is as long-winded as the question, I'm afraid. :)

The big problem is that the Selection.CreateAutoTextEntry method lets
you specify a name and a category but not the template. In contrast,
the oTemplate.AutoTextEntries.Add method lets you specify the template
(as the object at the beginning of the expression) and the name, but
not the category -- the method automatically assigns the current
style's name as the category. This leads to some convoluted code.

Private Sub cmdOK_Click()
Dim oTemplate As Template
Dim strPath As String
Dim oNewStyle As Style
Dim bRemoveStyle

On Error GoTo ErrHdl
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\SKGlobal.dot"
Set oTemplate = Templates(strPath)

On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = ActiveDocument.Styles(strATCategory)
If Err.Number <> 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
ActiveDocument.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If

' change style of selection's paragraph to new category
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles(strATCategory)

' add AT entry to global template
oTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range

' reverse style assignment
ActiveDocument.Undo

' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If

Unload Me

ErrHdl:
MsgBox "Could not locate " & strPath
Unload Me
End Sub
 
A

Anne P.

Thanks,

I will give that a try.

Anne P.
Jay Freedman said:
Hi Anne,

The answer is as long-winded as the question, I'm afraid. :)

The big problem is that the Selection.CreateAutoTextEntry method lets
you specify a name and a category but not the template. In contrast,
the oTemplate.AutoTextEntries.Add method lets you specify the template
(as the object at the beginning of the expression) and the name, but
not the category -- the method automatically assigns the current
style's name as the category. This leads to some convoluted code.

Private Sub cmdOK_Click()
Dim oTemplate As Template
Dim strPath As String
Dim oNewStyle As Style
Dim bRemoveStyle

On Error GoTo ErrHdl
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\SKGlobal.dot"
Set oTemplate = Templates(strPath)

On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = ActiveDocument.Styles(strATCategory)
If Err.Number <> 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
ActiveDocument.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If

' change style of selection's paragraph to new category
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles(strATCategory)

' add AT entry to global template
oTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range

' reverse style assignment
ActiveDocument.Undo

' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If

Unload Me

ErrHdl:
MsgBox "Could not locate " & strPath
Unload Me
End Sub
 

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