macro to format header

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Let me attempt to illustrate what I'm trying to do. I have a routing sheet
plus explanation that is 3 pages in length. This must be attached to another
document that has a header. I only want the header to show up after the
routing sheet and the explanation. I've written a macro in order to do all of
this but I have one problem. I create a page break so that I can tell the two
headers to be different; I even tell it to start the numbering from page one.
However, when I run the macro it returns to "continue from previous section"
so that it says page 4 of x instead of 1 of x. Everything else works
flawlessly but I'd like to understand why it keeps resetting the page count.
I am guessing it has something to do with the fact that the macro cannot
adjust dynamically to different page counts and so it gets confused when you
insert 3 pages, but I'm not sure.

Thanks
 
Did you insert a true section break or just a page break? In this instance
you need a section break. Go to Insert | Break | Next Page. Once you have
done that go to your header and on the Header/Footer Toolbar click on Same as
Previous. You should then be able to create a header different that the
first one and it should stay for you. I hope this has been helpful to you.
 
Hi John

We would have to see your code and know what version of Word you're using to
work out what's wrong with it.

But you don't need a macro to achieve what you need (unless the purpose of
the macro is to automate the process).

If you start with the routing slip, insert a section break (a page break
isn't sufficient), then insert the "other document" (with header) into the
bottom of the routing slip (without header), it should produce what you need
without any adjustments. But that might not be practicable.

If you insert the routing slip (without header) into top of the "other
document" (with header), then you will need to do the following:
- separate the two parts with a section break (not a page break)
- in Section 2, set all headers and footers so they are not "same as
previous"
- use Insert > Page Number > Foramt to set Start At to 1, and the click OK
and **Close** (Not OK in the last dialog box)
- delete the text of headers and footers in Section 1

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
That is exactly what I'm trying to achieve: simplify the process for a
co-worker so that she doesn't have to waste time clicking all the buttons.
She has several of these documents to go through and send out.

Here goes:
Sub Macro1()

Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveUp Unit:=wdScreen, Count:=1
Selection.Paste
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
Selection.MoveDown Unit:=wdLine, Count:=15
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.MoveUp Unit:=wdLine, Count:=44
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Just tried what you said, it accomplishes just what I did originally. Page 6
of 3 is the first footer.
 
Hi John

I'm assuming that:
(1) You open both the routing slip and the main document
(2) You select the text in the routing slip and do Edit > Copy (or ctrl-c)
(3) You switch to the main document
(4) You click into the main document to indicate where to paste
(5) You now want to run a macro to (a) paste the text and (b) clean up the
headers and footers.

If that's the scenario, then the following should work. But test carefully:
there are no guarantees!

Option Explicit

Sub PasteTextAsNewSectionWithNoHeadersOrFooters()
'microsoft.public.word.docmanagement 16 July 2005
Dim oRange As Word.Range
Dim oNewSection As Word.Section
Dim oOldSection As Word.Section

''Before running this
''click in the document to
''indicate where to paste
''the contents of the clipboard

'Insert a section break at the selection
Set oRange = Selection.Range
With oRange
.Collapse wdCollapseStart
.InsertBreak Type:=wdSectionBreakNextPage
End With

'Paste the contents of the clipboard to the
'new section
With oRange
.Move Unit:=wdCharacter, Count:=-1
.InsertBefore vbCrLf & vbCrLf
.Collapse wdCollapseStart
.Paste
End With

'Get a reference to the sections
Set oNewSection = oRange.Sections(1)
Set oOldSection = ActiveDocument.Sections(oRange.Sections(1).Index + 1)

With oOldSection
'Unlink the H&Fs in the old section
.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False

'Set the starting page number for the old section
'as 1. Using both these might be overkill, but it works.
With .Headers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
End With

'Empty out the Headers and Footers in the new section
With oNewSection
.Headers(wdHeaderFooterEvenPages).Range.Delete
.Headers(wdHeaderFooterFirstPage).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Delete
.Footers(wdHeaderFooterEvenPages).Range.Delete
.Footers(wdHeaderFooterFirstPage).Range.Delete
.Footers(wdHeaderFooterPrimary).Range.Delete
End With

End Sub


For further endeavours, you may find the following useful:
How to modify a recorded macro
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
Back
Top