Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default win api function

Inside of a VBA / excel macro, is there a function I can
call to get the nt login of the current user?

Any help much appreciated.

Randy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default win api function

Hi Randy,

Here's a response I posted the other day to m.p.e.misc:


You can get the username from one of two places:

1) an environment variable:

MsgBox Environ$("Username")

2) the Windows API:

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

Public Function gsGetUsername() As String
Dim sName As String * 255
Dim nPos As Integer

GetUserName sName, 255
nPos = InStr(1, sName, vbNullChar)
If nPos Then gsGetUsername = Left$(sName, nPos - 1)
End Function

MsgBox gsGetUsername

I typically use #2, as I feel it is more reliable than #1. In your
Workbook_Open routine (double-click ThisWorkbook in the VBE to get to the
applicable codepane), you could check to see if a cell has a value - if not,
put the username the

Private Sub Workbook_Open()
If Len(Sheets("Sheet1").Range("A1").Value) = 0 Then
Sheets("Sheet1").Range("A1").Value = gsGetUsername
End If
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Randy wrote:
Inside of a VBA / excel macro, is there a function I can
call to get the nt login of the current user?

Any help much appreciated.

Randy


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
Excel Data Validation/Lookup function does function correcty Kirkey Excel Worksheet Functions 2 May 25th 09 09:22 PM
copy of excel file not showing formulal/function in the function b oaallam Excel Discussion (Misc queries) 4 September 6th 07 01:20 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
Emulate Index/Match combo function w/ VBA custom function Spencer Hutton Excel Worksheet Functions 2 May 2nd 05 05:26 PM
User-Defined Function pre-empting Built-in Function? How to undo???? MarWun Excel Programming 1 August 6th 03 09:31 PM


All times are GMT +1. The time now is 12:28 PM.

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"