Play AutoText in Header

S

Sammy

Hello,

I'm using Word 03. I know this should be easy but I'm lost. From searching
the newsgroup I found this code to add text to a header:

ActiveDocument.Bookmarks("myHeaderBookmark").Range.Text = "my text"

But I have the text/graphic in an autotext entry. How do I play an autotext
entry in a header? The Range object here doesn't seem to have any links to
autotext.

Any help is appreciated!
 
S

Sammy

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
page header. Thanks
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
S

Sammy

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!
 
S

Sammy

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
 
S

Sammy

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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
S

Sammy

THANK YOU FOR YOUR HELP!

Jay Freedman said:
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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

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