Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default GetUserName help

I am a complete newbie to VBA. I want to use the advapi.dll call below
method below to compare the network username to restrict access to the
workbook: If it is possible, I would rather use the API than the
"Environ" for security reasons. Is it possible to do this?

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

Function UserNameWindows() As String

Dim lngLen As Long
Dim strBuffer As String

Const dhcMaxUserName = 255

strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function

TO COMBINE WITH THE BELOW NOT USING THE ENVIRON METHOD.

Private Sub Workbook_Open()
Application.ScreenUpdating = False
FrontpageFirst
x = Environ("username")
On Error Resume Next
Select Case x
Case "pmason"
Worksheets("Summary").Visible = True
Worksheets("Key to Abbrvs").Visible = True
Worksheets("Open Positions").Visible = True
Worksheets("Filled Positions").Visible = True
Worksheets("Temp to Hires").Visible = True
Case Else
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
End Select
Worksheets(2).Activate
Worksheets(1).Visible = xlVeryHidden
Application.ScreenUpdating = True
End Sub

Sub FrontpageFirst()
Dim page As String
page = "Unauthorized"
On Error Resume Next
Worksheets(page).Visible = True
If Worksheets(1).Name = page Then
Worksheets(1).Activate
Else
Worksheets(page).Activate
If Err = 0 Then
ActiveSheet.Move befo=Worksheets(1)
Else
Worksheets.Add befo=Worksheets(1)
Worksheets(1).Name = page
End If
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
FrontpageFirst
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
Application.DisplayAlerts = True
ActiveWorkbook.Save
Application.DisplayAlerts = False
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default GetUserName help

Just change the

x = Environ("UserName")

to

x = UserNameWindows


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Chris" wrote in message
oups.com...
I am a complete newbie to VBA. I want to use the advapi.dll call below
method below to compare the network username to restrict access to the
workbook: If it is possible, I would rather use the API than the
"Environ" for security reasons. Is it possible to do this?

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

Function UserNameWindows() As String

Dim lngLen As Long
Dim strBuffer As String

Const dhcMaxUserName = 255

strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function

TO COMBINE WITH THE BELOW NOT USING THE ENVIRON METHOD.

Private Sub Workbook_Open()
Application.ScreenUpdating = False
FrontpageFirst
x = Environ("username")
On Error Resume Next
Select Case x
Case "pmason"
Worksheets("Summary").Visible = True
Worksheets("Key to Abbrvs").Visible = True
Worksheets("Open Positions").Visible = True
Worksheets("Filled Positions").Visible = True
Worksheets("Temp to Hires").Visible = True
Case Else
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
End Select
Worksheets(2).Activate
Worksheets(1).Visible = xlVeryHidden
Application.ScreenUpdating = True
End Sub

Sub FrontpageFirst()
Dim page As String
page = "Unauthorized"
On Error Resume Next
Worksheets(page).Visible = True
If Worksheets(1).Name = page Then
Worksheets(1).Activate
Else
Worksheets(page).Activate
If Err = 0 Then
ActiveSheet.Move befo=Worksheets(1)
Else
Worksheets.Add befo=Worksheets(1)
Worksheets(1).Name = page
End If
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
FrontpageFirst
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
Application.DisplayAlerts = True
ActiveWorkbook.Save
Application.DisplayAlerts = False
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default GetUserName help

Thanks!


Bob Phillips wrote:
Just change the

x = Environ("UserName")

to

x = UserNameWindows


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Chris" wrote in message
oups.com...
I am a complete newbie to VBA. I want to use the advapi.dll call below
method below to compare the network username to restrict access to the
workbook: If it is possible, I would rather use the API than the
"Environ" for security reasons. Is it possible to do this?

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

Function UserNameWindows() As String

Dim lngLen As Long
Dim strBuffer As String

Const dhcMaxUserName = 255

strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function

TO COMBINE WITH THE BELOW NOT USING THE ENVIRON METHOD.

Private Sub Workbook_Open()
Application.ScreenUpdating = False
FrontpageFirst
x = Environ("username")
On Error Resume Next
Select Case x
Case "pmason"
Worksheets("Summary").Visible = True
Worksheets("Key to Abbrvs").Visible = True
Worksheets("Open Positions").Visible = True
Worksheets("Filled Positions").Visible = True
Worksheets("Temp to Hires").Visible = True
Case Else
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
End Select
Worksheets(2).Activate
Worksheets(1).Visible = xlVeryHidden
Application.ScreenUpdating = True
End Sub

Sub FrontpageFirst()
Dim page As String
page = "Unauthorized"
On Error Resume Next
Worksheets(page).Visible = True
If Worksheets(1).Name = page Then
Worksheets(1).Activate
Else
Worksheets(page).Activate
If Err = 0 Then
ActiveSheet.Move befo=Worksheets(1)
Else
Worksheets.Add befo=Worksheets(1)
Worksheets(1).Name = page
End If
End If
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
FrontpageFirst
For i = 2 To Worksheets.Count
Worksheets(i).Visible = xlVeryHidden
Next
Application.DisplayAlerts = True
ActiveWorkbook.Save
Application.DisplayAlerts = False
End Sub


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



All times are GMT +1. The time now is 06:29 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"