PDA

View Full Version : Solved: Replace Header Contents Starting with Section 2



clhare
12-01-2010, 03:08 PM
Can someone tell me what I am doing wrong in the following code? I get a message that says invalid use of property when I try to assign a value to aTemplate, but I don't know why or what it should be instead. I am trying to replace everything in the headers (starting with section 2) with my AutoText entry.

Headers and Footers make me crazy!Sub Test()
Dim aTemplate As Template
Dim strAutoText As String
Dim intSection As Integer
Dim intHFType As Integer
Dim intSecCount As Integer
Dim rngPane As Range

strAutoText = "MyAutoText"
' Update Headers starting with section 2
intSecCount = ActiveDocument.Sections.Count
For intSection = 2 To intSecCount
With ActiveDocument.Sections(intSection)
For intHFType = 1 To 3
Set rngPane = .Headers(intHFType).Range
' Insert AutoText
aTemplate = "C:\Program Files\Microsoft Office XP\Office10\Startup\My Template.dot"
aTemplate.AutoTextEntries(strAutoText). _
Insert Where:=rngPane, RichText:=True
Next intHFType
End With
Next intSection
End SubAny help is greatly appreciated!

Dave
12-01-2010, 10:25 PM
aTemplate = "C:\Program Files\Microsoft Office XP\Office10\Startup\My Template.dot"
I believe aTemplate is a string when you do this. Trial Dimming as a string, or removing the quotes, or Dimming as a Variant, or wait till Gerry can help :) Dave

Tinbendr
12-02-2010, 02:04 AM
Set aTemplate = Documents("C:\Program Files\Microsoft Office XP\" & _
"Office10\Startup\My Template.dot").AttachedTemplate David

clhare
12-02-2010, 04:47 AM
Awesome! It works perfectly now!

Thanks!

clhare
12-02-2010, 07:10 AM
Actually, I spoke too soon. Now I'm getting a "Bad file name" error when I use this code. Any idea why that would cause an error? If the template is also open at the time I run the macro on a document, the macro works fine. But if the template is closed, the macro doesn't work. I doublechecked and the path and filename I am using for the template is correct.

fumei
12-02-2010, 11:01 AM
"If the template is also open at the time I run the macro on a document, the macro works fine. But if the template is closed, the macro doesn't work. "

What do you mean by "open"?

If a .DOT file is not:

1. actually open (i.e. the file itself, and thus in the Documents collection);

OR

2. accessible because it is an attached template to a document that IS open;

OR

3. in an installed and loaded global template

then you can not access its AutoText.

fumei
12-02-2010, 11:05 AM
Put another way, you can NOT set a template object to a filename.

You set a template object (aTemplate) to a member of the Templates collection.

See Help on Template Object.

fumei
12-02-2010, 11:27 AM
Option Explicit

Sub OnlyPastSection2()

Dim oHF As HeaderFooter
Dim strAutoText As String
Dim TemplateName As String
Dim j As Long

strAutoText = "MyAutoText"
TemplateName = "C:\Program Files\Microsoft Office " & _
"XP\Office10\Startup\My Template.dot"

' add the template as a global
AddIns.Add TemplateName, Install:=True

' action all Sections except Section 1
For j = 2 To ActiveDocument.Sections.Count
' all for headers in section
For Each oHF In ActiveDocument.Sections(j).Headers
With oHF.Range
' delete everything, enter text
' and activate AutoText
.Delete
.Text = strAutoText
.InsertAutoText
End With
Next
Next
' remove template from globals
AddIns(TemplateName).Installed = False
AddIns(TemplateName).Delete
End Sub
The above loads your .DOT file as a global, uses an AutoText in it to replace headers past Section 2, and then removes its as a global.

fumei
12-02-2010, 01:06 PM
Although I just noticed. Your .DOT is in Startup. Therefore it is loaded. Therefore you should be able to use its AutoText.
Sub OnlyPastSection2()
Dim oHF As HeaderFooter
Dim strAutoText As String
Dim j As Long

strAutoText = "MyAutoText"
For j = 2 To ActiveDocument.Sections.Count
For Each oHF In ActiveDocument.Sections(j).Headers
With oHF.Range
.Delete
.Text = strAutoText
.InsertAutoText
End With
Next
Next
End Sub

fumei
12-02-2010, 03:06 PM
"Headers and Footers make me crazy! "

Cheryl, did you read my posts on headers and footers in your other threads?

I agree they seem to be hair-pulling nuts, but once you get your mind around the actual model Microsoft employs, it gets much easier.

What is making you crazy? I would like to help. In the case of this thread, the issue/problem had nothing to do with headers/footers. It was the template object.

clhare
12-06-2010, 06:51 AM
I think the selection and range objects are my biggest area of confusion. Headers and footers are a very close second, and when I have to work with both of them together.... I'm in deep water for sure!

fumei
12-06-2010, 09:49 AM
Ah. "Selection and range". Yes, well. That is a big issue, and extremely critical to using VBA and Word. If you post some direct questions about what is hard for you, perhaps we can help. Getting those two comfortable in your mind will make things MUCH easier.

Selection: what is selected (or highlighted) on screen. This is critical. selection is what is on-screen. It is what you can see. It is the focus of where the cursor is.

Range: a purely mathematical identification (i.e. a Start and and End) of an area in the document. Everything in a document has a range.