PDA

View Full Version : Solved: Insert AutoText Entry in Section 1 Headers Only



clhare
01-08-2007, 07:27 AM
Hello...

I need to insert an AutoText entry into just the headers for Section 1. The document has "different first page" selected in the setup, so there's going to be two headers I have to add the AutoText entry to. Can someone tell me how to do that? The following is as close as I can get, but it's not right and I don't know how to fix it.


Dim intHFType As Integer
Dim rngSelection As Range
Dim rngPane As Range

With ActiveDocument.Sections(1)
For intHFType = 1 To 3 ' Starting with the first header
Set rngPane = .Headers(intHFType).Range
With rngPane
ActiveDocument.AttachedTemplate.AutoTextEntries("MyAutoText").Insert Where:= _
Selection.Range, RichText:=True
ActiveWindow.ActivePane.View.NextHeaderFooter
Application.DisplayAutoCompleteTips = True
ActiveDocument.AttachedTemplate.AutoTextEntries("MyAutoText").Insert Where:= _
Selection.Range, RichText:=True
End With
Next intHFType
End With


Any help would be greatly appreciated!

fumei
01-08-2007, 04:37 PM
As you mention "just" Section 2, I take it there are other Sections.

Are those other Section(s) already set as not Same as previous?

clhare
01-08-2007, 05:37 PM
Yes they already unlinked from each other. So I just need to add the AutoText entry to any headers for section 1.

fumei
01-08-2007, 10:56 PM
Your code runs 1 to 3, to include all possible headers. Apparently though you are only using 2. So actually you have two choices. You can insert the autotext explicitly to the two being used (assuming the entry is in Normal.dot):With NormalTemplate.AutoTextEntries("MyAutoText")
' 2 = wdHeaderFooterFirstPage
.Insert Where:=ActiveDocument.Sections(1).Headers(2).Range
' 1 = wdHeaderFooterPrimary
.Insert Where:=ActiveDocument.Sections(1).Headers(1).Range
End With

OR, you can do with the 1 to 3 and put it in all of them:Dim var
For var = 1 To 3
NormalTemplate.AutoTextEntries("MyAutoText")
.Insert Where:=ActiveDocument.Sections(1).Headers(var).Range
NextThis will put the entry MyAutoText in all three headers of Section 1.

There is no need to use Selection, or View NextHeaderFooter. You can simply DO it.

NOTE! This will replace the header range. If this is not what you want, then say so. For example, if you actually want to put the autotext entry after some existing text, then the code has to be different.

fumei
01-08-2007, 11:08 PM
Darn....too quick with the click.

For example, to insert the AutoText entry at the end of any existing header text:Dim var
Dim r As Word.Range
For var = 1 To 3
Set r = ActiveDocument.Sections(1).Headers(var).Range
r.Collapse Direction:=wdCollapseEnd
NormalTemplate.AutoTextEntries("MyAutoText") _
.Insert Where:=r
Next
Set r = Nothing

clhare
01-11-2007, 07:59 AM
Thank you so much!!!

fumei
01-12-2007, 09:07 AM
You are welcome so much.