View Single Post
 
Old 03-14-2022, 05:47 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,142
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