
VBA Code to Insert Path/Filename/Date/Time at end of document
Hi Jill,
Ok, here's some code that writes the fields:
--------------------------------------------------------
Set oRange = ActiveDocument.Content
'Get the end of the document
oRange.Collapse direction:=wdCollapseEnd
With oRange
'Insert paragraphmark,
.InsertParagraph
.Collapse direction:=wdCollapseEnd
'Insert path and file field
ActiveDocument.Fields.Add Range:=oRange, _
Type:=wdFieldEmpty, _
Text:="FILENAME \p", _
preserveformatting:=False
'Tab
.MoveEnd unit:=wdParagraph
.InsertAfter Chr(9)
.MoveStart unit:=wdParagraph
'Insert datefield
ActiveDocument.Fields.Add Range:=oRange, _
Type:=wdFieldEmpty, _
_
preserveformatting:=False
End With
Set oRange = Nothing
--------------------------------------------------------
Hope this helps,
regards,
Astrid
So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
Quote:
----- Original Message -----
Sent: Thursday, January 10, 2002 10:53 PM
Subject: RE: VBA Code to Insert Path/Filename/Date/Time at end of document
> Hi Astrid ...
> Thank you; this is very helpful. I also would like to be able to
accomplish
> this with fields. If you can point me in the direction of any code for
> fields, this would also be most useful.
> Thank you for taking the time to respond to my post. I appreciate your
> effort.
> ... Jill
> -----Original Message-----
> Sent: Thursday, January 10, 2002 4:21 PM
> To: Jill
> Subject: Re: VBA Code to Insert Path/Filename/Date/Time at end of document
> Hi Jill,
> Do you want this data as text or as fields that update whenever the
document
> is saved with a new name and with the current date and time each time the
> field is updated?
> If the former:
> --------------------------------------------------------
> Dim oRange As Range
> Dim sFileName As String
> Dim sDate As String
> Dim sTextToInsert As String
> Set oRange = ActiveDocument.Content
> 'Get the end of the document
> oRange.Collapse direction:=wdCollapseEnd
> 'Get the path and filename of the document
> sFileName = ActiveDocument.FullName
> 'Get date and time
> sDate = Format(Now, "MM dd yyyy hh:mm")
> 'Paragraph mark, sfilename, tab, sdate
> sTextToInsert = vbCr & sFileName & Chr(9) & sDate
> oRange.Text = sTextToInsert
> Set oRange = Nothing
> --------------------------------------------------------
> Hope this helps,
> regards,
> Astrid
> So that all can benefit from the discussion, please post all follow-ups to
> the newsgroup. Visit the MVP Word FAQ site at http://www.mvps.org/word/
> > Dave ...
> > Thank you so much.
> > Jill