Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2022, 04:33 AM
Mitzilein Mitzilein is offline Add time stamp by clicking on button Windows 10 Add time stamp by clicking on button Office 2021
Novice
Add time stamp by clicking on button
 
Join Date: Mar 2022
Posts: 1
Mitzilein is on a distinguished road
Default Add time stamp by clicking on button


Hi,
I've designed a word form and the user at the end needs to confirm that it's all been completed accurately and to the best of their knowledge. To do this, I would like them to tick a checkbox which then adds a time stamp and their username after the confirmation statement.
My problem is, that my current code doesn't work as the 'selection' would then be the checkbox - how to I get the macro to add the time stamp and user name into the next paragraph? tried it with selection.movedown but no success. Here's my current code:

Private Sub CheckBox1_Click()
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.InsertDateTime DateTimeFormat:="dd/MM/yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUK, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
Selection.TypeText Text:=", "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"USERNAME ", PreserveFormatting:=True
End Sub
Reply With Quote
  #2  
Old 03-14-2022, 05:47 AM
gmayor's Avatar
gmayor gmayor is offline Add time stamp by clicking on button Windows 10 Add time stamp by clicking on button Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try the following
Code:
Private Sub CheckBox1_Click()
Dim oRng As Range
Dim oFld As Field
    If CheckBox1.Value = True Then
        Set oRng = Selection.Range
        oRng.End = oRng.End + 1
        oRng.Collapse 0
        oRng.Text = Format(Date, "dd/MM/yyyy ") & Format(Time, "hh:mm")
        oRng.Collapse 0
        oRng.Text = ", "
        oRng.Collapse 0
        Set oFld = oRng.Fields.Add(Range:=oRng, Type:=wdFieldUserName, PreserveFormatting:=False)
        oRng.End = oFld.Result.End
        oRng.Collapse 0
        oRng.Text = vbCr
        oRng.Collapse 0
    End If
 End Sub
Personally I would use a content control to hold the time stamp thus
Code:
Private Sub CheckBox1_Click()
Dim oRng As Range
Dim bCC As Boolean
Dim oCC As ContentControl
Dim sTime As String
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = "Completed" Then bCC = True
        Exit For
    Next oCC
    If CheckBox1.Value = True Then
        sTime = Format(Date, "dd/MM/yyyy ") & Format(Time, "hh:mm") & ", " & Application.UserName
        If bCC = True Then
            oCC.LockContents = False
            oCC.Range.Text = sTime
            oCC.LockContents = True
        Else
            Set oRng = Selection.Range
            oRng.End = oRng.End + 1
            oRng.Collapse 0
            Set oCC = oRng.ContentControls.Add
            With oCC
                .Type = wdContentControlRichText
                .Title = "Completed"
                .Tag = .Title
                .Range.Text = sTime
                .LockContentControl = True
                .LockContents = True
            End With
            Set oRng = oCC.Range
            oRng.End = oRng.End + 1
            oRng.Collapse 0
            oRng.InsertParagraph
        End If
    Else
        If bCC = True Then
            oCC.LockContents = False
            oCC.Range.Text = ChrW(8203)
            oCC.LockContents = True
        End If
    End If
    Set oCC = Nothing
    Set oRng = Nothing
End Sub
This will toggle the time depending on the value of the checkbox. Frankly I wouldn't use an activeX checkbox either, but let's not go there now.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add time stamp by clicking on button Eliminate time stamp in Track Changes Aden Word 19 07-10-2023 09:02 PM
Email Time stamp gazmoz17 Outlook 0 07-15-2021 01:08 AM
Time Stamp to seconds RRB Excel Programming 1 06-15-2021 03:07 AM
Time Stamp Incorrect ashippen Outlook 0 06-27-2016 12:04 PM
Time Stamp for Categories? mlef Outlook 0 05-18-2016 09:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft