Footers again

C

Charles Germann

Word 97
VB 6

I am trying to create footers in a macro. I have just about everything
working with the different sections (I want a different footer for each
section)

The problem I am having is when I execute this line of code:

Selection.HeaderFooter.LinkToPrevious = False

I lose focus to the footer I wanted to type in. The footer "box" is still up
but my cursor is no longer inside, so when I add the text:

Selection.TypeText Text:=strFooter

It does not show up.....

How can I set focus back to the footer after I execute the LinkToPrevious =
False code?

Thanks
 
W

Word Heretic

G'day "Charles Germann" <[email protected]>,

use your own range object, not selection. I go into this exact topic
(working with H&F ranges) extensively in my Word VBA Spellbook.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


Charles Germann was spinning this yarn:
 
M

Mark Tangard

Hi Charles,

Rule of thumb: When manipulating headers & footers, the fastest way
to a dead end is to try to do it using the Selection object. ;) In
almost any code you create or record, if you see the Selection object
spattered all over it, especially Selection.TypeText and even more
especially if you see 'SeekView' anywhere, it usually means you're
headed for trouble.

Try running this code in a new empty document:

Dim sek as Section
Set sek = Selection.Sections(1)
sek.Headers(wdHeaderFooterPrimary).Range.Text = "XYZ"

Notice how the screen doesn't even quiver; the header just...arrives.

Other advantages:

1 - Focus doesn't matter. The cursor can be anywhere in the file,
and the footer pane can even be closed (which is cleaner anyway).

2 - The selection, if any, doesn't move. So you can run code like
this without worrying about where you're aimed.

3 - You can build a header/footer that you can't even see yet, e.g.,
you can set the second-page-and-beyond header for a letter before
your document even acquires its second page. Try this in a new
empty one-page document, in Print Layout View:

Dim r As Range, d As Document
Set d = ActiveDocument
d.PageSetup.DifferentFirstPageHeaderFooter = True
Set r = d.Sections(1).Headers(wdHeaderFooterPrimary).Range
r.Text = "This is the header for page 2 and beyond."

Now press CTRL+Enter to make a hard page break and look at the
top of page 2.

Things get tricker when you need a field in your header or
footer. If the code gyrations seem a bit complex for that,
you can enter your whole footer as an AutoText and have it
inserted (again, at a *range*) in one go.

Using ranges can be tough to wrap your brain around at first,
but it's almost always faster, quieter and more reliable than
using the Selection.
 
C

Charles Germann

I appreciate your help Mark.

I'm still having a problem with this macro though....

I create a report that is driving from information I get from a database.
Everything works great there.

Ass I read information from the database, I look for certain things.. If I
find what I am looking for I create a new section:

Selection.InsertBreak Type:=wdSectionBreakContinuous

Should I be using this instead?
Selection.Sections.Add

Lets say this is Section 2
Once I have created this new section, I want to create a footer for this
section

I thought that if I used:

Selection.Sections(2).Footers(wdHeaderFooterPrimary).LinkToPrevious =
False

And
Selection.Sections(2).Headers(wdHeaderFooterPrimary).Range.Text = "XYZ"

This would do what I wanted... The problem is that "Selection.Sections(2) "
doesn't seem to exist, I get an error.

How do I create the new section or access the new section that I have
created?

Thanks again Mark
 
M

Mark Tangard

Charles,

Brace yourself, this is one of those forehead-slapper answers:

The first line of code to add the section (by adding the break) is correct,
assuming you want a 'continuous' break, i.e., not a new-page section break.
The problem is with Selection.Sections(2). Recognize that wherever you see
[ObjectA].[ObjectBplural](Number) it means ObjectA contains a *collection*
of ObjectB's.

But adding a break doesn't add a section to the *Selection*. The selection
is still just your happily blinking cursor, which is now on the south side
of the break, and therefore it still contains only *one* section. The new
section you've made is the new Selection.Sections(1).

It took me weeks to reccognize the corollary to [ObjectA].[ObjectBplural](#)
above, which is that [ObjectA].[ObjectBplural](1) means "the current ObjectB."
And that's what you want -- the section you just made, i.e., current section.

(If this is the first time you've added a section to the doc, the new section
is indeed ACTIVEDOCUMENT.Section(2), but as helpful as that may seem, it isn't
as likely to be useful later on, when you add more sections, since you'd have
to keep track of the # of increments.) (Unless of course your entire procedure
always adds just one section.)
 
C

Charles Germann

Thank you Mark

I got it working the way I wanted with your help!

Mark Tangard said:
Charles,

Brace yourself, this is one of those forehead-slapper answers:

The first line of code to add the section (by adding the break) is correct,
assuming you want a 'continuous' break, i.e., not a new-page section break.
The problem is with Selection.Sections(2). Recognize that wherever you see
[ObjectA].[ObjectBplural](Number) it means ObjectA contains a *collection*
of ObjectB's.

But adding a break doesn't add a section to the *Selection*. The selection
is still just your happily blinking cursor, which is now on the south side
of the break, and therefore it still contains only *one* section. The new
section you've made is the new Selection.Sections(1).

It took me weeks to reccognize the corollary to [ObjectA].[ObjectBplural](#)
above, which is that [ObjectA].[ObjectBplural](1) means "the current ObjectB."
And that's what you want -- the section you just made, i.e., current section.

(If this is the first time you've added a section to the doc, the new section
is indeed ACTIVEDOCUMENT.Section(2), but as helpful as that may seem, it isn't
as likely to be useful later on, when you add more sections, since you'd have
to keep track of the # of increments.) (Unless of course your entire procedure
always adds just one section.)

--
Mark Tangard, Microsoft Word MVP
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters

Charles said:
I appreciate your help Mark.

I'm still having a problem with this macro though....

I create a report that is driving from information I get from a database.
Everything works great there.

Ass I read information from the database, I look for certain things.. If I
find what I am looking for I create a new section:

Selection.InsertBreak Type:=wdSectionBreakContinuous

Should I be using this instead?
Selection.Sections.Add

Lets say this is Section 2
Once I have created this new section, I want to create a footer for this
section

I thought that if I used:

Selection.Sections(2).Footers(wdHeaderFooterPrimary).LinkToPrevious =
False

And
Selection.Sections(2).Headers(wdHeaderFooterPrimary).Range.Text = "XYZ"

This would do what I wanted... The problem is that "Selection.Sections(2) "
doesn't seem to exist, I get an error.

How do I create the new section or access the new section that I have
created?

Thanks again Mark




still up


LinkToPrevious =
 

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