PDA

View Full Version : Time Date Stamp That Will Not Update When a Document is Opened



Dysentery
11-21-2014, 01:35 PM
Hello all,

First of all, TOTAL visual basic noob here. I'm working on creating a form for employees to fill out in Word 2013. I need the form to time/date stamp itself when the user fills out the form. However, I do not want that time/date stamp to update every time someone else opens that document. In other words, if it is blank it needs to auto stamp the time and date. After that person using the form emails it to me, when I open it I want to see the time/date the form was filled out, not the date/time I open it.

This will be a protected document with many, many form fields for the user to tab through and fill out, then submit to me via email.

I had something similar set up on an excel spreadsheet, with this code:

Private Sub Worksheet_Change(ByVal Target As Range)

ActiveSheet.Unprotect ("password")
If Range("I5") = "" Then
Range("I5") = Now
End If
ActiveSheet.Protect ("password")
End Sub

It worked great. the time/date stamp would be added when someone began filling in the spreadsheet and would not update when I opened it.

Would this be the right approach for adding this functionality to a protected Word doc? I know next to nothing about code. Could anyone help?

macropod
11-21-2014, 02:19 PM
Your form should use a CREATEDATE field instead of an ordinary DATE field. A CREATEDATE field will only update when a document is created or if it is later re-saved via Save As. No code required.

Dysentery
11-24-2014, 04:27 PM
I'm sorry, I should have been more clear in my original post. I need that time/date to become a permanent part of that document. In other words, even if someone makes a change elsewhere in that document and re-saves it, the date/time the document was submitted needs to stay exactly the same.

macropod
11-24-2014, 04:50 PM
In that case, you need to both define where the date is to be inserted and insert it there in the required format. Word doesn't work with cell addresses and the like; rather, you need to think of the content as a stream of characters etc., which you can reference in various ways.

For example, to insert the date as the first item in the document, you might use code like:
ActiveDocument.Range.InsertBefore Format(Now,"DDDD, MMMM D, YYYY")
An alternative method would be to use a CREATEDATE field or DATE field, then run:
ActiveDocument.Fields(1).Unlink
For all that, though, there is nothing about what you've inserted that prevents a user changing it. Furthermore, your goal is easily defeated in the user doesn't allow your macro to run (e.g. by choice or because of how their system's macro security is set). And yes, you can protect whatever range you add the date to, but Word's protection model is vastly different to Excel's. There are multiple forms of protection that can be applied, and which you use depends on what you're trying to achieve with the document as a whole. You would need to provide more detail about that before specific advice could be given.