Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Windows 10升级到64位时询问PtrSafe属性时出现问题?_Vba - Fatal编程技术网

Vba Windows 10升级到64位时询问PtrSafe属性时出现问题?

Vba Windows 10升级到64位时询问PtrSafe属性时出现问题?,vba,Vba,我对以下声明有疑问,如下所示: Private声明函数WNetGetUser Lib“mpr.dll”别名“WNetGetUserA”(ByVal lpName为字符串,ByVal lpUserName为字符串,lpnLength为长)为长 错误要求使用PtrSafe属性。请尝试此声明: Private Declare PtrSafe Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _

我对以下声明有疑问,如下所示:

Private声明函数WNetGetUser Lib“mpr.dll”别名“WNetGetUserA”(ByVal lpName为字符串,ByVal lpUserName为字符串,lpnLength为长)为长

错误要求使用
PtrSafe
属性。

请尝试此声明:

Private Declare PtrSafe Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
          (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long
并使用下一个函数检索记录的用户名:

Function GetUserName() As String
 Const lpnLength As Long = 255
 Dim status As Long, lpName, lpUserName As String

 'Assign the buffer size constant to lpUserName.
 lpUserName = Space$(lpnLength + 1)

 status = WNetGetUser(lpName, lpUserName, lpnLength)

 'See whether error occurred.
 If status = 0 Then
    lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
 End If

 GetUserName = lpUserName
End Function
可以称之为:

Sub TestUserName()
    Debug.Print GetUserName
End Sub
所有这些,只要你坚持使用API

但VBA有其简单的获取方法:

Debug.Print Application.UserName
如果您不喜欢,使用VBScript(在VBA中)可能会有帮助:

Sub testUserNameVBSCript()
 Dim userName As String
   userName = CreateObject("WScript.Network").userName
   Debug.Print userName
End Sub

谢谢你发现我的打字错误-我现在发现了不起作用的语句,包括PtrSafe,它也起作用了。