vba 访问共享网络文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27248221/
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:23:17  来源:igfitidea点击:

Access Shared Network Folder

excelvbanetworkingshared

提问by cfugge

I need to access via VBA a folder hosted on a Network File Server. The folder is accessible in writing only via a Service Account (different from normal user accounts) for which I do have username and password.

我需要通过 VBA 访问托管在网络文件服务器上的文件夹。该文件夹只能通过我有用户名和密码的服务帐户(不同于普通用户帐户)以书面形式访问。

Via the UI I can see that folder and map it as a local drive but in order to access it in writing I need to log off from Windows and logon via the Service Account.

通过 UI,我可以看到该文件夹​​并将其映射为本地驱动器,但为了以书面形式访问它,我需要从 Windows 注销并通过服务帐户登录。

Is there any way to access the network folder during a normal user session but hardcoding username and pwd in the VBA code?

有没有办法在普通用户会话期间访问网络文件夹,但在 VBA 代码中硬编码用户名和密码?

I did try mapping the folder as local drive with:

我确实尝试将文件夹映射为本地驱动器:

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "S:", "\corp-server\HostingFolder", False, Username, pwd

but did not work ("S" drive was not mapped). If instead I do the same but without providing Username and password:

但不起作用(“S”驱动器未映射)。如果我这样做但不提供用户名和密码:

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "S:", "\corp-server\HostingFolder"

it works perfectly.

它完美地工作。

Wondering now if what I am trying to do is actually possible? If not, is there any alternative?

现在想知道我正在尝试做的事情是否真的可行?如果没有,有什么替代方法吗?

Thanks

谢谢

采纳答案by AdamsTips

You might find this answerof value in your testing.

您可能会在测试中找到这个有价值的答案

Essentially, I would check a couple things...

基本上,我会检查一些事情......

  1. Make sure you are not already connected to this resource using the current logged in user. If you are, you might get an error message like the following: enter image description here

  2. Make sure you are using the domain\usernamesyntax in your username.

  1. 确保您尚未使用当前登录的用户连接到此资源。如果是,您可能会收到如下错误消息: enter image description here

  2. 确保您使用的domain\username是用户名中的语法。

Otherwise I think you are the right track. I put together some sample code based on the link above, and was able to successfully connect to a network share under a different user name and iterate through a list of files.

否则我认为你是正确的轨道。我根据上面的链接整理了一些示例代码,并且能够以不同的用户名成功连接到网络共享并遍历文件列表。

(Note the tip that you don't actually have to map a drive to establish a connection.)

(注意您实际上不必映射驱动器来建立连接的提示。)

The following code is a really quick (working) VBA implementation of the sample listed at Access network share from within VBScript eg FileSystemObject

以下代码是从 VBScript访问网络共享中列出的示例的非常快速(工作)的 VBA 实现,例如 FileSystemObject

Public Sub TestNetShareName()

    Dim NetworkObject As Object
    Dim FSO As Object
    Dim Directory As Object
    Dim Filename As Object
    Dim ServerShare As String
    Dim UserName As String
    Dim Password As String

    ServerShare = "\corp-server\HostingFolder"
    UserName = "mydomain\myuser"
    Password = "freddie123"

    Set NetworkObject = CreateObject("WScript.Network")
    Set FSO = CreateObject("Scripting.FileSystemObject")

    NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password

    Set Directory = FSO.GetFolder(ServerShare)
    For Each Filename In Directory.Files
        Debug.Print Filename.Name
    Next

    Set Filename = Nothing
    Set Directory = Nothing
    Set FSO = Nothing

    NetworkObject.RemoveNetworkDrive ServerShare, True, False

    Set NetworkObject = Nothing

End Sub