VBA MapNetworkDrive 到带有 Windows 凭据的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26741669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:07:33  来源:igfitidea点击:

VBA MapNetworkDrive to server with windows credentials

vbams-accesssharepoint

提问by trixrabbit

I'm using windows 7 and Access 2010. I'm trying to connect to our server to see files. My problem is that I don't want to have the username or password in the code. I would like to have windows to prompt for it if authentication needed (from my tests, once you authenticate you don't have to re-do it for a while). From MapNetworkDrive Object MSDN, strUser and strPassword is optional and uses current users credentials, but this doesn't seem to work for a server.

我使用的是 Windows 7 和 Access 2010。我正在尝试连接到我们的服务器以查看文件。我的问题是我不想在代码中包含用户名或密码。如果需要身份验证,我想让 Windows 提示它(从我的测试来看,一旦你通过身份验证,你就不必重新做一段时间了)。从MapNetworkDrive Object MSDN, strUser 和 strPassword 是可选的,并使用当前用户凭据,但这似乎不适用于服务器。

Dim objFSO As Object
Dim objFolder As Object
Dim objNetwork As Object
Dim strShareLetter As String
Dim strURL As String, strUser as String, strPassword as String
strShareLetter = "L:"


Set objFSO = CreateObject("Scripting.FileSystemObject")
strURL = "\company@SSL\DavWWWRoot\companydav\nodes345678\"

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strShareLetter, strURL, False, strUser, strPassword

Set objFolder = objFSO.GetFolder(strURL)

MsgBox objFolder.Files.Count
MsgBox objFolder.SubFolders.Count

Set objFolder = Nothing
Set objFSO = Nothing

objNetwork.RemoveNetworkDrive (strShareLetter)
Set objNetwork = Nothing

If I write my user/password in the code everything works. But I need other users to be able to use it and I don't want my credentials being used everywhere. I would like a windows credentials prompt or something along that way. I don't want to prompt for the user password via vba for security reasons.

如果我在代码中写下我的用户/密码,一切正常。但是我需要其他用户才能使用它,而且我不希望我的凭据在任何地方都被使用。我想要一个 Windows 凭据提示或类似的东西。出于安全原因,我不想通过 vba 提示输入用户密码。

Any ideas? I searched google for solutions but I can't seem to find one. thank you

有任何想法吗?我在谷歌上搜索了解决方案,但似乎找不到。谢谢你

回答by MattB

It sounds like you've already recognized that you need some sort of user form in your project to collect the user's credentials.

听起来您已经意识到您的项目中需要某种用户表单来收集用户的凭据。

Here's how I would do that. I'd insert a User-Form from the VBA editor. From there I'd grab a couple of text boxes and labels to it, label one box as "User ID" and the other as "Password." For the password text box I'd be sure to insert "*" in the PasswordChar property to mask the password. I'd also put a button on there that says "Submit" or "Log In" or something to that effect.

这就是我将如何做到这一点。我会从 VBA 编辑器插入一个用户表单。从那里我会抓取几个文本框和标签,将一个框标记为“用户 ID”,另一个标记为“密码”。对于密码文本框,我一定要在 PasswordChar 属性中插入“*”以屏蔽密码。我还会在那里放一个按钮,上面写着“提交”或“登录”或类似的东西。

Once you've done that, You'll need a couple of properties:

完成后,您将需要几个属性:

Private pUserId As String
Private pPassword As String 

Public Property Let UserId(value as string)
    pUserId = value
End Property

Public Property Get UserId() as string
    UserId = pUserId
End Property

Public Property Let Password(value as string)
    pPassword = value
End Property

Public Property Get Password() As String
    Password = pPassword
End Property 

Then you will want to use your log in button's click event to load the information from the text boxes into the properties and hide the form.

然后,您将需要使用登录按钮的单击事件将文本框中的信息加载到属性中并隐藏表单。

Private Sub CommandButton1_Click()
    UserId = TextBox1.text
    Password = TextBox2.text
    Me.hide
End Sub

Then all you have to do is dim and display the user form to the user.

然后您所要做的就是变暗并向用户显示用户表单。

...
Set objNetwork = CreateObject("WScript.Network")
Dim LogInForm As UserForm1
Set LogInForm = New UserForm1
LogInForm.Show vbModal
objNetwork.MapNetworkDrive strShareLetter, strURL, False, LogInForm.UserId, LogInForm.Password
...

And that's pretty much it. You might need to learn how to create user forms in VBA or brush up on that.

仅此而已。您可能需要学习如何在 VBA 中创建用户表单或复习它。