VBA Code to Insert Path/Filename/Date/Time at end of document 
Author Message
 VBA Code to Insert Path/Filename/Date/Time at end of document

Hi Jill,

You can get something like
C:\Test.doc
1/9/02
4:12:03 PM

by using the following:

Dim oRng As Range
Dim s As String
Set oRng = ActiveDocument.Range _
    (Start:=ActiveDocument.Range.End - 1, _
    End:=ActiveDocument.Range.End - 1)
s = vbCrLf & ActiveDocument.FullName & vbCrLf
s = s & Date & vbCrLf
s = s & Time & vbCrLf
oRng.InsertAfter s

HTH


Quote:
> Does anybody know if there is any VBA code available out
> there to insert the path/filename, date, time on the last
> page only of a document?

> Thanks in advance.



Mon, 28 Jun 2004 05:12:40 GMT  
 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/



Quote:
> Dave ...

> Thank you so much.

> Jill



Tue, 29 Jun 2004 05:21:08 GMT  
 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



Tue, 29 Jun 2004 08:04:40 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. VBA code to print all icon-inserted documents within a document

2. Insert Date&Time into Filename

3. Insert Date&Time into Filename

4. start date/time, end date/time problem

5. code to insert date and path

6. Need Code that will Insert Date and Time

7. help inserting original filename and path

8. Inserting a paragraph at end of document?

9. I need Path & Filename validation code

10. getting just the filename, not the filename path from the opendialog control

11. Date/time in filename

12. Compare date in form with date/time in form with date/time in database

 

 
Powered by phpBB® Forum Software