PDA

View Full Version : Update Code to work in All Headers



clhare
11-10-2010, 10:16 AM
The following code will replace the existing header in all header types of section 2 with AutoText. I need help modifying this code in 2 ways:

1) Need one version that will make the update to all header types in all sections of the document
2) Need another version that will make the update to the first page header in section 1 only

Sub UpdateHeader()

Dim aTemplate As Template
Dim myTemplate As Template
Dim strAutoText As String
Dim r As Word.Range
Dim intHFType As Integer
strAutoText = "MyHeaderText"

' Go to top of document
Selection.HomeKey unit:=wdStory
For Each aTemplate In Templates
' Insert AutoText
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
For intHFType = 1 To 3
Set r = ActiveDocument.Sections(2).Headers(intHFType).Range
myTemplate.AutoTextEntries(strAutoText).Insert where:=r
Next
End If
Next
End Sub

Any assistance is greatly appreciated!

fumei
11-10-2010, 10:42 AM
If you are dealing with documents, why are you using Templates?

If the issue is what template is being used by any given document, why do you loop through the Templates collection, rather than the Documents collection?

Why are you using Selection.HomeKey? There is no need to "go to top of document".

Sub UpdateHeader()
Dim aDoc As Document
Dim strAutoText As String
Dom oSection As Section
Dim oHF As HeaderFooter

strAutoText = "MyHeaderText"
For Each aDoc In Documents
If aDoc.AttachedTemplate = "My Template.dot" Then
MyTemplate = aDoc.AttachedTemplate
For Each oSection In aDoc.Sections
For Each oHF In oSection.Headers
myTemplate.AutoTextEntries(strAutoText).Insert _
Where:= oHF.Range
Next
Next
End If
Next
End Sub
To use for a specific Section header (DifferentFirstPage), use THAT object.
For Each oSection In aDoc.Sections
myTemplate.AutoTextEntries(strAutoText).Insert _
Where:= oSection.Headers(DifferentFirstPage).Range

clhare
11-10-2010, 12:18 PM
I thought I replied, but it's not here... Anyway, I am using a template to store specific styles and AutoText that I will keep in my Startup folder (and give to a few other people to use as well). So, I want the macro to find the AutoText in my template and insert it into the footers. I had gotten some assistance in this method previously for the styles, but I can't figure out how to get the code to work for inserting AutoText in the headers.

fumei
11-10-2010, 12:20 PM
And do you have it working now?

clhare
11-10-2010, 12:24 PM
No. I'm still trying to figure out how to update my code. So far, nothing changes in the headers when I run it.

fumei
11-10-2010, 12:35 PM
OK, start simple.

Try inserting an Autotext into any header.

Can you make that happen?

clhare
11-10-2010, 12:52 PM
I do have this code, which will update the headers in the section the cursor is in. This works, but again, I can't figure out how to make it work throughout the document.

strAutoText = "MyAutoText"
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
' Select entire header and delete anything found
Selection.WholeStory
Selection.Delete unit:=wdCharacter, Count:=1
' Find my template among the loaded templates
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=Selection.Range, RichText:=True
Exit For
End If
Next
'ActiveDocument.AttachedTemplate.AutoTextEntries(strAutoText). _
Insert Where:=Selection.Range, RichText:=True
' Return to the main document
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

clhare
11-10-2010, 02:41 PM
Ok, I got the following code to work. This code updates all the header types in all sections:
Sub UpdateAllHeaders()
Dim r As Range
Dim s As Section
Dim aTemplate As Template
Dim myTemplate As Template
strAutoText = "MyAutoText"
For Each s In ActiveDocument.Sections
Set r = s.Headers(wdHeaderFooterPrimary).Range
With r
For Each aTemplate In Templates
If aTemplate = "MyTemplate.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=r, RichText:=True
Exit For
End If
Next
End With
Next s
End Sub
Why do I have to use "wdHeaderFooterPrimary" in order for it to work? I tried using "wdHeaderFooterFirstPage" and then nothing updated.

I'm still stumped though on how to update just the first page header in section 1.

These headers and footers make me CRAZY!!!

fumei
11-10-2010, 03:19 PM
"Ok, I got the following code to work. This code updates all the header types in all sections:"

No, it does not.


For Each s In ActiveDocument.Sections
Set r = s.Headers(wdHeaderFooterPrimary).Range
With r
For Each aTemplate In Templates
If aTemplate = "MyTemplate.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=r, RichText:=True
Exit For
End If
Next
End With
does NOT change all the headers. It only changes Primary.

You need to understand headers better.

ALL Sections have six header/footers. All of them. Six. They are:

Section(x).Headers(HeaderFooterPrimary)
Section(x).Headers(HeaderFooterFirstPage)
Section(x).Headers(HeaderFooterEvenPages)
Section(x).Footers(HeaderFooterPrimary)
Section(x).Footers(HeaderFooterFirstPage)
Section(x).Footers(HeaderFooterEvenPages)

IF you do not have Different first page checked, or Different odd
and even checked (in Page Setup), then:
Headers(HeaderFooterPrimary) is used for ALL headers.
Footers(HeaderFooterPrimary) is used for ALL footers.

If you checked just Different first page, then:
Primary is used for all header/footer except for the first page.

If you checked Different first page AND Different odd and even, then:
HeaderFooterPrimary is used for ODD pages
HeaderFooterFirstPage is used for first page
HeaderFooterEvenPages is used for even pages

If you want to change all headers and footers in all Sections, use the object code I have posted.
For Each oSection In aDoc.Sections
For Each oHF In oSection.Headers


To repeat, in your code:
Set r = s.Headers(wdHeaderFooterPrimary).Range
This will set (and action) the range for Primary. If that is all that is being used, the changes will be seen in all visible headers - but not the footers.

If you have different first page, the change will NOT be applied to it.

"I tried using "wdHeaderFooterFirstPage" and then nothing updated."

Then you are only using Primary.

You did not answer my questions regarding Templates.

clhare
11-10-2010, 03:36 PM
We have a Startup template that has many different macros that we can use on any given document. I have been asked to add this macro to that template.

I'm still doing something wrong. I'm either not replacing the right code with your code or replacing too much re replacing all headers in all sections.

Here's what I have now... and I know it's wrong...
Sub UpdateAllHeaders()

Dim r As Range
Dim s As Section
Dim aDoc As Document
Dim oSection As Section
Dim oHF As HeaderFooter
strAutoText = "MyAutoText"
For Each oSection In aDoc.Sections
For Each oHF In oSection.Headers
With oHF
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=r, RichText:=True
Exit For
End If
Next
End With
Next oHF
Next oSection
End Sub

clhare
11-10-2010, 04:53 PM
Ok.... I got the macro to work and it now updates all the headers in the document, even if Different Odd/Even or Different First Page is selected.

Here's what I have for this part now. Does it look right?
Sub UpdateAllHeaders()
Dim oSection As Section
Dim oHF As HeaderFooter
strAutoText = "MyAutoText"
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
With oHF.Range
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=oHF.Range, RichText:=True
Exit For
End If
Next
End With
Next oHF
Next oSection
End Sub

clhare
11-10-2010, 05:35 PM
Ok, me again....

Here's what I have for both the macros. Can you tell me if they are correct? The first should insert the AutoText in all headers throughout the document. The second should insert the AutoText in only the first header in the first section of the document. I've tried to account for the different Page Setup selections for the second one and it seems to be working no matter what I selected in Setup.
Sub UpdateAllHeaders()
Dim oSection As Section
Dim oHF As HeaderFooter
Dim aTemplate As Template
Dim myTemplate As Template
strAutoText = "MyAutoText_All"
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
With oHF.Range
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=oHF.Range, RichText:=True
Exit For
End If
Next
End With
Next oHF
Next oSection
End Sub

Sub UpdateFirstPageHeader()
Dim oSection As Section
Dim oHF As HeaderFooter
Dim aTemplate As Template
Dim myTemplate As Template
strAutoText = "MyAutoText"
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
Set myTemplate = aTemplate
With ActiveDocument.Sections(1)
If .PageSetup.DifferentFirstPageHeaderFooter = True Then
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range, RichText:=True
ElseIf .PageSetup.DifferentFirstPageHeaderFooter = False Then
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(1).Range, RichText:=True
End If
End With
End If
Next
End Sub

I really, really hope this is right!

clhare
11-11-2010, 09:47 AM
Last night this code was finally working (I tested it several times). Now it's not working at all. What am I doing wrong????

clhare
11-11-2010, 10:35 AM
I think I'm losing my mind. I had renamed the template itself differently from what I put in the code. Once I fixed that, it worked fine again.

:whyme:

fumei
11-12-2010, 10:02 AM
1. What is with the templates? I have asked, and you do not answer.

For Each aTemplate In Templates
WHY are you doing this?


2. Please tell me what it is you are wanting to do? Do the documents have Different first page, or not? It really does not matter actually, but it may help to know.

3. Do you actually understand what:
If .PageSetup.DifferentFirstPageHeaderFooter = True Then
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range, RichText:=True
ElseIf .PageSetup.DifferentFirstPageHeaderFooter = False Then
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(1).Range, RichText:=True
End If
is actually doing? What it is doing is:

IF Different first page is checked in Page Setup, insert the AutoText into HeaderFooterFirstPage

Else

insert AutoText in HeaderFooterPrimary

You do not grasp what IF statements do.

ElseIf .PageSetup.DifferentFirstPageHeaderFooter = False
is completely unnecessary. You do not need it whatsoever.


If .PageSetup.DifferentFirstPageHeaderFooter = True Then
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1) _
.Headers(wdHeaderFooterFirstPage).Range, RichText:=True
Else
myTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(1).Range, RichText:=True
End If

clhare
11-12-2010, 01:54 PM
Ok... the "for each aTemplate in Templates..." came from code given to me in another post where I was having trouble accessing AutoText that was saved in a Startup template. Since I'm doing the same thing here (just trying to get the AutoText in a header), I used that same code in my attempts.

As for the documents that I want to insert AutoText in headers for, some have a different first page, some do not. I tried using just the one piece of code in each type of document and found that not all the documents updated. So I tried adding the second piece and they appear to update regardless of what is selected in the Page Setup.

Also, I see what you mean about the "IF" statement and I've corrected my macro. I have a tendency to overthink things.

I really, really appreciate all your help with this. I know I couldn't have figured it out without you!

clhare
11-12-2010, 02:04 PM
Ok, as if this one hasn't gone on long enough....

How would I tweak this macro to start the update in Section 2 and leave Section 1 as is? If I can figure that out, I won't need to save Section 1's header as AutoText. Currently, the macro will replace all headers, but for my documents, the first page is a title page and the header will not need to change. So, I either have to recreate the header in Section 1 or somehow get this macro to leave it alone.

fumei
11-15-2010, 10:58 AM
1. As I have stated, all Sections have a DifferentFirstPage header - regardless of whether it is being used, or not. To check if it IS being used, check the value of the Exist property.
' NOTE wdheaderFooterFirstPage = 2
If ActiveDocument.Sections(1).Headers(2).Exists = True Then
' do your stuff
IF Different first page is CHECKED then the .Exists property = True

Therefore you can test if DifferentFirstPage is being used, or not. The above tests to see if Section Different firstpage is checked, or not.

2. The reason I keep asking about the templates, is that:
For Each aTemplate In Templates
If aTemplate = "My Template.dot" Then
does a test of all the active templates to see if one of them is My Template.dot. It does NOT check if the current document (ActiveDocument) is using My Templates.dot. It just test to see if My Templates is part of the current templates. In other words, if the active document is using "Yadda.dot" - NOT My Template.dot - it would still be actioned. Surely this is not correct. If it is, why bother doing the test? You are actioning the current active document anyway!.

So...

Sub UpdateFirstPageHeaderIF_There()
Dim aTemplate As Template
Set aTemplate = path_to_template & "My Template.dot"
If ActiveDocument.Sections(1).Headers(2).Exists = True Then
aTemplate.AutoTextEntries(strAutoText). _
Insert Where:=ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
End If
End Sub
This test to see if Section(1) is using Different first page (NOT that it has one...it does!).

If it is using Different first page, it sets the template object to My Templates directly, and inserts the AutoText.

clhare
11-19-2010, 05:16 AM
Ok, I see where you are going! I did not realize that "Different First Page" was there whether it was used or not. And I can see that it makes more sense to just set the aTemplate to "My Template.dot" than to see if it is "there".

I will make these updates to my code.

Thank you so much for your help!! I always learn something along the way.:biggrin:

Cheryl