Headers should not appear in the first section.

R

rVo

I'm trying to create a macro that sets the headers for a document in the
following way:
- Odd & even pages have different headers (no problem here);
- The first 2 pages of the document should not contain any headers (these
pages make up section 1 of the document) (problem here);
- Page numbering should start from 1 on the third page (which is the first
page of section 2) (no problem here);
- The remaining pages of the document are in various sections.

The problem so far is that I can't get this to work in any way (not even
manually). I always end up with a header on page 2 (but not on page 1,
although I mentioned that the first page should not be threated special.)

I'd be very happy with code advice or anything that enables me to create a
correct situation manually first.

Kind regards,

rVo
 
D

Doug Robbins - Word MVP

I wouldn't be trying to do this with a macro, but show us what you are
trying to use and we may be able to spot the problem.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

rVo

Ok, what would you need then? A sample document?
I can't get it posted to the newsgroup. What do I do now?

Kind regards,

rVo
 
D

Doug Robbins - Word MVP

IF you are using a macro, copy and paste the code of the macro into a
message that you post back here.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

rVo

Dear Doug,

upto now I haven't been using a macro, I am trying to create a "proof of
concept" without succes.
Basically the problem is this:
I get the same header/footer throughout my document, although I want it to
be different for the first 2 pages in the document.
I tried to make a rough version in VBA, but that's a quick & dirty version.
I want to do the right way.

Kind regards,

rVo

I've been checking on www.mvps.org/word hoping to finde some advice on
approaching headers/footers from VBA, but I did not find any documentation
on that subject there.
 
R

rVo

Ok, below is some code that is supposed to put a header on page 1 of the
document, leave the header of the second page blank
Page 3&4 (and all following) should get mirrored headers.

I have no control over headers which are already in the document; for
testing purposes you need atleast 4 pages in a document.

The result of the code below is never the same which made me guess that Word
is encountering one of it's infamous timing-problems.
The document on which I've tested this code is 400+ pages. Using Word2K3 on
Win2K on a very fast machine with 1GB of memory.

I had to put a # on each line because I'm behind a very limitting firewall
which won't let me send out code snippets otherwise.
(the # can simply be removed with a text editor's find/replace option).

I really hope it's not too shocking what I did here.

Kind regards,

rVo


#Sub SetHeaders()
# Selection.GoTo what:=wdGoToPage, which:=wdGoToFirst
#
# 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
#
# 'Make sure that the first header is empty
# '---------------------------------------------------------------
# ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
# Selection.WholeStory
# Selection.Delete
#
# 'Make header of first page
# '---------------------------------------------------------------
# With Selection.PageSetup
# .OddAndEvenPagesHeaderFooter = False
# .DifferentFirstPageHeaderFooter = False
# End With
# Selection.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1,
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
# With Selection.Tables(1)
# If .Style <> "Tabelraster" Then
# .Style = "Tabelraster"
# End If
# .ApplyStyleHeadingRows = True
# .ApplyStyleLastRow = True
# .ApplyStyleFirstColumn = True
# .ApplyStyleLastColumn = True
# End With
#
# With Selection.Range.Cells(1)
# .PreferredWidthType = wdPreferredWidthPoints
# .PreferredWidth = 100
# End With
# Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
# Selection.Font.Bold = True
# Selection.TypeText "Some text"

# 'Clear header for second page
# '---------------------------------------------------------------
# ActiveWindow.ActivePane.View.NextHeaderFooter
# Selection.WholeStory
# Selection.Delete
#
# 'Clear header for third page
# '---------------------------------------------------------------
# ActiveWindow.ActivePane.View.NextHeaderFooter
# Selection.HeaderFooter.LinkToPrevious = False
# Selection.WholeStory
# Selection.Delete
# Selection.HeaderFooter.LinkToPrevious = False
#
# 'fill header for third page
# '---------------------------------------------------------------
# Selection.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=3,
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
#
# Selection.MoveRight unit:=wdCell, Count:=1
# Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPercent
# Selection.Range.Cells(1).PreferredWidth = 10
#
# Selection.MoveRight unit:=wdCell, Count:=1
# Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPercent
# Selection.Range.Cells(1).PreferredWidth = 8
#
# Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
# Selection.TypeText "-"
# Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
# Selection.TypeText "-"
#
# Selection.MoveLeft unit:=wdCell, Count:=1
# With Selection.Cells
# With .Borders(wdBorderLeft)
# .LineStyle = wdLineStyleSingle
# .LineWidth = wdLineWidth050pt
# .Color = wdColorAutomatic
# End With
# With .Borders(wdBorderRight)
# .LineStyle = wdLineStyleSingle
# .LineWidth = wdLineWidth050pt
# .Color = wdColorAutomatic
# End With
# .Borders(wdBorderTop).LineStyle = wdLineStyleNone
# .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
# .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
# .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
# .Borders.Shadow = False
# End With
#
# With Options
# .DefaultBorderLineStyle = wdLineStyleSingle
# .DefaultBorderLineWidth = wdLineWidth050pt
# .DefaultBorderColor = wdColorAutomatic
# End With
#
# Selection.MoveLeft unit:=wdCell, Count:=1
# Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPercent
# Selection.Range.Cells(1).PreferredWidth = 65
#
# Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
# Selection.TypeText Text:="Some other text"
#
# With Selection.HeaderFooter.PageNumbers
# .RestartNumberingAtSection = True
# .StartingNumber = 1
# End With
#
# 'clear header for fourth page
#
'-------------------------------------------------------------------------
# ActiveWindow.ActivePane.View.NextHeaderFooter
# Selection.HeaderFooter.LinkToPrevious = False
# Selection.WholeStory
# Selection.Delete
# Selection.HeaderFooter.LinkToPrevious = False 'twice because once
doesn't seem to work
#
# 'fill header for fourth page
#
'-------------------------------------------------------------------------
#
# Selection.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=3,
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
#
# With Selection.Tables(1)
# If .Style <> "Tabelraster" Then
# .Style = "Tabelraster"
# End If
# .ApplyStyleHeadingRows = True
# .ApplyStyleLastRow = True
# .ApplyStyleFirstColumn = True
# .ApplyStyleLastColumn = True
# End With
#
# With Selection.Range.Cells(1)
# .PreferredWidthType = wdPreferredWidthPercent
# .PreferredWidth = 8
# End With
#
# Selection.MoveRight unit:=wdCell, Count:=1
# Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPercent
# Selection.Range.Cells(1).PreferredWidth = 17
# Selection.MoveRight unit:=wdCell, Count:=1
# Selection.Range.Cells(1).PreferredWidthType = wdPreferredWidthPercent
# Selection.Range.Cells(1).PreferredWidth = 75
#
# Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
# Selection.TypeText Text:="Again some text"
# Selection.MoveLeft unit:=wdCell, Count:=1
#
# With Selection.Cells
# With .Borders(wdBorderLeft)
# .LineStyle = wdLineStyleSingle
# .LineWidth = wdLineWidth050pt
# .Color = wdColorAutomatic
# End With
# With .Borders(wdBorderRight)
# .LineStyle = wdLineStyleSingle
# .LineWidth = wdLineWidth050pt
# .Color = wdColorAutomatic
# End With
# .Borders(wdBorderTop).LineStyle = wdLineStyleNone
# .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
# .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
# .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
# .Borders.Shadow = False
# End With
#
# With Options
# .DefaultBorderLineStyle = wdLineStyleSingle
# .DefaultBorderLineWidth = wdLineWidth050pt
# .DefaultBorderColor = wdColorAutomatic
# End With
#
# Selection.MoveLeft unit:=wdCell, Count:=1
# Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
# Selection.TypeText "-"
# Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
# Selection.TypeText "-"
#


# 'Extreme Brutal way to link all following headers to the previous ones
(I know)
# '--------------------------------------------------------------
# On Error GoTo yourDone
# Do While 1 = 1
# ActiveWindow.ActivePane.View.NextHeaderFooter
# Selection.HeaderFooter.LinkToPrevious = True
# Loop
#yourDone:
# On Error GoTo 0
#
# 'Ending
# '---------------------------------------------------------------
# ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

# Selection.GoTo what:=wdGoToPage, which:=wdGoToFirst
# On Error Resume Next
# ActiveDocument.Fields(1).Update 'this is the TOC in my document
# On Error GoTo 0
#End Sub
 
C

Charles Kenyon

You are using the selection object rather than the range object. If you use
the range, you will get more consistent results and not need multiple pages
when your procedure is run.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
R

rVo

Dear Charles,

Could you point me to an example (or some documentation) for this approach
as I have no experience at all with it?

Thanks for your intrest/help.

rVo
 
C

Charles Kenyon

Sorry, I don't know of any. Below is some code that I use in manipulating
headers and footers that may help you get started. I just use the help in
vba.

Private Sub ReplaceHeaders(sTemplateName As String)
' Altered to delete stories 8 October 2005

' Replaces Header and FirstPageHeader with contents from
' base template
' Replaces Footer and FirstPageFooter with contents from
' base template
' Assumes that bookmarks have been preserved in base and copies.
' Otherwise will generate error
' Required bookmarks are "Footer1," "Footer2," "Header1," and
"Header2"
'
Dim rRange As Range
Dim sFooter As String
Dim sHeader As String
'
' First Page Header
Set rRange = ActiveDocument.StoryRanges(wdFirstPageHeaderStory)
rRange.Delete
rRange.InsertFile FileName:=WorkGroupPath _
& "Letters & Faxes\" & sTemplateName, _
Range:="Header1", _
ConfirmConversions:=False, Attachment:=False, Link:=False
' Continuation Header
Set rRange = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
rRange.Delete
rRange.Style = ActiveDocument.Styles("Header 3")
rRange.InsertFile FileName:=WorkGroupPath _
& "Letters & Faxes\" & sTemplateName, _
Range:="Header2", _
ConfirmConversions:=False, Attachment:=False, Link:=False
' First Page Footer
Set rRange = ActiveDocument.StoryRanges(wdFirstPageFooterStory)
rRange.Delete
rRange.InsertFile FileName:=WorkGroupPath _
& "Letters & Faxes\" & sTemplateName, _
Range:="Footer1", _
ConfirmConversions:=False, Attachment:=False, Link:=False
' Continuation Footer
Set rRange = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
rRange.Delete
rRange.InsertFile FileName:=WorkGroupPath _
& "Letters & Faxes\" & sTemplateName, _
Range:="Footer2", _
ConfirmConversions:=False, Attachment:=False, Link:=False
End Sub
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
R

rVo

Thanks Charles, I found out how to do it now.
The solution for this problem lies somewhere in the middle...

I open a new empty document and create the content of the header in it
(through the selection-approach which is more handy for tables and layout).
Then I copy the entire content of this new document, close it and paste the
content into the right header (e.g.
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paste).

Because I create the layout in an empty document I don't get the
timingproblems which I would get normally in a 400+ pages document. And
because I paste into the header range without first selecting it the range
may be empty before I approach it.

If you would like to see the entire code I can post it here or send it to
you.

Many thanks (you made my day) and keep it up.

rVo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top