Access Shared Network Folder

核能气质少年 提交于 2019-11-28 10:21:47
AdamsTips

You might find this answer of 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:

  2. Make sure you are using the domain\username syntax in your 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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!