Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert AutoText via Macro from Startup Template

Status
Not open for further replies.

cheriberi

Technical User
May 27, 2003
60
US
Is it possible to search for specific text in a document and, if found, move the cursor to the start of the text and then insert an AutoText entry that is found in a separate template located in the Startup folder?

Thanks!

Cheryl
[ponder]
 
When would you be trying to run the code? On an autoexec macro or simailar or via a toolbar button?

Just curious as to the above. As to what you want - it's quite possible, but you'll have to add a few bits into your code to access the autotext from your global (startup) template.

I suggest you use the macro recorder to quickly do the search code and select the range you want to add the autotext, and then you'll have to add a procedure like:


Public sub InsertGlobalAutoText(AutextName as string)

Dim intTemplate as integer

for intTemplate = 1 to Application.templates.count
If application.templates(intTemplate).name = "Startup.dot" then
Application.template(intTemplate).autotextentries(autextname).insert where:=selection.range
Exit For
End If
Next intTemplate

End sub

This makes sure that (A) it can find the template where you have the autotext, and (B) the autotext is actually there so you'll not get errors if either if these conditions are false.

Remember to replace "Startup.dot" with the name of your template.

Also passing the name of the autotext you require to the procedure means you can specify different autotexts for different pieces of text. However you will have to write the procedure that determines this. (Quite simple - use a select case to determine the text and then pass the required autotext name dependent upon the case).

Remember that you'll have to add the procedure above to the template of the document where you are doing the text search, not the startup.dot, although it is possible to call a global template procedure using the Application.Run method. Trickier though unless you are experienced in VBA.

Hope this proves of help.
Asjeff
 
The problem is that I can't put a macro in the actual document. So I was hoping that by setting up the AutoText entries in a Template in the Startup folder, I'd be able to use them in any document.
 
You should be able to. Set up your macro in a global template (ie one you save in the startup folder) and it should work with any document provided that document is the activedocument

Asjeff
 
I tried that, but if I try to run the macro that should insert the AutoText entry, nothing is inserted. It works if I put the AutoText entries on a Toolbar menu in the global template, but there are 12 AutoText entries that would always be inserted, so I'd rather have a macro do it to make sure the right one goes in the right place.

Here is my code:

Code:
Sub AddAutoText()

Dim intTemplate As Integer
Dim strAutoTextName As String

strAutoTextName = "MyAutoText"

' Find AutoText insertion point (before specific text)
Selection.Find.ClearFormatting
With Selection.Find
    .Text = "Insert before this text"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.MoveLeft unit:=wdCharacter, Count:=1

' Make sure template is found and AutoText is there so no error occurs
For intTemplate = 1 To Application.Templates.Count
    If Application.Templates(intTemplate).Name = "My Template.dot" Then
        Application.Templates(intTemplate).AutoTextEntries(strAutoTextName).Insert Where:=Selection.Range
        Exit For
    End If
Next intTemplate

End Sub

Thanks for your help with this!

Cheryl
 
Hi Cheryl

Sorry - haven't been ignoring you. Very quickly, because I'm under the cosh at the moment - can you let me know if your autotexts are text based or graphic based?

Suspect that we might be able to do it along the lines of:

(I have passed the name of the autotext through to the procedure but you could just as easily determine the name within it instead)


sub AddAutoText (AutextName as string)
Dim myRange as range
set myrange = activedocument.content
myrange.find.execute findtext:="Insert before this text", Forward:=true
if myrange.find.found = true then
with myrange.find.replacement
.text = InsertGlobalAutoText ("AutotextName")
'you could also try: .value = InsertGlobalAutoText ("AutotextName") - see how it goes
end with

myrange.find.execute Replace:=wdReplaceAll
End if

And then we would have to set "InsertGlobalAutoText" as a public function, so

Public Function InsertGlobalAutoText(AutextName as string)

Dim intTemplate as integer

for intTemplate = 1 to Application.templates.count
If application.templates(intTemplate).name = "Startup.dot" then
Application.template(intTemplate).autotextentries(autextname).insert where:=selection.range
Exit For
End If
Next intTemplate

End function


Both procedures will have to be in your global template. Please forgive me becausse I haven't got the time to really test thsi for you, but I will put an email notification on this post so that I will know as soon as you reply.

I'm still not sure as to what criteria you are using to initiate the procedures - and have you ensured that the autotext are actually saved in the global template you are using and not the normal.dot or other template?

Let me know how you get on

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top