Writing neat VBA

R

Roderick O'Regan

I have a document with Odd and Even pages headers and footers.

At this point I insert a new section and which I have to remove the
header and footer linking to the previous section.

I create a temporary bookmark, add a page break, unlink the primary
and even headers and footers, remove the bookmark and then the page
break.

Everything works as it should and there is no problem.

However, below is the code which does all the unlinking and I just
wanted to say the same thing but with a little more elegance and
finesse.

Could somebody therefore, please show me how these few lines might be
written in properly coded VBA?
======================
ActiveWindow.ActivePane.View.SeekView = wdSeekEvenPagesHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.Bookmarks("temp").Select
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.Bookmarks("temp").Select
ActiveWindow.ActivePane.View.SeekView = wdSeekEvenPagesFooter
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.Bookmarks("temp").Select
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
========================
You might wonder why all the backwards and forwards between the
headers, main document and back to the footers with the selection of
the temp bookmark in between?

Well, I found that Word "got lost"(i.e. didn't go to where I expected
it to go) if I told it to unlink the odd and even headers and then do
the same with the odd and even footers. It somehow got confused and
went and did things either in the previous section or the one
following the new section.

I created some explicit actions and that did the trick!!

Regards

Roderick
 
D

Doug Robbins

This is the way to do it:

With Selection.Sections(1)
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
End With


--
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
 
J

Jezebel

If you want to avoid any assumptions about which headers and footers are
defined --

Dim pHeaderFooter as Word.HeaderFooter
With Selection.Sections(1)
For each pHeaderFooter in Footers
pHeaderFooter.LinkToPrevious = FALSE
Next
For each pHeaderFooter in Headers
pHeaderFooter.LinkToPrevious = FALSE
Next
End With
 
D

Doug Robbins

Neat, but not quite. While it compiles OK without the periods before
Footers and Headers, it produces an error when run.

This works:

Dim pHeaderFooter As Word.HeaderFooter
With Selection.Sections(1)
For Each pHeaderFooter In .Footers
pHeaderFooter.LinkToPrevious = False
Next
For Each pHeaderFooter In .Headers
pHeaderFooter.LinkToPrevious = False
Next
End With


--
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
 
J

Jezebel

Runs fine on my machine (W2003).



Doug Robbins said:
Neat, but not quite. While it compiles OK without the periods before
Footers and Headers, it produces an error when run.

This works:

Dim pHeaderFooter As Word.HeaderFooter
With Selection.Sections(1)
For Each pHeaderFooter In .Footers
pHeaderFooter.LinkToPrevious = False
Next
For Each pHeaderFooter In .Headers
pHeaderFooter.LinkToPrevious = False
Next
End With


--
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
 
D

Doug Robbins

Not here, also on 2003.

--
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

Roderick O'Regan

Thank you both for the Master Class on good VBA coding.

I tried your first version Doug and it did exactly what I needed to be
done.

What I noticed is the fact that I didn't need to have both the Odd as
well as the Even page header/footer displayed. That's why I created
the page break in my first attempt.

If the section started just with an Odd footer in view it also
unlinked the even one when text flow created the next page which was
that section's Even footer.

Next I'm going to test out your design, Jezebel, on my Word XP as well
as 2003 and see what happens. I might have to adopt Doug's amended
version if Word versions start going funny on me.

But once again, thanks.

Roderick
 

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