none
Code to insert or append and hide Usernames and Date/Time stamps requested RRS feed

  • Question

  • Hi Programmers,

    I'm a teacher and would like to have students start using a document with code that did the following:

    1 - place the insertion point after whatever the last text character is and then hard return.

    2 - insert the username and date/time stamp, and if a username and date/time stamp is already present, just append it to the end of the stamps (for a history of edits/saves).

    3 - format the stamps so that student users without VBA knowledge cannot see them (not even with the little squiggly spelling and grammar check lines if possible).

    4 - format the stamps so that student users without VBA knowledge cannot overwrite or tamper with them.

    5 - make a button (in my Word 2010) that calls the methods so that when I open student docs, I can just click to turn on/off the stamps' visibility.

    Is this possible??? Thanks a bunch for any assistance with this.

    Below is starter code that inserts the username and date/time stamp after bookmarks. However, this can all be edited by the user and is not invisible.


    Microsoft Word Objects:

    Option Explicit

    Private Sub Document_Open()
    Call Register_Event_Handler
    End Sub


    Module 1:

    Option Explicit

    Dim X As New Class1
    Public Sub Register_Event_Handler()
        Set X.App = Word.Application
    End Sub


    Module 2:

    Option Explicit

    Public Sub Check_Completed()
    If ActiveDocument.Bookmarks.Exists("TimeStamp") = True Then
        If ActiveDocument.Bookmarks("TimeStamp").Empty = True Then _
        ActiveDocument.Bookmarks("TimeStamp").Range.Text = Now
    End If
    If ActiveDocument.Bookmarks.Exists("Name") = True Then
        If ActiveDocument.Bookmarks("Name").Empty = True Then _
        ActiveDocument.Bookmarks("Name").Range.Text = Environ("username")
    End If
    End Sub


    Class Module:

    Option Explicit

    Public WithEvents App As Word.Application

    Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    Call Check_Completed
    End Sub

    Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    Call Check_Completed
    End Sub

    Wednesday, May 29, 2013 11:52 AM

Answers

  • Try the following:

    Code for an ordinary module in the template:

    Option Explicit
    Dim wdAppClass As New ThisApplication

    Public Sub AutoExec()
    Set wdAppClass.wdApp = Word.Application
    End Sub

    Code for the 'ThisDocument' module in the template:

    Private Sub Document_New()
    On Error Resume Next
    Call Document_Open
    End Sub

    Private Sub Document_Open()
    Call TrackMe
    End Sub

    Sub TrackMe(Optional Hidden As Boolean)
      ActiveDocument.TrackRevisions = True
      With Options
        .InsertedTextMark = wdInsertedTextMarkNone
        .InsertedTextColor = wdByAuthor
        .DeletedTextMark = wdDeletedTextMarkNone
        .DeletedTextColor = wdByAuthor
        .RevisedPropertiesMark = wdRevisedPropertiesMarkNone
        .RevisedPropertiesColor = wdByAuthor
        .RevisedLinesMark = wdRevisedLinesMarkNone
        .RevisedLinesColor = wdAuto
        .CommentsColor = wdByAuthor
        .RevisionsBalloonPrintOrientation = wdBalloonPrintOrientationPreserve
      End With
      With ActiveWindow.View
        .ShowRevisionsAndComments = False
        .RevisionsView = wdRevisionsViewFinal
        .RevisionsMode = wdInLineRevisions
      End With
    End Sub

    Code for the 'ThisApplication' class module in the template:

    Public WithEvents wdApp As Word.Application

    Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
    Application.ScreenUpdating = False
    With ActiveDocument
      If .TrackRevisions = False Then
        Call TrackMe
      End If
    End With
    Application.ScreenUpdating = True
    End Sub

    To see the editing details, simply change the 'view' from 'Final' to 'Final showing markup'. You can also change the addition/deletion colouring etc for greater clarity.

    The only limitation I see at the moment is that, if the user manages to turn off change tracking and make a change without changing the selection, that particular change won't be tracked.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Marked as answer by honorcode Wednesday, May 29, 2013 4:36 PM
    • Edited by macropodMVP Wednesday, May 29, 2013 11:46 PM Added missing declaration
    Wednesday, May 29, 2013 2:24 PM

All replies

  • What you're asking for with 3 & 4 isn't possible. Indeed, hidden content in a document is prone to accidental deletion.

    A suggested alternative: Use a Document_Open macro to enforce the use of change-tracking, with the text formatted to look 'normal', and with the document display set to 'Final'. You'll need to couple that with a WindowSelectionChange macro - just in case the user disables change-tracking, so you can turn it on again. With such a setup, every edit will be time-stamped, with the user's name. Of course, to implement any macro-based solution effectively, you'll need to either store the documents in a trusted location (recommended) or set the macro security level to its minimum (not recommended), so the use isn't given the option to disable macros.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 29, 2013 12:16 PM
  • Thanks Paul,

    Understood about storage and use. For the code, I've searched the Internet and haven't found many examples.Would you be able to provide code for the Open and WindowSelectionChange events? Most of the code I obtained above is not mine and am not a wiz at VBA. Here are a couple of sites that pertain:

    http://social.technet.microsoft.com/Forums/en-US/word/thread/3f1e4529-386c-45e9-b338-3fe7d674fd01/

    http://jyeee.blogspot.com/2006/10/turn-offstopdisable-final-showing.html. Jason Yee's code is as follows:

    With ActiveWindow.View

    .ShowRevisionsAndComments = False
    .RevisionsView = wdRevisionsViewFinal
    End With

    Does it matter if this is in Document open or Activewindow?

    Thanks

    Wednesday, May 29, 2013 1:26 PM
  • Try the following:

    Code for an ordinary module in the template:

    Option Explicit
    Dim wdAppClass As New ThisApplication

    Public Sub AutoExec()
    Set wdAppClass.wdApp = Word.Application
    End Sub

    Code for the 'ThisDocument' module in the template:

    Private Sub Document_New()
    On Error Resume Next
    Call Document_Open
    End Sub

    Private Sub Document_Open()
    Call TrackMe
    End Sub

    Sub TrackMe(Optional Hidden As Boolean)
      ActiveDocument.TrackRevisions = True
      With Options
        .InsertedTextMark = wdInsertedTextMarkNone
        .InsertedTextColor = wdByAuthor
        .DeletedTextMark = wdDeletedTextMarkNone
        .DeletedTextColor = wdByAuthor
        .RevisedPropertiesMark = wdRevisedPropertiesMarkNone
        .RevisedPropertiesColor = wdByAuthor
        .RevisedLinesMark = wdRevisedLinesMarkNone
        .RevisedLinesColor = wdAuto
        .CommentsColor = wdByAuthor
        .RevisionsBalloonPrintOrientation = wdBalloonPrintOrientationPreserve
      End With
      With ActiveWindow.View
        .ShowRevisionsAndComments = False
        .RevisionsView = wdRevisionsViewFinal
        .RevisionsMode = wdInLineRevisions
      End With
    End Sub

    Code for the 'ThisApplication' class module in the template:

    Public WithEvents wdApp As Word.Application

    Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
    Application.ScreenUpdating = False
    With ActiveDocument
      If .TrackRevisions = False Then
        Call TrackMe
      End If
    End With
    Application.ScreenUpdating = True
    End Sub

    To see the editing details, simply change the 'view' from 'Final' to 'Final showing markup'. You can also change the addition/deletion colouring etc for greater clarity.

    The only limitation I see at the moment is that, if the user manages to turn off change tracking and make a change without changing the selection, that particular change won't be tracked.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Marked as answer by honorcode Wednesday, May 29, 2013 4:36 PM
    • Edited by macropodMVP Wednesday, May 29, 2013 11:46 PM Added missing declaration
    Wednesday, May 29, 2013 2:24 PM
  • Thanks Paul,

    I'm getting a compile error: Method or data member not found on this:

    Public Sub AutoExec()
    Set wdAppClass.wdApp = Word.Application  'at the wdApp
    End Sub

    Also, does anything go in the default Microsoft Word Objects ThisDocument object, or just have Module1, ThisDocument module, and ThisApplication module?

    Lastly, does this tracking actually store the number of saves, deletions, etc.? Reason I ask is that I've made numerous revisions a trial doc and the Reviewing Pane is only showing 1.

    Thanks for your help.

    Wednesday, May 29, 2013 2:47 PM
  • Please read and follow the instructions. Your error message suggests you haven't inserted the 'Dim wdAppClass As New ThisApplication' line before the sub.

    The tracking doesn't make a count - it stores the actual changes. Typing/deleting a whole paragraph counts as one change. You can use a macro to get counts by user date/time, kind of change.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 29, 2013 10:52 PM
  • You need something like

    Public WithEvents myApp As Word.Application

    at the top of the ThisApplication class module.


    Peter Jamieson

    Wednesday, May 29, 2013 11:26 PM
  • Oops - missed that in posting. Thanks. I had in my development code, but didn't post it. Previous post now fixed!

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, May 29, 2013 11:46 PM
    Wednesday, May 29, 2013 11:45 PM