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