Adding Autotext

G

Greg Maxey

Hello,

I am playing around with an addin for Word. I have saved the addin as a
..dot file. When the addin runs creates an AutoText entry:

ActiveDocument.AttachedTemplate.AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range

I need for the entry to be stored in the addin .dot template instead of the
Normal template which is what occurs now.

I tried
TemplateNameTemplate.AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range
And
TemplateName.AutoTextEntries.Add Name:="seqDate", Range:=Selection.Range

where template name is the name of my template but I got and object error.

So I have a template that is function as an Addin. When the addin macro
runs it creates an AutoText entry. I need the entry to be stored in the
addin template and not the normal template. Thanks.
 
G

Greg Maxey

OK,

I have learned that:

Templates(?).AutoTextEntries.Add Name .....

will work if I know the index number.

I cobbled together the following to figure out the index number of the
template as the number can change if other templates and addin are involved

i = 0
For Each oTmp In Templates
i = i + 1
If oTmp.Name = "DateSequencer.dot" Then
Exit For
End If
Next

Then I use:
Templates(i).AutoTextEntries.Add .....

Why does this have to be so hard. Why can't I just use my template name
like:
DateSequencer.AutoTextEntries.Add.... ???


Thanks.
 
J

Jonathan West

Greg Maxey said:
Hello,

I am playing around with an addin for Word. I have saved the addin as a
.dot file. When the addin runs creates an AutoText entry:

ActiveDocument.AttachedTemplate.AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range

I need for the entry to be stored in the addin .dot template instead of
the Normal template which is what occurs now.

For that, you use ThisDocument. ThisDocument refers to the template
containing the code that is running.

ThisDocument.AutoTextEntries.Add Name:="seqDate", Range:=Selection.Range
 
H

Helmut Weber

Hi Greg,

instead of a number as index you may use the templates fullname.
s = oTmp.fullname
Templates(s).AutoTextEntries.Add "Test3", Selection.Range

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg Maxey

Helmut,

Last night I tried Dim oTmp as Template

Set oTem = DateSequencer.Dot

and got a compile error.

The method Jonathan has posted works fine.

Thanks.
 
G

Greg Maxey

Helmut,

I spoke too soon. I thought the method that Jonathan provided "would" work
before actually trying. Please elaborate if you will on how to set oTmp to
the template fullname.

Here is the whole snipet:
i = 0
For Each oTmp In Templates
i = i + 1
If oTmp.Name = "DateSequencer.dot" Then
Exit For
End If
Next

'Create the AutoText Entry
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldDocVariable And Mid(oFld.Code.Text, 15, 9) = "Quote
Var" Then
oFld.Select
Templates(i).AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range
Selection.Collapse Direction:=wdCollapseEnd
If Templates(i).Saved = False Then Templates(i).Save
Exit For
End If
Next
 
G

Greg Maxey

Jonathan,

I tried this and got a compile error on the
..AutoTextEntries

Method or data member not found.

Am I doing something wrong? Here is the whole snipet of code (with your
line stetted out for now):

i = 0
For Each oTmp In Templates
i = i + 1
If oTmp.Name = "DateSequencer.dot" Then
Exit For
End If
Next

'Create the AutoText Entry
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldDocVariable And Mid(oFld.Code.Text, 15, 9) = "Quote
Var" Then
oFld.Select
'ThisDocument.AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range
Templates(i).AutoTextEntries.Add Name:="seqDate",
Range:=Selection.Range
Selection.Collapse Direction:=wdCollapseEnd
If Templates(i).Saved = False Then Templates(i).Save
Exit For
End If
Next
 
H

Helmut Weber

Hi Greg,
If oTmp.Name = "DateSequencer.dot" Then s = oTmp.fullname
Exit For
End If
Next
Templates(s).AutoTextEntries.Add Name:="seqDate", _
Range:=Selection.Range

Seems, I must be missing something.
Just set, if the template's name = "DateSequencer.dot",
the variable s, which is the fullname, to oTmp.fullname.

You have been in Moscow? Wow!

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jonathan West

Hi Greg

Greg Maxey said:
Jonathan,

I tried this and got a compile error on the
.AutoTextEntries

Method or data member not found.

Am I doing something wrong?

No, I forgot to include a vital element. It should be this

ThisDocument.AttachedTemplate.AutoTextEntries.Add _
Name:="seqDate", Range:=Selection.Range

Since ThisDocument is already a template, it is attached to itself. But
since AttachedTemplate is a Template object while ThisDocument is a Document
object, it does have an AutoTextEntries collection.

Sometimes it's necessary to be convoluted...
 
H

Helmut Weber

Hi Greg,
sorry,
there was a question from a Greg from Moscow,
"moving caret to cursor".
Was a good question, could have been from You.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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