如何使用vba访问具有不同凭据的网络Excel文件?

时间:2022-02-13 14:47:19

I've created VBA code so that a group of people in our company can "stamp" a word document with a unique number stored in an Excel sheet on a network drive (essentially giving a serial#). These people do not have access to said network drive, but I'd like them to be able to execute the VBA code.

我已经创建了VBA代码,以便我们公司的一组人可以使用存储在网络驱动器上的Excel工作表中的唯一编号“标记”word文档(基本上是给出序列号#)。这些人无法访问所述网络驱动器,但我希望他们能够执行VBA代码。

I've been reading over various articles on this site and others for the last couple days, and was able to adapt this post to work for me. However, if I try and execute the VBA code more than once, I get the "multiple connections" error described in the original answer. It seems, then, that the mapped drive is not being removed. Is this a simple syntax issue? Or am i missing something in my RemoveNetworkDrive statement?

在过去的几天里,我一直在阅读本网站和其他人的各种文章,并能够使这篇文章适合我。但是,如果我尝试多次执行VBA代码,我会得到原始答案中描述的“多个连接”错误。那么,似乎没有删除映射的驱动器。这是一个简单的语法问题吗?或者我在RemoveNetworkDrive语句中遗漏了什么?

ServerShare = "\\Servername\path"
UserName = "domain\username"
Password = "password"

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

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

str_WbPath = ServerShare & "\MRL Number Generator.xlsm"
Set exWb = objExcel.Workbooks.Open(str_WbPath)

'Do Stuff in excel


Set exWb = Nothing
Set FSO = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set NetworkObject = Nothing

1 个解决方案

#1


0  

I'll tell you I it worked for me. I used a drive, and I removed the drive instead.

我会告诉你我这对我有用。我使用了一个驱动器,而是移除了驱动器。

'This insures you find a empty drive letter
For i = 67 To 90
    netDrive = Chr(i) & ":"
    If Not fs.DriveExists(netDrive) Then Exit For
Next

'mount network drive on the above selected drive letter
Network.MapNetworkDrive netDrive, ServerShare, False, UserName, Password

'DO STUFF
'DO STUFF

'remove that drive letter
Network.RemoveNetworkDrive netDrive, True, True

I'd put the remove on some error handling as well, otherwise you'll spawn a bunch of drives if you have some error.

我也将删除文件放在一些错误处理上,否则如果你有一些错误,你会产生一堆驱动器。

#1


0  

I'll tell you I it worked for me. I used a drive, and I removed the drive instead.

我会告诉你我这对我有用。我使用了一个驱动器,而是移除了驱动器。

'This insures you find a empty drive letter
For i = 67 To 90
    netDrive = Chr(i) & ":"
    If Not fs.DriveExists(netDrive) Then Exit For
Next

'mount network drive on the above selected drive letter
Network.MapNetworkDrive netDrive, ServerShare, False, UserName, Password

'DO STUFF
'DO STUFF

'remove that drive letter
Network.RemoveNetworkDrive netDrive, True, True

I'd put the remove on some error handling as well, otherwise you'll spawn a bunch of drives if you have some error.

我也将删除文件放在一些错误处理上,否则如果你有一些错误,你会产生一堆驱动器。