A1VBCode Forums

Current User Location Error


http://www.a1vbcode.com/vbforums/Topic31669.aspx

By allgames71 - 10/12/2012

Hi ,

I write to get the current user name , and the result is :

A> In line 15 , the result is "Raymond\Favorites" , where Raymond is the current user name which locate at Drive D:\

     My question is : how can I get "D:\Raymond\Favorites" ???

B> In line 16 , the result is "Raymond\Documents" in Win7

     My question is : why is it not "Raymond\Favorites"

     I've try to use : FileName = sUserName & "\Videos" , but with the same result : "Raymond\Documents" ???

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

3. Private Sub Form_Load()
4. Dim sUserName As String
5. Dim lSize As Long

6. lSize = 20
7. sUserName = Space(20)
8. Call GetUserName(sUserName, lSize)
9. sUserName = Trim(sUserName)
10. sUserName = Left(sUserName, Len(sUserName) - 1)
11. '------------------------------------------------------------------------------
12. Dim Res
13. Dim FileName
14. FileName = sUserName & "\Favorites"
15. MsgBox FileName
16. Res = Shell("explorer.exe " & FileName, vbNormalFocus)

17. End Sub

Thanks for everybody.

By Mark - 10/17/2012

With your project open, click on the Project menu. Scroll down towards the bottom and select References from the menu. From the dialog the opens, place a check mark next to "Micorsoft Scripting Runtime" in the list and then click the OK button.