VBA-无法将驱动器映射到另一台计算机上的共享点

aa

我正在使用VBA映射到公司的共享点驱动器。目的是将本地文件保存到共享点,并在成功后删除本地文件并取消映射驱动器。

在我的计算机上(Windows 10 64位),代码运行正常,可以成功映射驱动器,创建文件夹和文件,成功上传到共享点并取消映射驱动器。

但是,当我在同事的计算机(Window 7)上运行包含相同代码的相同excel工作簿时,它失败了。没有显示任何错误,除了它一直加载并加载直到Excel Not Responsive为止我尝试手动映射驱动器,它成功。

我尝试调试,发现该代码在(停止加载时保持)停止运行,MsgBox "Hello"但找不到丢失的内容。

两者都使用Excel 2016

任何帮助和建议,表示赞赏。让我知道是否需要更多信息。提前致谢。

这是我的VBA代码

Sub imgClicked()

Dim fileName As String

Dim SharePointLib As String
Dim MyPath As String
Dim folderPath As String
Dim objNet As Object
Dim copyPath As String
Dim copyFilePath As String

folderPath = Application.ThisWorkbook.path
MyPath = Application.ThisWorkbook.FullName

Dim objFSO As Object
Dim strMappedDriveLetter As String
Dim strPath As String
Dim spPath As String

strPath = "https://company.com/sites/test/test 123/" 'example path
spPath = AvailableDriveLetter + ":\test.xlsm" 'example path
copyPath = folderPath + "\copyPath\"

'Add reference if missing
Call AddReference

Set objFSO = CreateObject("Scripting.FileSystemObject")

With objFSO

strMappedDriveLetter = IsAlreadyMapped(.GetParentFolderName(strPath))

If Not Len(strMappedDriveLetter) > 0 Then

  strMappedDriveLetter = AvailableDriveLetter

  If Not MapDrive(strMappedDriveLetter, .GetParentFolderName(strPath)) Then

    MsgBox "Failed to map SharePoint directory", vbInformation, "Drive Mapping Failure"
     Exit Sub

  End If

 End If

 ' Check file/folder path If statement here

End With

Set objFSO = Nothing

End Sub

获取可用驱动器的代码

  ' Returns the available drive letter starting from Z
 Public Function AvailableDriveLetter() As String

' Returns the last available (unmapped) drive letter, working backwards from Z:

Dim objFSO As Object
Dim i As Long

Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = Asc("Z") To Asc("A") Step -1

Select Case objFSO.DriveExists(Chr(i))

  Case True

  Case False

    Select Case Chr(i)

      Case "C", "D"     ' Not actually necessary - .DriveExists should return True anyway...

      Case Else

        AvailableDriveLetter = Chr(i)

        Exit For

    End Select

End Select

Next i

Set objFSO = Nothing
 MsgBox "This is the next available drive: " + AvailableDriveLetter ' returns Z drive
 MsgBox "Hello" ' After this msgBox, starts loading until Not Responsive
End Function

映射驱动器功能

Public Function MapDrive(strDriveLetter As String, strDrivePath As String) As Boolean

Dim objNetwork As Object

If Len(IsAlreadyMapped(strDrivePath)) > 0 Then Exit Function

Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive strDriveLetter & ":", strDrivePath, False

MapDrive = True
MsgBox "Successfully Created the Drive!"
Set objNetwork = Nothing

End Function

MappedDrive的代码

Public Function GetMappedDrives() As Variant

' Returns a 2-D array of (1) drive letters and (2) network paths of all mapped drives on the users machine

Dim objFSO As Object
Dim objDrive As Object
Dim arrMappedDrives() As Variant
Dim i As Long

Set objFSO = CreateObject("Scripting.FileSystemObject")

ReDim arrMappedDrives(1 To 2, 1 To 1)

For i = Asc("A") To Asc("Z")

If objFSO.DriveExists(Chr(i)) Then

  Set objDrive = objFSO.GetDrive(Chr(i))

  If Not IsEmpty(arrMappedDrives(1, UBound(arrMappedDrives, 2))) Then

    ReDim Preserve arrMappedDrives(1 To 2, 1 To UBound(arrMappedDrives, 2) + 1)

  End If

  arrMappedDrives(1, UBound(arrMappedDrives, 2)) = Chr(i)            ' Could also use objDrive.DriveLetter...
  arrMappedDrives(2, UBound(arrMappedDrives, 2)) = objDrive.ShareName
End If

Next i

GetMappedDrives = arrMappedDrives

Set objDrive = Nothing
Set objFSO = Nothing

End Function

Public Function IsAlreadyMapped(strPath As String) As String

' Tests if a given network path is already mapped on the users machine
' (Returns corresponding drive letter or ZLS if not found)

Dim strMappedDrives() As Variant
Dim i As Long

strMappedDrives = GetMappedDrives

 For i = LBound(strMappedDrives, 2) To UBound(strMappedDrives, 2)

  If LCase(strMappedDrives(2, i)) Like LCase(strPath) Then

  IsAlreadyMapped = strMappedDrives(1, i)

    Exit For

  End If

  Next i

  Set objNetwork = Nothing

  End Function

添加参考

Sub AddReference()
 'Macro purpose:  To add a reference to the project using the GUID for the
 'reference library

Dim strGUID As String, theRef As Variant, i As Long

 'Update the GUID you need below.
strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}"

 'Set to continue in case of error
On Error Resume Next

 'Remove any missing references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
    Set theRef = ThisWorkbook.VBProject.References.Item(i)
    If theRef.isbroken = True Then
        ThisWorkbook.VBProject.References.Remove theRef
    End If
Next i

 'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear

 'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0

 'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
     'Reference already in use.  No action necessary
Case Is = vbNullString
     'Reference added without issue
Case Else
     'An unknown error was encountered, so alert the user
    MsgBox "A problem was encountered trying to" & vbNewLine _
    & "add or remove a reference in this file" & vbNewLine & "Please check the " _
    & "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub
Ashleedawg

程序多次imgClicked调用函数AvailableDriveLetter请记住,该函数必须在每次引用它时执行。

我跑imgClicked(假设这是你开始的程序),我被告知,两次"Next available letter = Z"并且"Hello"然后坠毁的Excel(也许陷在创建文件系统对象,寻找可用的驱动器盘符的循环?)

尝试AvailableDriveLetter在过程开始时分配一个变量(字符串),并在每次需要该值时都引用该变量,以查看是否仍然存在问题。

(请记住在执行之前先保存-在对“应用程序挂起”问题进行故障排除时,我会感到沮丧,因为我一直忘记保存所做的更改,然后在崩溃时将其丢失!)

如果这不起作用,请在“ Hello”框之后F9End Function添加一个断点(),然后查看代码是否在此处停止。(我很难相信是MsgBox或是End Function罪魁祸首。)如果没有,那之后执行哪个程序?

问题是否解决还有一件事:

Option Explicit在模块的开头添加Compile项目,然后添加项目并修复丢失的变量声明。

在对问题进行故障排除时,建议使用此方法,以消除可能导致问题的变量声明问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将映射的驱动器迁移到另一台计算机

在另一台计算机上移动受HP DriveLock保护的硬盘驱动器

如何直接从另一台计算机上的Crashplan备份目标硬盘驱动器还原?

将具有Perforce工作区的驱动器从一台计算机移动到另一台计算机

VBA 宏在尝试保存时抛出错误(但仅在一台计算机上)

将辅助硬盘驱动器从一台计算机移动到另一台并维护已安装的软件

关闭另一个用户使用VBA在同一台计算机上打开的excel工作簿

安装的archlinux是否可以通过替换硬盘驱动器在另一台计算机上运行?

在另一台计算机上运行

在外部驱动器上安装Linux,然后使用它启动另一台计算机

将文件复制到另一台计算机上的共享文件夹

为什么笔记本的Windows 8硬盘驱动器无法启动另一台计算机?

使用闪存驱动器将已编译的二进制文件复制到另一台计算机

如何通过网络将磁盘驱动器复制到另一台计算机(创建并使用这样的磁盘映像)?

如何使用32GB闪存驱动器将200GB文件传输到另一台计算机?

是否可以将LAMP服务器移动到同一台计算机上的另一个驱动器上?

Samba 文件共享在一台 Windows 计算机上工作,但在另一台计算机上不工作

无法使用另一台计算机上的Express-Generator访问NodeJS服务器

将 ubuntu 安装迁移到同一台计算机上的另一个驱动器

已发布的qt .exe在另一台计算机上无法打开

为什么从另一台计算机上无法使用GRPC?

我无法在另一台Ubuntu计算机上访问文件

无法发布到另一台计算机上的服务结构本地群集

在另一台计算机上编辑fstab,无法再启动

从另一台计算机访问虚拟机上的Web服务器

如何使用从另一台计算机安装的 ubuntu 作为外部驱动器访问硬盘驱动器?

Vagrantfile挂在一台计算机上,但未挂在另一台计算机上

无法在一台计算机上访问网站,但可以在另一台计算机上访问

ssh无法从另一台计算机到虚拟Ubuntu服务器