Oh by the way. The first question you have is also because you use a click sub to set the username. Put it into the form initializing sub.
As another note, I like to declare external procedures explicitly instead of or in addition to referencing them. That way you know where they are coming from. In your code, the WNetGetUser function comes from out of town, so I would:
1. Declare where it's coming from (see below)
2. Use it in a separate function (also see below)
Alternatively to WNetGetUser, you can use the WScript object in Windows. It can be used for some good information. For example, the WScript.NetWork keeps the username. In the sub below, I've added both WNetGetUser and WScript.
'Declare for WNetGetUser:
Private Declare Function WNetGetUser Lib "Mpr.dll" _
Alias "WNetGetUserA" _
(ByVal lpName As String, _
ByVal lpUserName As String, _
lpnLength As Long) As Long
'Function to find userName with WNetGetUser:
Private Function getUserName() As String
Const NoError = 0 'The Function call was successful
' Buffer size for the return string.
Const lpnLength As Integer = 255
' Get return buffer space.
Dim status As Integer
' For getting user information.
Dim lpName, lpUserName As String
' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)
' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
' An error occurred.
lpUserName = ""
End
End If
getUserName = lpUserName
End Function
'Sample Initialize sub for Form:
Private Sub UserForm_Initialize()
Dim AllBlocks As Object
Dim objBlk As Object
Dim AllLayers As Object
Dim Layer As Object
Dim WScriptObj As Object
'declare local variables
'just in case you didn't set the label field
'to empty by default
Label6.Caption = ""
On Error GoTo noWScriptObject
Set WScriptObj = CreateObject("WScript.Network")
Label6.Caption = WScriptObj.Username
'additional info from the WScript.Network object could
'e.g. be (don't go overboard in needless info, though):
'Label7.Caption = WScriptObj.ComputerName
'Label8.Caption = WScriptObj.UserDomain
noWScriptObject:
'here you would call getUserName if you don't
'want to use or have troubles using the WScript
'object
'Label6.Caption = getUserName
Set AllLayers = ThisDrawing.Layers
'get the layers from the layers collection
For Each Layer In AllLayers
'for every layer listed
ComboBox1.AddItem Layer.Name
'add the layer name to the list box
Next
Set AllBlocks = ThisDrawing.Blocks
For Each objBlk In AllBlocks
'we don't want layout blocks!
If Not objBlk.IsLayout Then
ComboBox2.AddItem objBlk.Name
End If
Next
End Sub
*still hating this stoopid language*