简体   繁体   English

访问共享网络文件夹

[英]Access Shared Network Folder

I need to access via VBA a folder hosted on a Network File Server. 我需要通过VBA访问网络文件服务器上托管的文件夹。 The folder is accessible in writing only via a Service Account (different from normal user accounts) for which I do have username and password. 只能通过服务帐户(与普通用户帐户不同)以书面形式访问该文件夹,我有用户名和密码。

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). 但没有工作(“S”驱动器没有映射)。 If instead I do the same but without providing Username and password: 如果相反我做同样但没有提供用户名和密码:

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 谢谢

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. 确保您在domain\\username中使用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中的Access网络共享列出的示例的一个非常快速(工作)的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

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

相关问题 如何使用Excel VBA访问共享的网络文件夹? - How To Access Shared Network Folder Using Excel VBA? 当其他用户正在编辑(打开)Excel工作簿时,如何限制用户编辑网络共享文件夹中的Excel工作簿? - How to restrict a user from editing an Excel workbook in a network shared folder when it is being edited (open) by another user? 当数据库保存在共享网络驱动器上时,将Access数据库链接到Excel数据透视表 - Linking Access database to Excel pivot table, when Database is saved on a shared network drive Access VBA是否使用与登录用户不同的凭据来访问网络文件夹? - Does Access VBA use different credentials than logged in user to access network folder? 在共享文件夹上添加 Outlook 搜索文件夹 - Add Outlook Search Folder on shared folder 在网络驱动器上共享 *.xlam - 调用 subs 和函数 - shared *.xlam on network drive - call subs and functions 如何查找网络中共享的excel文件的完整网络路径? - How to find the full network path of an excel file shared in a network? 如何访问共享邮箱中的电子邮件? - How to access emails in shared mailbox? 将共享Outlook文件夹的内容导出到Excel - Export contents of shared Outlook folder into Excel 如何从共享文件夹中获取电子邮件正文? - How to get body of the email from a shared folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM