It's an interesting observation on "object-oriented" programming: sometimes you
want an interaction between two objects, and it isn't obvious which object
should be the actor -- the owner of the method being called -- and which should
be acted upon.
In this case, the actor is the AutoText entry, which has an Insert method. The
range into which that entry is inserted will be a parameter given to the method.
For example, to put the "Page X of Y" AutoText in the first page header, you
would write this:
NormalTemplate.AutoTextEntries("Page X of Y").Insert _
Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
If you don't want to replace the current contents of the header, but instead put
the AutoText after the existing text, you could do this:
Dim oRg As Range
Set oRg = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
oRg.Collapse wdCollapseEnd
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=oRg
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
>One more thing, I would prefer not to use a bookmark which seems like an
>extra step. I just want to insert an autotext entry directly into the first
[quoted text clipped - 12 lines]
>>
>> Any help is appreciated!
Sammy - 12 Mar 2008 17:11 GMT
Hi Jay,
Thank you for explaining this. Now other things make sense also.
One more question. If the template the autotext entry lives in is a global
template named Mystuff, would I just replace the Normaltemplate with
mystufftemplate? Thanks again for your help!
> It's an interesting observation on "object-oriented" programming: sometimes you
> want an interaction between two objects, and it isn't obvious which object
[quoted text clipped - 40 lines]
> >>
> >> Any help is appreciated!
Sammy - 12 Mar 2008 18:29 GMT
Actually, the macro is now hanging on the autotext entry statement. They are
stored in the MyStuff global template and not in Normal. I've declared
"mystufftemplate" as a template and I've replaced NormalTemplate with
MyStuffTemplate in the code but it still won't work. What am I missing?
thanks
> Hi Jay,
>
[quoted text clipped - 48 lines]
> > >>
> > >> Any help is appreciated!
Sammy - 12 Mar 2008 18:57 GMT
I'm really trying to answer my own question. I tried the following:
Dim originalattachment As String
' Get the currently attached template and store it in a variable.
originalattachment = Templates(1).Path & Application.PathSeparator _
& ActiveDocument.AttachedTemplate
' Set the attached template to your custom template.
ActiveDocument.AttachedTemplate = "C:\Startup\Firm.dot"
' Insert the AutoText at the insertion point.
ActiveDocument.AttachedTemplate.AutoTextEntries("MyEntry").Insert _
Where:=Selection.Range, RichText:=True
' Re-Attach the original template.
ActiveDocument.AttachedTemplate = originalattachment
But it errors on "ActiveDocument.attachedTemplate = "C:\Startup\MyStuff"
The template is in the startup folder and everything is spelled correctly.
Not sure what's wrong....thanks
> Actually, the macro is now hanging on the autotext entry statement. They are
> stored in the MyStuff global template and not in Normal. I've declared
[quoted text clipped - 54 lines]
> > > >>
> > > >> Any help is appreciated!
Jay Freedman - 13 Mar 2008 02:32 GMT
Hi Sammy,
If the template is in Word's Startup folder (which probably isn't C:\Startup),
then it is by definition a member of the Templates collection. You can get a
valid pointer to it without making it the AttachedTemplate.
There is a little twist, because the Templates collection doesn't support using
the template's name in place of the numeric index to address it directly. Since
you don't necessarily know the index number of the member of the collection you
want, you have to loop through the collection until you find the one whose name
matches. Fortunately the Templates collection rarely has more than a handful of
members, so this executes quickly enough.
This bit of code should get you where you want to go:
Dim oTmpl As Template
For Each oTmpl In Templates
If LCase(oTmpl.Name) = "firm.dot" Then
Exit For
End If
Next oTmpl
If Not oTmpl Is Nothing Then
oTmpl.AutoTextEntries("MyEntry").Insert _
Where:=Selection.Range, RichText:=True
End If
Note that comparing the LCase of the name to the desired name all in lower case
avoids problems if the capitalization of the name is different than expected.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
>I'm really trying to answer my own question. I tried the following:
>
[quoted text clipped - 71 lines]
>> > > >>
>> > > >> Any help is appreciated!
Sammy - 13 Mar 2008 22:05 GMT
THANK YOU FOR YOUR HELP!
> Hi Sammy,
>
[quoted text clipped - 109 lines]
> >> > > >>
> >> > > >> Any help is appreciated!