2015-06-02 80 views

回答

1

或者很簡單:

MsgBox Environ$("USERPROFILE") 

您可以使用VBA的Environ()方法,擴大環境變量,只是傳遞參數作爲字符串並放下%標記。

0

我用這個函數:

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

' Return the user's profile path. 
Private Function ProfilePath() As String 
Dim win_dir As String 
Dim user_name As String 
Dim ret_len As Long 

    ' Get the windows directory. 
    win_dir = Space$(256) 
    ret_len = GetWindowsDirectory(win_dir, Len(win_dir)) 
    win_dir = Left$(win_dir, ret_len) 

    ' Get the user's name. 
    user_name = Space$(256) 
    GetUserName user_name, ret_len 
    user_name = Left$(user_name, ret_len) 

    If (Asc(Right$(user_name, 1)) = 0) Then 
     user_name = Left$(user_name, Len(user_name) - 1) 
    End If 

    ProfilePath = win_dir & "\" & user_name 
End Function 
+0

我很困惑。如果你有解決方案,爲什麼這是一個問題?或者這個代碼是問題的一部分? – PaulFrancis

+0

當然,我只是好奇,因爲問題是8分鐘前,並在8分鐘前回答。好。你也可以簡單地使用Environ(「UserProfile」) – PaulFrancis

相關問題