如何在Windows 7中获取与%UserProfile%
结果相同的当前用户的路径?
答案 0 :(得分:1)
或者,非常简单:
MsgBox Environ$("USERPROFILE")
您可以使用VBA中的Environ()
方法扩展环境变量,只需将参数作为字符串传递并删除%
标记。
答案 1 :(得分: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