简体   繁体   中英

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. The MSAccess file is located in Shared Network Folder that requires username and password which I already have. 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.

I use this method but still showing this error -2147023677: Access Shared Network Folder

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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