remove the header of only page 2

J

Jack Sons

Hi all,

With the code below I wanted to get rid of the page header of page 2, the
last page of the doc, after which I remove the entire page 2. Unfortunately
also the pageheader of page 1 disappears.

If possible I would like to use code that deletes page 2 anyhow, with its
header if it has one, but leaving page 1 as it was, including its header if
it has one. If somebody would show me the code I would be most grateful.

If it is necessary to remove the header before deleting the page (like I did
in my code) that is OK with me, but please show me how my code should be
changed to obtain the desired result (that is leaving page 1 intact).

Perhaps the code could be much simpler than mine.

Jack Sons
The Netherlands
----------------------------------------------------------------------------
------------------------
Sub Macro7()

Selection.EndKey Unit:=wdStory
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.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.LargeScroll Down:=-2
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.EndKey Unit:=wdLine
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
 
C

Charles Kenyon

What about the page 3 header?

Have you looked into the page layout command of having different headers for
the first page?

Putting the text "Header text" into an IF field can work.

{ IF { PAGE } = "2" "" "Header text" } will display Header text on every
page except page 2 if placed in a general header. The field braces must be
inserted using Ctrl-F9, not by regular typing.

Don't know how to write code to do this but if you really need vba, you
could record a macro and edit it.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
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.
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

If Page 2 is not in a separate section and deleting the header from page 2
also deleted the header from page 1, there is no need to delete the header
before deleting the page.

If Page 2 is in a separate section, then use

With ActiveDocument.Sections(2)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
End With
Selection.EndKey wdStory
Selection.Bookmarks("\page").Range.Delete

The last two lines of code delete the last page of the document. You can
just use them alone if Page 2 is not in a separate section.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
J

Jack Sons

Doug,

"Manually" I can delete page 2 only if I first remove the pageheader (which
is the same letterhead as is on page 1). As long as the letterhead is there
deleting all text on page 2 will not cause it to go away.

I used your code, it changes absolutely nothing. But if I type some text on
page 2 (it was empty before, except for the letterhead) execution of your
code halts at the last line with an error message saying the the bookmark
doesn't excist.

BTW, how to delete a whole page other than by deleting all text on it? (that
is the way I always use, I mean without using VBA, when all text is deleted
the page will disappear).

Also this: how can I determine wether page 2 is in a separate section or
not? (I think it is)

Most important question:

The second (empty) page is the result of breaking a merged document into its
subdocs with the code below. The subdocs should be one page letters, but
after breaking down all individual subdocs suddenly will have that miserable
and unwanted extra page with the same letterhead as page 1 an nothing else
on it. Where and why did that extra page come into being and what should be
done to prevent it or cure it.

Hope you will help me again, I came so close to solving my problem.

Jack.

Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "Myletter" & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste


'---------------------------------------------------------------------------
----------------------
Dim naam As String
Dim volgnummer As String
Dim klascode As String
Dim halvenaam As String
Dim toevoegsel As String

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
volgnummer = Selection.Text

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=3
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
klascode = Selection.Text

Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCell
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
naam = Selection.Text

Selection.HomeKey Unit:=wdStory

halvenaam = naam + " " + volgnummer + " " + klascode
toevoegsel = "achterstand 060204"
helenaam = halvenaam + " " + toevoegsel


'---------------------------------------------------------------------------
----------------------

ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous

ActiveDocument.SaveAs "C:\Documents and Settings\2\Mijn
documenten\Word\INCASSO\CORRESPO\MyLetter\" + helenaam,
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=False, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Counter = Counter + 1
Wend


"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
If Page 2 is not in a separate section and deleting the header from page 2
also deleted the header from page 1, there is no need to delete the header
before deleting the page.

If Page 2 is in a separate section, then use

With ActiveDocument.Sections(2)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
End With
Selection.EndKey wdStory
Selection.Bookmarks("\page").Range.Delete

The last two lines of code delete the last page of the document. You can
just use them alone if Page 2 is not in a separate section.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
Jack Sons said:
Hi all,

With the code below I wanted to get rid of the page header of page 2, the
last page of the doc, after which I remove the entire page 2.
Unfortunately
also the pageheader of page 1 disappears.

If possible I would like to use code that deletes page 2 anyhow, with its
header if it has one, but leaving page 1 as it was, including its header
if
it has one. If somebody would show me the code I would be most grateful.

If it is necessary to remove the header before deleting the page (like I
did
in my code) that is OK with me, but please show me how my code should be
changed to obtain the desired result (that is leaving page 1 intact).

Perhaps the code could be much simpler than mine.

Jack Sons
The Netherlands
--------------------------------------------------------------------------
--
------------------------
Sub Macro7()

Selection.EndKey Unit:=wdStory
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.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.LargeScroll Down:=-2
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.EndKey Unit:=wdLine
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

--------------------------------------------------------------------------
--
 
P

Peter Hewett

Hi Jack

When you use MailMerge to create an output document it separates each
document in the output file using a section break. Additionally it always
includes a redundant section break at the end of the document, so you
normally have 1 more Section than you do actual documents.

The reason you're getting the extra blank page is that when you select and
cut you sections it includes the section break from the mail merge
document. The easy solution is to just delete the second section!

The following code should delete the extra section break in the document.
You need to run this code against the output file you paste the section
into, not the mail merge output document:

Dim rngSection2 As Word.Range

Set rngSection2 = ActiveDocument.Sections(2).Range
rngSection2.MoveStart wdCharacter, -1
rngSection2.delete

HTH + Cheers - Peter


Doug,

"Manually" I can delete page 2 only if I first remove the pageheader
(which is the same letterhead as is on page 1). As long as the
letterhead is there deleting all text on page 2 will not cause it to go
away.

I used your code, it changes absolutely nothing. But if I type some text
on page 2 (it was empty before, except for the letterhead) execution of
your code halts at the last line with an error message saying the the
bookmark doesn't excist.

BTW, how to delete a whole page other than by deleting all text on it?
(that is the way I always use, I mean without using VBA, when all text
is deleted the page will disappear).

Also this: how can I determine wether page 2 is in a separate section or
not? (I think it is)

Most important question:

The second (empty) page is the result of breaking a merged document into
its subdocs with the code below. The subdocs should be one page letters,
but after breaking down all individual subdocs suddenly will have that
miserable and unwanted extra page with the same letterhead as page 1 an
nothing else on it. Where and why did that extra page come into being
and what should be done to prevent it or cure it.

Hope you will help me again, I came so close to solving my problem.

Jack.

Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "Myletter" & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste


'------------------------------------------------------------------------
--- ----------------------
Dim naam As String
Dim volgnummer As String
Dim klascode As String
Dim halvenaam As String
Dim toevoegsel As String

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
volgnummer = Selection.Text

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=3
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
klascode = Selection.Text

Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCell
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
naam = Selection.Text

Selection.HomeKey Unit:=wdStory

halvenaam = naam + " " + volgnummer + " " + klascode
toevoegsel = "achterstand 060204"
helenaam = halvenaam + " " + toevoegsel


'------------------------------------------------------------------------
--- ----------------------

ActiveDocument.Sections(2).PageSetup.SectionStart =
wdSectionContinuous

ActiveDocument.SaveAs "C:\Documents and Settings\2\Mijn
documenten\Word\INCASSO\CORRESPO\MyLetter\" + helenaam,
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=False, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Counter = Counter + 1
Wend


"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
 
P

Peter Hewett

Hi Jack

I should have said:

Additionally it always includes a redundant section break at the end of the
document, so you normally have the same number of Sections as you do actual
documents.

Cheers - Peter
 
J

Jack Sons

Peter,

Thanks for your answer, fast as lightning!
You need to run this code against the output file you paste the section
into, not the mail merge output document:

Where exactly do I put your code in mine? If the subdocs exist of, say, 4
pages ech, a fifth will be added. So I would like your code to delete not
specifically the second page (section?) but the "last" one. How should your
code read to cover that?
so you normally have 1 more Section than you do actual documents.

I noticed that.
So with 20 letters in the mailmerged doc my code saves 19 and the 20th
remains (on the screen). The result is that the last break down doc is not
saved and stays on the screen as a remainder of the original mailmerge
product. (Or that something else?)
I therefore did put after the "wend" the "break down and save code" again,
as you see in the snippet below. Not very clever code, I am afraid, but it
works perfectly.

The only problem I encountered is that each saved doc - from the 1st up to
and including the 20th - has that wretched extra page that is empty but
comes with the header of page 1. Normally when I write a letter longer than
one page each following page does not have that header, just as I want it to
be.
Of course I hope and expect your code will get rid of the extra page, but
please also explain why that extra generated page has the header in the
first place. Perhaps you already said why, but then I did not understand. I
am only beginning to manipulate Word docs with VBA, so I am really not a
great light in this field (My knowledge of English is not very good, should
I say "not a genius"?)

Jack.


'------------------------------------------------------------

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=2
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
volgnummer = Selection.Text

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=3
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
klascode = Selection.Text

Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCell
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
naam = Selection.Text

Selection.HomeKey Unit:=wdStory

halvenaam = naam + " " + volgnummer + " " + klascode
toevoegsel = "achterstand 060204"
helenaam = halvenaam + " " + toevoegsel

ActiveDocument.SaveAs "C:\Documents and Settings\2\Mijn
documenten\Word\INCASSO\CORRESPO\MyLetter\" + helenaam,
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=False, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close


'---------------------------------------------------------------------------
---------------------------

Application.ScreenUpdating = True
End Sub
 
J

Jack Sons

Peter,

BTW, did I see it right that you live in New Zealand, where it is 9 hours
earlier than here (The Hague, The Netherlands) and that you also (like me)
do the NG things very often late at night? My compliments!
I wouldn't know how to survive in Excel- and now in Word-land without the
support of the newsgroups.

Jack.
 
P

Peter Hewett

Hi Jack

I should have been clearer:

' part of your code....
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste

Replace the above code with the following:
Dim docNew As Word.Document
Dim rngSection2 As Word.Range

ActiveDocument.Sections.First.Range.Cut
Set docNew = Documents.Add
docNew.Content.Paste

Set rngSection2 = docNew.Sections(2).Range
rngSection2.MoveStart wdCharacter, -1
rngSection2.delete

' Now save the new Word document - use the docNew document object!!!
docNew.SaveAs "Your path and filename here"

But move the variable declaarations (the 2 Dim statements) to the top of you
macro.

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Jack

I do live in NZ - A beautiful country that Lord Of The Rings and Last Samurai
ALMOST does justice to!!.

I work from home, I develop software based around Word/Excel using VBA/VB and
a little dot.Net. If I'm not to busy I jump on and chip in - IF I know the
answer!

But yeah, it's 1.40AM here so it's off to bed for me soonish!

Good luck and I hope you find your answer(s) soon!!

Cheers - Peter
 

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