MrExcel Message Board

Excel 2007 for Scientists and Engineers


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Notices

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2002, 08:13 AM   #1
matthewh
New Member
 
Join Date: Mar 2002
Posts: 34
Default

Is there a way I can get the name of the user logged onto Windows to use in Excel VBA to name files, put the username into macros and formulas, etc. I have many uses for this kind of functionality in Excel and (if not too off topic) Access as well. I'm sure I've seen this elsewhere before, but can't remember where or how. We use Office 2000 on Win98 and 2000 workstations.

Thanks
matthewh is offline   Reply With Quote
Old Mar 14th, 2002, 08:19 AM   #2
Guest
 
Posts: n/a
Default

Application.UserName
  Reply With Quote
Old Mar 14th, 2002, 08:19 AM   #3
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Try this function GetUserName()

Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function
Tommy Bak is offline   Reply With Quote
Old Mar 14th, 2002, 08:19 AM   #4
Al Chara
MrExcel MVP
 
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
Default

The following code returns the username of excel not windows:
Application.UserName

I hope this helps.
Kind regards,
Al
Al Chara is offline   Reply With Quote
Old Mar 14th, 2002, 09:07 AM   #5
Guest
 
Posts: n/a
Default

Application.UserName
  Reply With Quote
Old Mar 14th, 2002, 09:38 AM   #6
pilot
Board Regular
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 305
Default

On Excel running over a network, I had occasion to use this where UserName returned useless info like the company name.

Sh.[A1] = UserLoggedIn
pilot is offline   Reply With Quote
Old Aug 31st, 2007, 10:59 AM   #7
MichaelMCSE
Board Regular
 
Join Date: Jul 2002
Location: Charlotte, NC
Posts: 417
Default

Quote:
Try this function GetUserName()

Option Explicit
' This is used by GetUserName() to find the current user's
' name from the API
Declare Function Get_User_Name Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long
Function GetUserName() As String
Dim lpBuff As String * 25

Get_User_Name lpBuff, 25
GetUserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
End Function
Where do you put this?

If we have a button on a worksheet that presents a msgbox with the username where do I put the above code?

Thank you,
Michael
MichaelMCSE is offline   Reply With Quote
Old Aug 31st, 2007, 11:07 AM   #8
lenze
MrExcel MVP
 
lenze's Avatar
 
Join Date: Feb 2002
Location: Helena, MT
Posts: 13,690
Default

The code to return the log-on name is
Code:
Environ("UserName")
Code:
Application.UserName
returns the application name set under Tools>Options

lenze
__________________
If you have to tell your boss you're good with Excel, you're NOT!!
All I know about Excel I owe to my ignorance!
Scotch: Because you don't solve great Excel problems over white wine
lenze is offline   Reply With Quote
Old Aug 31st, 2007, 11:14 AM   #9
MichaelMCSE
Board Regular
 
Join Date: Jul 2002
Location: Charlotte, NC
Posts: 417
Default

I like simple. Thank you.
MichaelMCSE is offline   Reply With Quote
Old Nov 9th, 2007, 05:23 AM   #10
Grizlore
Board Regular
 
Grizlore's Avatar
 
Join Date: Aug 2006
Location: Rose of the Shires
Posts: 215
Default

I found this solution worked well, as Environ("Username") just returned the Users login initials, and not the Users actual name.

Code:
Option Explicit 
  
Sub Users_Fullname() 
'originally coded as VB script by A.Vials, converted to VBA by Sly 
Dim objInfo 
Dim strLDAP 
Dim strFullName 
  
Set objInfo = CreateObject("ADSystemInfo") 
strLDAP = objInfo.UserName 
Set objInfo = Nothing 
strFullName = GetUserName(strLDAP) 
  
MsgBox "Full name of User is " & strFullName  'step to test 
  
End Sub 

Function GetUserName(strLDAP) 
  Dim objUser 
  Dim strName 
  Dim arrLDAP 
  Dim intIdx 
  
  On Error Resume Next 
  strName = "" 
  Set objUser = GetObject("LDAP://" & strLDAP) 
  If Err.Number = 0 Then 
    strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn") 
  End If 
  If Err.Number <> 0 Then 
    arrLDAP = Split(strLDAP, ",") 
    For intIdx = 0 To UBound(arrLDAP) 
      If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then 
        strName = Trim(Mid(arrLDAP(intIdx), 4)) 
      End If 
    Next 
  End If 
  Set objUser = Nothing 
  
  GetUserName = strName 
  
End Function
__________________
GriZlore


Statistics are like a lamp-post to a drunken man - more for leaning on than illumination.
Grizlore is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 08:06 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes