Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Different Headers in different pages using macro in word

Status
Not open for further replies.

VisualBasicHelp

Programmer
Sep 7, 2005
74
GB
I am trying to create a macro which gives me different headers on different pages. But when I am trying to add a new header in the 3rd page , that is, if the first 2 pages are section 1 and 3rd page is section 2 . It is making the changes in section 1 and not in section 2. Can anybody please help me out. I am just messed up with that.
If I do it directly without macro, I am able to do it. For reference I am pasting my code. Please refer to it.

'Creating a section in the next page
Selection.InsertBreak Type:=wdSectionBreakNextPage

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

'Making sure the page layout is printview
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If

'Activating the header
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

'Deleting everything
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, count:=1

'Formatting the text
Selection.Font.Size = 14
Selection.Font.Name = "Arial"
Selection.Font.Bold = True

'Typing in the header
Selection.TypeText Text:="TESTCASE : "
Selection.TypeParagraph
Selection.TypeText Text:="TEST STEP : " & teststep




ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Thanks in advance
 



Hi,

I'd advise to get in the habit of NOT using the Selection Method alot.

Check out Sections Collection Object. Section is an object whose Parent is a Document Object. More explicitly define the area you are working on.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
First of all, Skip is correct. Using the Selection object does have its uses, but often it is not the best way to do things.

Word and headers/footers is not really that hard, but it CAN be weird to think about.

It is much much better to use objects when possible. It is also possible to access the headers and footers directly, that is without using View.

Before I continue, I also want to point out that you do not need to use multiple instruction to text.
Code:
Selection.TypeText Text:="TESTCASE : "             
Selection.TypeParagraph
Selection.TypeText Text:="TEST STEP : " & teststep
can be written as:
Code:
Selection.TypeText Text:="TESTCASE : " & vbcrlf & "TEST STEP : " & teststep
....whatever the variable teststep is....

OK, in any case, I hope you do not feel I am trying to trash your code here. I am actually trying to help.
'Creating a section in the next page
Selection.InsertBreak Type:=wdSectionBreakNextPage
This is not correct. You are not creating a Section in the next page. You are creating a new Section. The next page comes FROM the action of creating the Section. This may seem picky, but it is crucial to understanding the object model, and THAT is crucial to understanding what the bleep is going on.


REALLY understanding headers and footers (by code) would mean going into understanding how Word thinks of Sections. I will make it brief (in my normal Gerry way....).

1. ALL Sections have three headers (and three footers). Whether you put text in them or not, there are ALWAYS three headers in every Section. You can put text in one of them, delete the text...it does not matter - the header will still exist. More weirdness - say you check DifferentFirstPage (in Page Setup) so you have a different first page header, then put text into it. If you test for its existence (using the Exists method) it will return TRUE. Now go and UNCHECK DifferentFirstPage. Test for its existence, and the Exist method will return FALSE - it does not exist. Yet, if you also ask for the text of the range of that ("non-existing") header...it will in fact give you the text in that header. AND, if you re-check that header, any text you left there will still be there.

2. Word defaults to Same As Previous. This means whenever you make a new Section, Word assumes that you want the same header as the previous Section. This must always be considered when creating headers by code.

3. Word deletes header information in reverse order. This is absolutely imperative to understand if you do any deleting of headers. I know I am giving WAAAAAY too much information here, but this one is important.

Say you have a document (simple headers - no DifferentFirstPage etc). Three pages, three sections (one per page), three headers, and the text of the headers is like:

This is Header 1 in Section 1 - Section 1 header text
This is Header 2 in Section 2 - Section 2 header text
This is Header 3 in Section 3 - Section 3 header text

Delete the section break between Section 2 and Section 3. The headers will now look like:

This is Header 1 in Section 1 - Section 1 header
This is Header 3 in Section 3 - Section 2 header text

Now delete the Section break between Section 1 and Section 2 - thus making the document only ONE Section. The header will look like:

This is Header 3 in Section 3 - Section 1 header text

Word removes headers recursively! And there is nothing you can do about it. The only way to retain the original (previous) Header content - that is if you are deleting Section breaks - is to put that content into a variable (or something) so you can put it back.

Finally....
Code:
Sub NewSectionNewHeader()
[COLOR=red]' just to use your teststep variable[/color red]
Dim teststep As String
teststep = "Hello World"
With Selection
[COLOR=red]' assuming you ARE doing this at the end of the document[/color red]
   .EndKey Unit:=wdStory
   .InsertBreak Type:=wdSectionBreakNextPage
End With
With Selection.Sections(1).Headers(wdHeaderFooterPrimary)
[COLOR=red]' NOTE! the following will actually delete
' any existing text carried over from the
' previous header!
' It also assumes you do NOT have any different header
' structure - DifferentFirstPage, DifferentOddEven[/color red]
      .LinkToPrevious = False
      .Range.Text = "TESTCASE : " & vbCrLf & _
            "TEST STEP : " & teststep
End With
End Sub
This should do what it is I think you are trying to do.

Note that this DOES use the Selection object. For a simple piece of code like this, it is not so bad to use. However, if you have multiple actions - say if you are trying to run through headers and footers doing stuff, or looking for text - then it would be better to make a HeaderFooter object and use that.

One of the important things here, is that you are not using View, so there are no changes to what the user sees.

Regarding Selection....in the case of the immediate situation - you have JUST made a new Section (and it is Section 2)...
Code:
With Selection.Sections(1).Headers(wdHeaderFooterPrimary)
      .LinkToPrevious = False
      .Range.Text = "TESTCASE : " & vbCrLf & _
            "TEST STEP : " & teststep
End With
has the exact same effect as:
Code:
With ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary)
   .LinkToPrevious = False
   .Range.Text = "TESTCASE : " & vbCrLf & _
            "TEST STEP : " & teststep
End With

In the first, you are actioning the first Section (it does not matter what DOCUMENT Section number it may be) of the Selection.

In the second, you are actioning explicitly Section 2 of the document.

In other words...
Code:
Sub NewSectionNewHeader()
Dim teststep As String
teststep = "Hello World"
With Selection
   .EndKey Unit:=wdStory
   .InsertBreak Type:=wdSectionBreakNextPage
End With
With ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary)
   .LinkToPrevious = False
   .Range.Text = "TESTCASE : " & vbCrLf & _
            "TEST STEP : " & teststep
End With
End Sub
will do the same as the other code. As long as it is in fact Section 2.

Gerry
My paintings and sculpture
 
Thanks a lot. I have lots of question regarding this Gerry. But since I have to finish up this task as early as possible, I am just asking you the required. Can u please tell me how can I get "Page x of y" in the footer.
 
let me make it more clear. I mean for each sections. That is if in section 1, I have 3 pages, I need to get "page 1 of 3","page 2 of 3" and "page 3 of 3". The same in section 2 also, like "page 1 of 2", "page 2 of 2". if it contains 2 pages
 


In your header/footer:

Insert/Field - Category Numbering -- SECTION & SECTION PAGES

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
I am getting the footer as per your suggestion, but, how can I use code for it in a macro using Gerry's code.
 


Have you turned on your macro recorder?

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top