Try this. Note that the "Declare Function" code has to go at the top of the
code module or you'll get a compile error.' Makes sure all variables are
dimensioned in each subroutine.
Option Explicit
' Access the GetUserNameA function in advapi32.dll and
' call the function GetUserName.
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long ' Main routine
to Dimension variables, retrieve user name
' and display answer.
Sub Get_User_Name()
' Dimension variables
Dim lpBuff As String * 25
Dim ret As Long, UserName As String
' Get the user name minus any trailing spaces found in the name.
ret = GetUserName(lpBuff, 25)
UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
' Display the User Name
MsgBox UserName
End Sub
-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] Behalf Of Heaton, Timothy H.
(USD)
Sent: Saturday, September 22, 2007 11:22 PM
To: [email protected]
Subject: [Access VBA Central] Finding a Windows username in VBA
I wrote a home-made Common Dialog Box for VBA, but I'm wondering if
there's a way to grab the current Windows username in VBA. I want it for
a shortcut to My Documents, as in:
ChDir "C:\Documents and Settings\" & UserName & "\My Documents"
Any ideas?
-Tim
[Non-text portions of this message have been removed]