Retroshift
Board Regular
- Joined
- Sep 20, 2016
- Messages
- 119
- Office Version
- 2019
- Platform
- Windows
Hi
The code below is meant to retrieve the username in a cell with the formula =fOSUserName()
The code works flawlessly on my private desktop with Excel 2019 64 bit.
However, the code does not seem to work on my company's desktop with Excel 2016 32 bit, even though the code works on this 32 bit desktop if I remove the if statement (so I merely write the "declare function").
I need the code to work on both 32 bit and 64 bit systems and I am wondering what the issue is?
The code below is meant to retrieve the username in a cell with the formula =fOSUserName()
The code works flawlessly on my private desktop with Excel 2019 64 bit.
However, the code does not seem to work on my company's desktop with Excel 2016 32 bit, even though the code works on this 32 bit desktop if I remove the if statement (so I merely write the "declare function").
I need the code to work on both 32 bit and 64 bit systems and I am wondering what the issue is?
VBA Code:
#If Win64 = 1 And VBA7 = 1 Then
Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
#Else
Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
#End If
Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function