简体   繁体   English

如何使用Excel VBA访问共享的网络文件夹?

[英]How To Access Shared Network Folder Using Excel VBA?

I have created a database using MSExcel as a front-end and MSAccess as a back-end. 我创建了一个数据库,使用MSExcel作为前端,使用MSAccess作为后端。 The MSAccess file is located in Shared Network Folder that requires username and password which I already have. MSAccess文件位于“共享网络文件夹”中,该文件夹需要我已经拥有的用户名和密码。 In order for other people without access to Shared Network Folder to use the file, I want to inject my own username/password in MSExcel file by using a VBA code. 为了使其他无法访问“共享网络文件夹”的人使用该文件,我想使用VBA代码在MSExcel文件中注入自己的用户名/密码。

I use this method but still showing this error -2147023677: Access Shared Network Folder 我使用此方法,但仍显示此错误-2147023677: 访问共享的网络文件夹

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 = "\\10.226.2.55\Dept_Share\MHE"
    UserName = "mydomain\myuser"
    Password = "password1234"

    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

The code works fine, also here. 该代码也可以正常工作。 But the error message: 但是错误信息:

在此处输入图片说明

is very clear. 很清楚 You cannot access the same source at the same time with two or more credentials. 您不能使用两个或多个凭据同时访问同一源。 Thus, resolve that, and the code will run. 因此,解决该问题,代码将运行。

Edit : 编辑

If/as the user already have a connection, map without credentials: 如果/由于用户已经有连接,请在不使用凭据的情况下进行映射:

NetworkObject.MapNetworkDrive "", ServerShare, False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM