Environ("username") in 2007

D

Dan

debug.Print Environ("username") does work on 2003 but not on 2007?

I have read other posting with a similar question and it is written ther
"problem is solved by changing the environmental variables" - how do you do
that or any other suggestion?
Many thanks,
Dan
 
D

Dave Peterson

My guess is that it's more related to the version of windows (not excel) that
you're running.

You may want to share that to see if others running the same configuration will
comment.
 
D

Dan

The problem is on XP, but I tried on another XP I have and working fine on
it, so maybe my installation is wrong.
 
D

Dave Peterson

You could verify that the environment variable exists.

Windows start button|Run
type:
cmd
and hit enter.

Then type:
Set
and hit enter

Scroll up/down looking for USERNAME.

It works fine for me with WinXP Pro and xl2007.

========
But there are other ways to get the username, too:

Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
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 = ""
End If
End Function

Sub testme()
MsgBox fOSUserName 'to test to see if it worked.
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top