#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 337
Default Username

I have the following code that works on some computer but not on others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Username

On the computers where this code does not work there is probably a needed
reference missing (to see what references have been added, select Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel 11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 337
Default Username

All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a needed
reference missing (to see what references have been added, select Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel 11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Username

Are the computers where you get this error Macs? The ENVIRON function is not
available on the Mac, and the API won't work on a Mac either.

You could also try retrieving Application.UserName, which is the name
entered on the Tools Options General tab.

Hutch

"Oldjay" wrote:

All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a needed
reference missing (to see what references have been added, select Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel 11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Username

The API example Tom posted should work fine for you in windows. Try pasting
it into a new module in a new project and try again. Earlier you said "
error Project or library not found", perhaps you have a missing reference
(Tools - references). If following works it would be highly indicative of a
missing ref.

MsgBox VBA.Interaction.Environ$("USERNAME")

If for you Environ("USERNAME") still doesn't work, ie it returns an empty
string but doesn't error, there's probably not much you can do about it. It
doesn't work for me either. However I get 10 returns if I run this -

Sub test()
Dim i As Long
On Error Resume Next
For i = 0 To 100
s = Environ(i)
If Len(s) Then
Debug.Print i, s
s = ""
End If
Next
End Sub

Regards,
Peter T


"Oldjay" wrote in message
...
All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a

needed
reference missing (to see what references have been added, select

Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the

missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel

11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on

others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 337
Default Username

I now get an error "Declare statrments not allowed as public membersof object
modules" when i try to run api

"Peter T" wrote:

The API example Tom posted should work fine for you in windows. Try pasting
it into a new module in a new project and try again. Earlier you said "
error Project or library not found", perhaps you have a missing reference
(Tools - references). If following works it would be highly indicative of a
missing ref.

MsgBox VBA.Interaction.Environ$("USERNAME")

If for you Environ("USERNAME") still doesn't work, ie it returns an empty
string but doesn't error, there's probably not much you can do about it. It
doesn't work for me either. However I get 10 returns if I run this -

Sub test()
Dim i As Long
On Error Resume Next
For i = 0 To 100
s = Environ(i)
If Len(s) Then
Debug.Print i, s
s = ""
End If
Next
End Sub

Regards,
Peter T


"Oldjay" wrote in message
...
All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a

needed
reference missing (to see what references have been added, select

Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the

missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel

11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on

others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Username

When trying code from this ng always place it in a normal module, unless
instructed otherwise or it's self evident from the code, eg it's an event

If for your own reasons the API declaration needs to go in say Thisworkbook,
sheet, userform or any other class module, declare it as Private

Private Declare Function GetUserName Lib etc

See Declare together with Public & Private in help

Regards,
Peter T


"Oldjay" wrote in message
...
I now get an error "Declare statrments not allowed as public membersof

object
modules" when i try to run api

"Peter T" wrote:

The API example Tom posted should work fine for you in windows. Try

pasting
it into a new module in a new project and try again. Earlier you said "
error Project or library not found", perhaps you have a missing

reference
(Tools - references). If following works it would be highly indicative

of a
missing ref.

MsgBox VBA.Interaction.Environ$("USERNAME")

If for you Environ("USERNAME") still doesn't work, ie it returns an

empty
string but doesn't error, there's probably not much you can do about it.

It
doesn't work for me either. However I get 10 returns if I run this -

Sub test()
Dim i As Long
On Error Resume Next
For i = 0 To 100
s = Environ(i)
If Len(s) Then
Debug.Print i, s
s = ""
End If
Next
End Sub

Regards,
Peter T


"Oldjay" wrote in message
...
All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a

needed
reference missing (to see what references have been added, select

Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the

missing
refernce is either 'Visual Basic for Applications' or 'Microsoft

Excel
11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"

_
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on

others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Username

Put the API declaration and function code in a VBA module (Insert Module
in the Visual Basic Editor). You can't put that code in the code module for a
userform or a worksheet or in the ThisWorkbook module.

Hutch

"Oldjay" wrote:

I now get an error "Declare statrments not allowed as public membersof object
modules" when i try to run api

"Peter T" wrote:

The API example Tom posted should work fine for you in windows. Try pasting
it into a new module in a new project and try again. Earlier you said "
error Project or library not found", perhaps you have a missing reference
(Tools - references). If following works it would be highly indicative of a
missing ref.

MsgBox VBA.Interaction.Environ$("USERNAME")

If for you Environ("USERNAME") still doesn't work, ie it returns an empty
string but doesn't error, there's probably not much you can do about it. It
doesn't work for me either. However I get 10 returns if I run this -

Sub test()
Dim i As Long
On Error Resume Next
For i = 0 To 100
s = Environ(i)
If Len(s) Then
Debug.Print i, s
s = ""
End If
Next
End Sub

Regards,
Peter T


"Oldjay" wrote in message
...
All computers have the same references and are running Excel 2003 SP3.
I tried the API and got the same error message at

GetUserName Buffer, BuffLen

"Tom Hutchins" wrote:

On the computers where this code does not work there is probably a

needed
reference missing (to see what references have been added, select

Refernces
from the Tools menu in the Excel Visual Basic Editor). I think the

missing
refernce is either 'Visual Basic for Applications' or 'Microsoft Excel

11.0
Object Library' (you may have a different version than 11.0).

Here is another way to retrieve the user's LAN ID using an API call:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

Sub AAAAA()
MsgBox UserName()
End Sub

Hope this helps,

Hutch

"Oldjay" wrote:

I have the following code that works on some computer but not on

others
It gives the error Project or library not found

Public Function UserName()

UserName = Environ("USERNAME")

End Function

Also tried UserName = Environ$("USERNAME")

What do I have to add?




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Username

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Username

For me,

?Environ(49) displays nothing
?Environ("username") displays my LAN ID

Hutch

"timmg" wrote:

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 337
Default Username

I can open a new workbook paste the code and get Lan ID
When I insert a new module in the existing workbook and paste the code I get
the same error. It's got to be something in this machine as the program works
with Environ("username") on other machines. This a 3.5M estimating program
with many menus and modules and more than 75 Subs.

Is there any other thing that I can look for?

"Tom Hutchins" wrote:

For me,

?Environ(49) displays nothing
?Environ("username") displays my LAN ID

Hutch

"timmg" wrote:

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Username

Clearly, there is no problem with the code. It must be an environmental
(system) problem, Excel has become corrupted, or there is a conflict with
another program.

First, I would double-check the references in the VBE on the computers that
won't run the Environ("username") statement. Maybe, for example, your
workbook was saved with a reference to the Excel 11.0 Object Library but
those machines only have the Excel 10.0 Object Library.

Second, I would check what add-ins, if any, are enabled on those machines.
Some add-ins (like Hyperion's SmartView) can alter or disable normal Excel
functions. Disable any add-ins not found on the machines that work, and see
if Environ("username") works then.

Hutch

"Oldjay" wrote:

I can open a new workbook paste the code and get Lan ID
When I insert a new module in the existing workbook and paste the code I get
the same error. It's got to be something in this machine as the program works
with Environ("username") on other machines. This a 3.5M estimating program
with many menus and modules and more than 75 Subs.

Is there any other thing that I can look for?

"Tom Hutchins" wrote:

For me,

?Environ(49) displays nothing
?Environ("username") displays my LAN ID

Hutch

"timmg" wrote:

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim


  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Username

I did try earlier to suggest the OP check for any missing references and how
to overcome (even how to get Environ to work with a missing ref). The OP's
stated error message was indeed indicative of a missing ref.

However, Environ("Username") does not work in all systems, it's flaky. It
doesn't work in mine, doesn't fail but merely returns an empty string.

The API is more reliable, subject not declaring it as Public in anything but
a normal module (which is what "Declare" defaults to when not preceded with
"Private").

Regards,
Peter T

"Tom Hutchins" wrote in message
...
Clearly, there is no problem with the code. It must be an environmental
(system) problem, Excel has become corrupted, or there is a conflict with
another program.

First, I would double-check the references in the VBE on the computers

that
won't run the Environ("username") statement. Maybe, for example, your
workbook was saved with a reference to the Excel 11.0 Object Library but
those machines only have the Excel 10.0 Object Library.

Second, I would check what add-ins, if any, are enabled on those machines.
Some add-ins (like Hyperion's SmartView) can alter or disable normal Excel
functions. Disable any add-ins not found on the machines that work, and

see
if Environ("username") works then.

Hutch

"Oldjay" wrote:

I can open a new workbook paste the code and get Lan ID
When I insert a new module in the existing workbook and paste the code I

get
the same error. It's got to be something in this machine as the program

works
with Environ("username") on other machines. This a 3.5M estimating

program
with many menus and modules and more than 75 Subs.

Is there any other thing that I can look for?

"Tom Hutchins" wrote:

For me,

?Environ(49) displays nothing
?Environ("username") displays my LAN ID

Hutch

"timmg" wrote:

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim




  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 337
Default Username

Thanks for all the help. I went ahead with the Application .username even
thou once in a while a user will use another computer and get the wrong name.

"Peter T" wrote:

I did try earlier to suggest the OP check for any missing references and how
to overcome (even how to get Environ to work with a missing ref). The OP's
stated error message was indeed indicative of a missing ref.

However, Environ("Username") does not work in all systems, it's flaky. It
doesn't work in mine, doesn't fail but merely returns an empty string.

The API is more reliable, subject not declaring it as Public in anything but
a normal module (which is what "Declare" defaults to when not preceded with
"Private").

Regards,
Peter T

"Tom Hutchins" wrote in message
...
Clearly, there is no problem with the code. It must be an environmental
(system) problem, Excel has become corrupted, or there is a conflict with
another program.

First, I would double-check the references in the VBE on the computers

that
won't run the Environ("username") statement. Maybe, for example, your
workbook was saved with a reference to the Excel 11.0 Object Library but
those machines only have the Excel 10.0 Object Library.

Second, I would check what add-ins, if any, are enabled on those machines.
Some add-ins (like Hyperion's SmartView) can alter or disable normal Excel
functions. Disable any add-ins not found on the machines that work, and

see
if Environ("username") works then.

Hutch

"Oldjay" wrote:

I can open a new workbook paste the code and get Lan ID
When I insert a new module in the existing workbook and paste the code I

get
the same error. It's got to be something in this machine as the program

works
with Environ("username") on other machines. This a 3.5M estimating

program
with many menus and modules and more than 75 Subs.

Is there any other thing that I can look for?

"Tom Hutchins" wrote:

For me,

?Environ(49) displays nothing
?Environ("username") displays my LAN ID

Hutch

"timmg" wrote:

What does

?Environ(49)

or

?Environ("username")

display when entered directly in the immediate window? (ctrl + G)

tim





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
(username v1) JJernigan Excel Discussion (Misc queries) 0 June 12th 08 09:13 PM
username robzrob Excel Worksheet Functions 2 May 4th 08 05:59 PM
userName Ronbo Excel Programming 6 September 26th 05 03:19 PM
UserName rjamison Excel Programming 0 June 14th 05 12:14 AM
UserName Myriam Excel Programming 12 April 20th 05 08:18 PM


All times are GMT +1. The time now is 07:56 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"