FileName w/o Disturbing Text Already in Footer

J

jerem

It is amazing how long one can meander through Intellisense trying to find
out how to do one simple thing? Mama Mia!! You come across something that
looks hopeful only to try it and realize Ooops, Object missing or some thing
or other on that line. So after a good long effort (and a considerable
amount of time down the drain) I'm throwing in the towel and asking Doug,
Graham, Greg or anyone else who comes upon this first:

Here's the code:

Dim oRng As Range
Dim oSection As Section
Dim oFooter As HeaderFooter

Call SameAsPrevious
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng
Fields.Add oRng, wdFieldFileName
.Collapse wdCollapseEnd
.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With
oFooter.Range.Paragraphs(1).Range.Font.Size = 8
End If
Next oFooter
Next oSection
End Sub

In a previous communication with Doug he'd given me various solutions for
FileName and Page Numbering. I've split some of the code up to give me just
a FileName in the footers, just Page Numbering in the Footers, a FileName and
Page Numbering and a few other variations which I've placed in a menu on a
toolbar. There's just one more variation I need and that is placing the
FileName in the Footer (which is accomplished by the above code) but placed
in there without deleting what already exists in the footer. I need the
FileName to be inserted at the very end of the Footer. Thanks in advance for
your help.
 
S

Stefan Blom

You have to collapse the range object *before* adding the field; otherwise
you are overwriting the information already in the range. Also, you forgot
to add a period before "Fields" in the With statement. Something like this
should work:

Sub testing()
Dim oRng As Range
Dim oSection As Section
Dim oFooter As HeaderFooter

For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range.Duplicate
With oRng
.Collapse wdCollapseEnd 'First this...
.Fields.Add oRng, wdFieldFileName '...and then this...
.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With
oFooter.Range.Paragraphs(1).Range.Font.Size = 8
End If
Next oFooter
Next oSection
End Sub
 
J

jerem

Yes, I finally figured that out after going through the msdn online
dictionary of terms -- .Collapse -- to see exactly what this statement means.
The following code is what I'm using and it works fine with one exception -
I want to use this macro to add a file name to a document that may already
have text in the footers - page numbers amongst other things. The only
problem is to get the filename to come in at 8 pt regardless to what numbered
paragraph it's coming in at. With the code I'm using now --
oFooter.Range.Paragraphs(2).Range.Font.Size = 8, this assumes that the
filename is going to come into the second paragraph of the footer (assuming
there's already a page number in the 1st paragraph of the footer). Don't
particularly like these assumptions and was wondering if there is any way to
specifically designate that the filename comes in at 8 pt rather than setting
a font setting for a paragraph number. Why? What if text in the footer is
3 lines and the filename is now coming in as the 4th paragraph? The code in
use now would actually change the font of the existing text in paragraph(2)
to 8 pt and the filename would come in at whatever font is designated for
paragraph(3). Not particularly crazy about that. Any ideas or suggestions
you might have woud be greatly appreciated. Again, I'm browsing that
Intellisense list, but no guarantee I find a solution in a timely manner. My
sentence structure in VBA is severely lacking. Okay, I need nouns, verbs,
adjectives for English sentences. What do I need to list first in VBA -
Objects, then Methods, then Properties? You got me! I just muddle around
and sometimes I get lucky and the sentence works.

Again, thanks for your reply and your willingness to help.

Sub InsFileNameAfterCurrentTextInFooters()

' Macro created 8/27/2009 by dwallace

Dim oRng As Range
Dim oSection As Section
Dim oFooter As HeaderFooter

Call SameAsPrevious
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng
.Collapse Direction:=wdCollapseEnd
.Fields.Add oRng, wdFieldFileName
.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With
oFooter.Range.Paragraphs(1).Range.Font.Size = 12
oFooter.Range.Paragraphs(2).Range.Font.Size = 8
oFooter.Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
oFooter.Range.Paragraphs(2).Alignment = wdAlignParagraphLeft
End If
Next oFooter
Next oSection
End Sub

However, one additional question
 
J

jerem

Continuation of my last post:

I am now trying to use this code (see code below) to format the file name
rather than the statement: oFooter.Range.Paragraphs(2).Range.Font.Size = 8
(see my prevoius post which formats the filename based on its position in the
footer). However, the code below does not migrate into every footer when
there are multiple sections. It only formats the filename in Sections 1's
footers only. I will continue to plug along to solve this, but anyone out
there know why this won't format all the footers in all the sections???
Thanks for your help.


Sub FormatFileName()
'
' wdEvenPagesFooterStory = 8
' wdPrimaryFooterStory = 9
' wdFirstPageFooterStory = 11
Dim oFld As Field
Dim rTmp As Range
For Each rTmp In ActiveDocument.StoryRanges
Select Case rTmp.StoryType
Case 8, 9, 11
For Each oFld In rTmp.Fields
If oFld.Type = wdFieldFileName Then
oFld.Select
With Selection
.Font.Size = 8
.ParagraphFormat.Alignment = wdAlignParagraphLeft
ActiveWindow.ActivePane.Close
End With
'Stop
' oFld.Delete ' if you like
End If
Next
End Select
Next
End Sub
 
S

Stefan Blom

Each section can have up to three different footers (and headers); this
happens if you select "Different first page" as well as "Different odd and
even" in Page Setup.

When modifying footer contents, you will have to access each type of footer,
in each section, separately. For example, the following code would change
the font size of the first paragraph in each footer in all sections of the
current document:

Sub testing()
Dim s As Section

For Each s In ActiveDocument.Sections
s.Footers(wdHeaderFooterEvenPages).Range.Paragraphs(1).Range.Font.Size = 8
s.Footers(wdHeaderFooterFirstPage).Range.Paragraphs(1).Range.Font.Size = 8
s.Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range.Font.Size = 8

Next s
End Sub

However, since you have different footers with different contents in them,
it may be easier to use code to insert and format the contents, section by
section, footer by footer.
 
J

jerem

Since I last wrote I managed to pilfer the code below and chang it slightly
to solve my problem. I call this macro after executing another macro that
puts the filename in all the footers. And, again, the reason I needed this
one is because the other one formatted the filename positionally (by
paragraph #) which was a problem if the filename didn't get placed into a
certain paragraph (which was the case if the footer already contained text in
it). The code below finds the filename and formats the filename wherever it
resides. Thanks for your help.

Sub FormatFileName()
'8 pt
Dim oSec As Section
Dim oFoot As HeaderFooter
Dim oField As Field

For Each oSec In ActiveDocument.Sections

For Each oFoot In oSec.Footers
If oFoot.Exists Then
For Each oField In oFoot.Range.Fields
If oField.Type = wdFieldFileName Then
oField.Select
With Selection
.Font.Size = 8
ActiveWindow.ActivePane.Close
End With
End If
Next oField
End If
Next oFoot
Next oSec
End Sub
 

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