VBA fOSUserName 64 bit - 32 bit issue

Retroshift

Board Regular
Joined
Sep 20, 2016
Messages
119
Office Version
  1. 2019
Platform
  1. 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?

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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Instead of:
VBA Code:
#If Win64 = 1 And VBA7 = 1 Then

Try just:
VBA Code:
#If VBA7 Then
 
Upvote 0
Thanks but I already tried this and it unfortunately has the same result. (And I also want to avoid using the environ(username) because it is prone to spoofing.)
 
Upvote 0
Can you make an if statement on the bit version of Excel alone?
Because both Excel 2016 and Excel 2019 use VBA 7.1.
Both desktops have Windows 64 bit.
The only difference is that the Excel 2016 is 32 bit and the Excel 2019 is 64 bit.
 
Upvote 0
I tested your code on 64bit windows
32bit Office Excel 2007 - - - Works
32bit Office Excel 2016 - - - Works

Perhaps your Work environment is restricted in some way.
 
Upvote 0
I cannot figure out in what way the environment could be restricted if the regular declare function (without the if statement) does work fine. Any ideas?
 
Upvote 0
Okay, I think I made it work by adding an IF(NOW) function to the cell of the username (for updating).
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top