FileName on Last Page of Word Doc

K

Karen Clark

Hi,

Can anyone help me with the code for the following:

Open the footer
Insert FileName (including path)
preferably without the file extension
in the footer of the LAST PAGE ONLY
left aligned
TNR size 8 font
Gray color
Close the footer

I could record a Macro in Word, but it's bulky and I
didn't know how to record the "last page only" thing.

Thanks very much,
Karen
VB novice
 
J

Jean-Guy Marcil

Karen Clark was telling us:
Karen Clark nous racontait que :
Hi,

Can anyone help me with the code for the following:

Open the footer
Insert FileName (including path)
preferably without the file extension
in the footer of the LAST PAGE ONLY
left aligned
TNR size 8 font
Gray color
Close the footer

I could record a Macro in Word, but it's bulky and I
didn't know how to record the "last page only" thing.

You do not need a macro for this:

Put this on your footer and format it the way you want:
{IF {PAGE} = {NUMPAGES} "{FILENAME}" ""}

Each {} pair is inserted by doing CTRL-F9.

Then select the whole thing and do SHIFT-F9 to toggle the code to the actual
values.
This will appear only on the last page of the document.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg

Karen,

Maybe something like:
Sub SetupFooter()
Dim myString As String


'Strip extention from file name and path
myString = ActiveDocument.FullName
myString = Left(myString, Len(myString) - 4)

'Go to footer

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
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If

'Insert Field Code
With Selection
.Font.Size = 8
.Font.Color = wdColorGray90
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="IF"
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="PAGE"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="NUMPAGES"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=Chr(34) & myString & Chr(34)

End With
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True 'display on

End Sub
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
Karen,

Maybe something like:
Sub SetupFooter()
Dim myString As String
<snip...>

Greg, have you looked at the article on nested fields at
http://word.mvps.org/faqs/macrosvba/NestedFieldsWithVBA.htm


I think you should avoid .SeekView and such code to access headers/footers.
The few times I tried, I got such headaches trying to keep the results
stable and predictable that I eventually gave up and used ranges instead.
The same with the Selection object in headers/footers.... can be a real
pain!

Just my 2 cents!

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

Greg

JGM,

Thanks. You know I thought that I had read every article in the Word
FAQs at least twice, but don't recall this one.

I probably read it before I even knew what VBA was :). Now I know
what it is, but can rarely figured out what to do with it.
 
G

Greg

IVO JGMs post then maybe:

Sub InsertNestedPageFieldInFooterOnTheFly()

Dim MyRange As Range
Dim myString As String

'Strip extention from file name and path
myString = ActiveDocument.FullName
myString = Left(myString, Len(myString) - 4)


Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True

'Insert dummy para at end of document
ActiveDocument.Range.InsertAfter vbCr
Set MyRange = ActiveDocument.Range
MyRange.Collapse wdCollapseEnd
MyRange.Select

'Insert nested field
With Selection
.Font.Size = 8
.Font.Color = wdColorGray90
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="IF"
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="PAGE"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="NUMPAGES"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=Chr(34) & myString & Chr(34)
'.Fields.Update
End With

ActiveWindow.View.ShowFieldCodes = False

'Cut field, delete dummy para mark, and paste field into header
With Selection
.MoveEndUntil Cset:=Chr(34), Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
.Cut
End With
'ActiveDocument.Paragraphs.Last.Range.Delete

Set MyRange = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).Range
MyRange.Collapse wdCollapseEnd
MyRange.Paste
Set MyRange = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).Range
MyRange.Paragraphs.Last.Range.Delete
Application.ScreenUpdating = True

End Sub
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
IVO JGMs post then maybe:

Sub InsertNestedPageFieldInFooterOnTheFly()

Dim MyRange As Range
Dim myString As String

'Strip extention from file name and path
myString = ActiveDocument.FullName
myString = Left(myString, Len(myString) - 4)

Sorry Greg, I do not mean to "nit pick," but I am curious about something.
Why would you want to strip the file extensions, but keep the full path?

I mean why is
X:\Office XP\HQ\Exercice\Aout 2004\Travail\Test\Report_August2004
preferred to
X:\Office XP\HQ\Exercice\Aout 2004\Travail\Test\Report_August2004.doc
?

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

Greg

JGM,

You need to ask the OP that. She is the one that wanted it that way
:)

However since you are "nit picking" ;-). Maybe you can help with this.
When I cut the field code from main text (unlike Dave Rado's code) it
includes the paragraph mark. When I paste in the footer it results in
two pms in the footer hence the code to remove the last one. I
couldn't figure out how to cut my code (less the pm) from the main text?
 
J

Jean-Guy Marcil

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

You need to ask the OP that. She is the one that wanted it that way
:)

Right, missed that!
Strange, no?
However since you are "nit picking" ;-). Maybe you can help with
this. When I cut the field code from main text (unlike Dave Rado's
code) it includes the paragraph mark. When I paste in the footer it
results in two pms in the footer hence the code to remove the last
one. I couldn't figure out how to cut my code (less the pm) from the
main text?

Select the field without the ¶, cut it, then delete the ¶. Would that work?

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

Charles Kenyon

I would recommend simply creating a Word field for this and storing it as an
AutoText entry. See
http://www.mvps.org/word/FAQs/Numbering/PageNumbering.htm for information on
how to construct a field that will only display on the last page of a
document. The easiest place to put this field is in a header or footer.

If necessary, you could then write a macro to insert the AT entry.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: 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.
 
G

Greg Maxey

JGM,

That is what I wanted to do, but when collapsed the field and tried to
select it I got an error. The only way I could get the field was to get it
and the pm.
 
J

Jean-Guy Marcil

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

That is what I wanted to do, but when collapsed the field and tried to
select it I got an error. The only way I could get the field was to
get it and the pm.

Don't "select" it. Use the range object.
Since you created it, you know which paragraph it is, right?

Set the range to the paragraph with the field, move the end by -1, cut,
delete the ¶.

Does that work?
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

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

Greg

JGM,

Yes it is the last paragraph. This seems to work and looks a little
better to me at least.

Sub InsertNestedFieldOnTheFly()

Dim oRng As Range
Dim myString As String

'Strip extention from file name and path
myString = ActiveDocument.FullName
myString = Left(myString, Len(myString) - 4)

Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True

'Insert dummy para at end of document
ActiveDocument.Range.InsertAfter vbCr
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
oRng.Select

'Insert nested field
With Selection
.Font.Size = 8
.Font.Color = wdColorGray90
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="IF"
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="PAGE"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=" = "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="NUMPAGES"
.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdMove
.TypeText Text:=Chr(34) & myString & Chr(34)
End With

'Cut field, delete dummy para mark, and paste field into header
Set oRng = ActiveDocument.Paragraphs.Last.Range
oRng.MoveEnd Unit:=wdCharacter, Count:=-1
oRng.Cut
ActiveDocument.Paragraphs.Last.Range.Delete

Set oRng = ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).Range
oRng.Collapse wdCollapseEnd
oRng.Paste
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True

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