PDA

View Full Version : Check box help



hdegraw
09-11-2013, 10:46 AM
I am new to the site and first time user of vba. The code I have below inserts the Windows User Name in one column of a table and the date and time in the next column when a check box is clicked. I would like to add vba programming that would take that name and date of the document when the check box is unclicked. Currently you can unclick the box and it updates the time and that is it. Thank you for your help.


Option Explicit

'api call for obtaining the username
Private Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
nSize As Long)


Public Function WindowsUserName() As String
' ---------------------------------------------
'Function to extract the name:
' ---------------------------------------------
Dim szBuffer As String * 100
Dim lBufferLen As Long
lBufferLen = 100
If CBool(GetUserName(szBuffer, lBufferLen)) Then
WindowsUserName = Left$(szBuffer, lBufferLen - 1)
Else
WindowsUserName = CStr(Empty)
End If
End Function


Private Sub CheckBox1_Click()
Selection.MoveRight Unit:=wdCell Selection.TypeText Text:=WindowsUserName
Selection.MoveRight Unit:=wdCell Selection.TypeText Text:=Now()
End Sub

fumei
09-11-2013, 06:17 PM
that would take that name and date of the document when the check box is unclicked.

Take it where? Do you mean delete it?

This is a three coumn table. The checkbox in the first column, the username in the second, date and time in the third?

fumei
09-11-2013, 06:20 PM
BTW, you can also get the windows login username with simply:


Environ("username")

fumei
09-11-2013, 06:51 PM
If you need the fully qualified name....

Environ("userdomain") & "\" & Environ("username")