Header Footer Story Ranges Page Number Text box causes problems

K

Kerri

Hi,

I am currently using Word 2003. I am using story ranges for the
headers and footers. I am having a few problems.

If there is a Page number in a text box in the Footer and you use any
of the collapse options it will collapse in the text box and therfore
insert text in the textbox. I want it to collapse to the beginning of
the Footer text not in the text box. I was using the Collapse options
so I could then bookmark the new text inserted in that range. I
realize you can use InsertBefore but that won't let me then make the
text I just inserted in to a bookmarked range...or will it?

Goals:
1. Go through the Footer stories and insert text for all stories.
2. Insert BEFORE all other text.
3. Bookmark the text just inserted with a unique name for each section
4. If Bookmark already exist then just replace that text with new text


Problem:
1. If I have 2 sections it will run the macro 2x and insert text 2x
into the footer.
2. If there is nothing in the footer it will error... I don't have
the code below but I will usually have it open the storyrange and
insert a space so I can put text in it.
3. If there is a TEXT BOX in the footer it will insert it in that
textbox and not in the footer.
4. If there is existing text in the footer, I want it to tab it over
or move to next line after I insert my text. This sounds easier than
it's been.

Thank you for your help.
Kerri

Here is my CODE:.
Sub FooterText()
Dim pFooter As Word.Range
Dim pIndex As Long
For pIndex = 1 To 4
On Error Resume Next
Set pFooter = ActiveDocument.StoryRanges(Choose(pIndex,
wdEvenPagesFooterStory, wdFootnotesStory, wdPrimaryFooterStory,
wdFirstPageFooterStory)) 'wdFirstPageFooterStory,
On Error GoTo 0
Do Until pFooter Is Nothing
:
strMyText = "_TEST_"
' pFooter.Text = "blah blah blah" 'This replaced all existing
text
Dim strBKEnd, strBKMKName As String
If pFooter.Text <> "" Then
' pFooter.InsertBefore strMyText
pFooter.Collapse wdCollapseStart '*keeps it from
bookmarking the entire footer
pFooter.InsertBefore strMyText
SecIndex = pFooter.Sections.Count
Select Case pFooter.StoryType
Case wdFirstPageFooterStory
strBKEnd = "_First"
Case wdEvenPagesFooterStory
strBKEnd = "Even"
Case Else
strBKEnd = ""
End Select
strBKMKName = "BKMK_" & SecIndex & strBKEnd
pFooter.Bookmarks.Add (strBKMKName)
End If
:
Set pFooter = pFooter.NextStoryRange
Loop
Next
End Sub
 
G

Greg Maxey

Maybe something like this will get you on track:

Sub FooterText()
Dim pFooter As Word.Range
Dim i As Long
Dim strMyText As String
Dim SecIndex As Long
Dim oRng As Word.Range
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
End With
Next i
For i = 1 To 3
On Error Resume Next
Set pFooter = ActiveDocument.StoryRanges(Choose(i,
wdFirstPageFooterStory, wdEvenPagesFooterStory, wdPrimaryFooterStory))
On Error GoTo 0
strMyText = "_TEST_"
Do Until pFooter Is Nothing
SecIndex = pFooter.Sections.Count
Select Case pFooter.StoryType
Case wdFirstPageFooterStory
If Len(pFooter.Text) = 1 Then
pFooter.Text = strMyText
ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex & "_First",
pFooter
Else
If ActiveDocument.Bookmarks.Exists("BKMK_" & SecIndex &
"_First") Then
Set oRng = ActiveDocument.Bookmarks("BKMK_" & SecIndex &
"_First").Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
Else
Set oRng = pFooter
oRng.Collapse wdCollapseStart
oRng.Text = strMyText
oRng.InsertAfter vbCr
ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
End If
End If
Case wdEvenPagesFooterStory
'Build similiar to above
Case wdOddPagesFooterStory
'Build similiar to above
End Select
Set pFooter = pFooter.NextStoryRange
Loop
Next
End Sub
 
K

Kerri

Hi Greg,
Thank you for taking the time to help me with this code. I played
with your code and modified it a little. But I couldn't get it to
accomplish a few things. I listed them as numbers so it is easier to
reference.

1. It won't insert text in to the footer if there isn't something
there to start with. If you run the code on new blank document with
nothing in the footer. Do you know a way around that?
2. I found that by doing collapseEnd, then expand the selection, that
it would put the my range at the start of the footer instead in the
text box.

oRng.Collapse wdCollapseEnd '**End wont put the
text in the pagenumber text box located in the footer
3. 'Question: Do you know of a way to get it to expand to "Control
Home" like you would do in selecting?
oRng.Expand unit:=wdParagraph '**Expands range to
begining of paragraph..hopefully this is enough
oRng.Collapse wdCollapseStart '**Collpases it at
the begining where I truly want it.
oRng.Text = strMyText
oRng.InsertAfter vbCr

4. Question: What does Len(pFooter.text) = 1 do? I read that it is
used to check the lengh of a string.
5. Last question: Dim i as Long - is used for both the Section.count
and the pFooter index. Shoud they each have their own respective i and
j instead? I'm wondering if this is why it seems to skip a footer
when "linktoprevious" is turned off?

Thank you for your help with this. ~Kerri
I posted my modified code below.

Kerri, Modified Version
'Works for not inserting in to the Textbox.
'Not putting in sections where Link to previous is turned off.
Sub FooterText()
Dim pFooter As Word.Range
Dim i As Long
Dim strMyText As String
Dim SecIndex As Long
Dim oRng As Word.Range
Dim j As Integer
Dim strBookmarkName As String
Dim strMyFtrType As String

'This is just to get a count of the sections
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterEvenPages).LinkToPrevious = False
End With
Next i

'Create a 3 page doc. Section one has tow pages, the first page is
Diff. first Page, second is just section1.
'then create a new section at the end of page two (gives you page
3 sec 2) Turn off Link to previous on the footer
'of section 2.
For j = 1 To 3 'If you leave as i then it skips section1 primary, If
you make it j then it skips section 2primary (all with section two's
Same as Previous turned off.)
On Error Resume Next
Set pFooter = ActiveDocument.StoryRanges(Choose(j,
wdFirstPageFooterStory, wdEvenPagesFooterStory, wdPrimaryFooterStory))
On Error GoTo 0

strMyText = "_HELLO_"


Do Until pFooter Is Nothing
SecIndex = pFooter.Sections.Count

strBookmarkName = "Bkmk_" & SecIndex & strMyFtrType
Select Case pFooter.StoryType

Case wdFirstPageFooterStory
strMyFtrType = "_First"
If Len(pFooter.Text) = 1 Then
pFooter.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, pFooter
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex & "_First",
pFooter
Else
If ActiveDocument.Bookmarks.Exists(strBookmarkName) Then
' If ActiveDocument.Bookmarks.Exists("BKMK_" & SecIndex &
"_First") Then
Set oRng =
ActiveDocument.Bookmarks(strBookmarkName).Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
Else
Set oRng = pFooter
'**Could check here to see if there is any text in the
range...if so then add a hard return
oRng.Collapse wdCollapseEnd '**End wont put the text in
the footer
oRng.Expand unit:=wdParagraph '**Expands range to
begining of paragraph..hopefully this is enough
oRng.Collapse wdCollapseStart '**Collpases it at the
begining where I truly want it.
oRng.Text = strMyText
oRng.InsertAfter vbCr
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
End If
End If

Case wdPrimaryFooterStory
strMyFtrType = "_Main"
If Len(pFooter.Text) = 1 Then
pFooter.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, pFooter
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex & "_First",
pFooter
Else
If ActiveDocument.Bookmarks.Exists(strBookmarkName) Then
' If ActiveDocument.Bookmarks.Exists("BKMK_" & SecIndex &
"_First") Then
Set oRng =
ActiveDocument.Bookmarks(strBookmarkName).Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
Else
Set oRng = pFooter
'**Could check here to see if there is any text in the
range...if so then add a hard return
oRng.Collapse wdCollapseEnd '**End wont put the text in
the footer
oRng.Expand unit:=wdParagraph '**Expands range to
begining of paragraph..hopefully this is enough
oRng.Collapse wdCollapseStart '**Collpases it at the
begining where I truly want it.
oRng.Text = strMyText
oRng.InsertAfter vbCr
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
End If
End If

Case wdEvenPagesFooterStory
strMyFtrType = "_Even"
If Len(pFooter.Text) = 1 Then
pFooter.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, pFooter
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex & "_First",
pFooter
Else
If ActiveDocument.Bookmarks.Exists(strBookmarkName) Then
' If ActiveDocument.Bookmarks.Exists("BKMK_" & SecIndex &
"_First") Then
Set oRng =
ActiveDocument.Bookmarks(strBookmarkName).Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
Else
Set oRng = pFooter
'**Could check here to see if there is any text in the
range...if so then add a hard return
oRng.Collapse wdCollapseEnd '**End wont put the text in
the footer
oRng.Expand unit:=wdParagraph '**Expands range to
begining of paragraph..hopefully this is enough
oRng.Collapse wdCollapseStart '**Collpases it at the
begining where I truly want it.
oRng.Text = strMyText
oRng.InsertAfter vbCr
ActiveDocument.Bookmarks.Add strBookmarkName, oRng
' ActiveDocument.Bookmarks.Add "BKMK_" & SecIndex &
"_First", oRng
End If
End If
End Select
Set pFooter = pFooter.NextStoryRange
Loop
Next
End Sub
 
G

Greg Maxey

Kerri,

I think we were making this too hard. Until you create a storyrange by
putting something in it it doesn't exist. The test I ran were on a document
that all ready had a least an empty paragraph in the footer.

Try this:

Sub FooterText()
Dim strMyText As String
Dim i As Long
Dim j As Integer
Dim pFooter As HeaderFooter
Dim oRng As Word.Range
Dim strMyFtrType As String
Dim strMyBMName As String
strMyText = "Your Text"
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
For j = 1 To 3
Set pFooter = .Footers(Choose(j, wdHeaderFooterFirstPage,
wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
pFooter.LinkToPrevious = False
strMyFtrType = Choose(j, "First", "Primary", "Even")
strMyBMName = "Bkmk_" & i & "_" & strMyFtrType
If ActiveDocument.Bookmarks.Exists(strMyBMName) Then
Set oRng = ActiveDocument.Bookmarks(strMyBMName).Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strMyBMName, oRng
Else
Set oRng = pFooter.Range
If Len(pFooter.Range.Text) > 1 Then
oRng.InsertBefore vbCr
End If
oRng.Collapse wdCollapseStart
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strMyBMName, oRng
End If
Next j
End With
Next i
End Sub
 
K

Kerri

Greg,

That worked, thank you! I truly appreciate all of your help. I've
learned so much from these news groups and people like you who take
the time share their expertise.
I hate to ask any more of you, but I can't figure out why it is still
inserting the text in the page number textbox. I have a work around
(below) but it will only work if there is only one paragraph in the
footer. Have you come across this problem before?

oRng.Collapse wdCollapseEnd
oRng.Expand unit:=wdParagraph
oRng.Collapse wdCollapseStart

Thank you again for your time.
Kerri
 
G

Greg Maxey

Kerri,

No idea. I created a new blank document with 5 pages, added a section
break in between 3 and five and put a text box in the footer of
section 1 with a Page field. I ran the code and all worked well. Are
you sure your page numbering is in a text box? Why?
 
K

Kerri

Hi Greg,

My apologies, your right it's not a text box but a frame. I didn't
notice until I tried to edit the properties of it. Most users will
insert the page number by choosing Insert | Page Number from the
menu. I usually recommend against this method, I suggest opening the
Header/Footer and inserting page numbers that way. But the majority
of the documents we will be using this macro on will have the page
number inserted from the menu. Do you get the same results if you use
the page numbers from the menu?

Kerri
 
G

Greg Maxey

Kerri,

"FRAMES" I hate those things and your problems is a good example of
why. After racking my feeble mind for almost an hour, I think I have
stumbled on something that will work. It appears that the start of
the footer range is actually "after" the end of the frame in the
range. Try this:

Sub FooterText()
Dim strMyText As String
Dim i As Long
Dim j As Integer
Dim pFooter As HeaderFooter
Dim oRng As Word.Range
Dim strMyFtrType As String
Dim strMyBMName As String
strMyText = "Your Text"
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
For j = 1 To 3
Set pFooter = .Footers(Choose(j, wdHeaderFooterFirstPage,
wdHeaderFooterPrimary, wdHeaderFooterEvenPages))
pFooter.LinkToPrevious = False
strMyFtrType = Choose(j, "First", "Primary", "Even")
strMyBMName = "Bkmk_" & i & "_" & strMyFtrType
If ActiveDocument.Bookmarks.Exists(strMyBMName) Then
Set oRng = ActiveDocument.Bookmarks(strMyBMName).Range
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strMyBMName, oRng
Else
Set oRng = pFooter.Range
If oRng.Frames.Count > 0 Then
If InStr(oRng.Frames(1).Range.Fields(1).Code, "PAGE") > 0
Then
oRng.Start = oRng.Frames(1).Range.End + 1
oRng.Collapse wdCollapseStart
End If
End If
If Len(pFooter.Range.Text) > 1 Then
oRng.InsertBefore vbCr
End If
oRng.Collapse wdCollapseStart
oRng.Text = strMyText
ActiveDocument.Bookmarks.Add strMyBMName, oRng
End If
Next j
End With
Next i
End Sub
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Kerri,

"FRAMES" I hate those things and your problems is a good example of
why. After racking my feeble mind for almost an hour, I think I have
stumbled on something that will work. It appears that the start of
the footer range is actually "after" the end of the frame in the
range. Try this:

Why do you hate frames? They are great!

I use them all the time in templates.
They allow you to position text anywhere on a page just by applying a
style...

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
JGM,

Well I suppose I shouldn't hate that which I don't understand. That
was the case here.

Actually I have used them for the purpose you mention.

http://gregmaxey.mvps.org/Margin_Text.htm

Yep, that is the classic example!

But you are right that they can be a pain to manipulate through VBA as they
are part of the text stream even tough they may appear totally disconnected
with the text.
ActiveDocument.Characters(1) could well be at the bottom of page 1, after a
few hundreds characters...

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

Kerri

Thank you Greg!

That is perfect! Thank you so much for sticking with me and all my
questions! I truly appreciate all your help!!! I had tried something
similar to the .frames but with .textbox but I couldn't figure out how
to get it to recgonize it. Now I see how to do it. Your wonderfult!
Thank you!

Kerri
 

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