Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

FileName on Last Page of Word Doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karen Clark - 06 Apr 2005 23:05 GMT
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
Jean-Guy Marcil - 07 Apr 2005 03:48 GMT
Karen Clark was telling us:
Karen Clark nous racontait que :

> Hi,
>
[quoted text clipped - 11 lines]
> 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.
Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg - 07 Apr 2005 12:07 GMT
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
Jean-Guy Marcil - 07 Apr 2005 13:48 GMT
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.
Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg - 07 Apr 2005 13:56 GMT
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.
Greg - 07 Apr 2005 14:36 GMT
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
Jean-Guy Marcil - 07 Apr 2005 14:47 GMT
Greg was telling us:
Greg nous racontait que :

> IVO JGMs post then maybe:
>
[quoted text clipped - 6 lines]
> 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
?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg - 07 Apr 2005 14:54 GMT
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?
Jean-Guy Marcil - 07 Apr 2005 19:45 GMT
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?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 07 Apr 2005 22:13 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg was telling us:
> Greg nous racontait que :
[quoted text clipped - 16 lines]
> Select the field without the ?, cut it, then delete the ?. Would that
> work?
Jean-Guy Marcil - 08 Apr 2005 00:21 GMT
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
[quoted text clipped - 22 lines]
>> Select the field without the ?, cut it, then delete the ?. Would that
>> work?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg - 08 Apr 2005 12:33 GMT
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
Charles Kenyon - 07 Apr 2005 21:08 GMT
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.
Signature

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.

> Hi,
>
[quoted text clipped - 15 lines]
> Karen
> VB novice
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.