首页 > 解决方案 > Sharepoint 中的 Word VBA MkDir 路径无效

问题描述

我正在尝试使用 VBA 在我的工作中在 Sharepoint 中创建一个文件夹。该文档是从 Sharepoint 打开的,因此应该没有凭据问题(我认为)。

我已经尝试了以下所有方法并且总是得到运行时错误“76”:找不到路径

.Path 如何读取文档的位置(显然已经删除了文档)

MkDir "https://company.sharepoint.com/directory/directory with spaces"

无证书

MkDir "//company.sharepoint.com/directory/directory with spaces"

目录之间有反斜杠

MkDir "https://company.sharepoint.com\directory\directory with spaces"

有更正的空格

MkDir "https://company.sharepoint.com/directory/directory%20with%20spaces"

以及上述的大多数组合。

我注意到 Word 需要更长的时间才能确定它是没有证书的无效路径。

由于 NDA 问题,我无法发布实际路径,但上述娱乐应该在路径中包含所有相关的可能问题。我没有从变量或输入中解析路径(尽管我稍后会),它们保存在一个私有子中。

感谢您提供的任何帮助。

标签: vbasharepointmkdir

解决方案


好的,这比我预期的花费了我更长的时间来完成。我基本上只是从我上面的第一个评论链接中的链接中获取了解决方案,并添加了错误处理,以便(希望)所有场景都有一个很好的退出点和解释。

Sub SharepointAddFolder()

    Dim filePath As String
    filePath = "https://web.site.com/SharedDocuments/Folder"

    'filePath = Replace(filePath, "https:", "")     'I didn't need these but who knows
    'filePath = Replace(filePath, "/", "\")
    'filePath = Replace(filePath, " ", "%20")

    Dim newFolderName As String
    newFolderName = "New Folder"

    Dim driveLetter As String
    driveLetter = "Z:"

    Dim ntwk As Object
    Set ntwk = CreateObject("WScript.Network")

    On Error GoTo ErrHandler
    ntwk.MapNetworkDrive driveLetter, filePath, False ', "username", "password"

    If Len(Dir(driveLetter & "/" & newFolderName, vbDirectory)) = 0 Then
        MkDir driveLetter & "/" & newFolderName
    Else
        MsgBox "Folder " & newFolderName & " already exists."
    End If

ExitThis:
    ntwk.RemoveNetworkDrive driveLetter
    Exit Sub

ErrHandler:
    Select Case Err.Number
        Case -2147024829
            MsgBox "Sharepoint site not found"

        Case 76
            'sharepoint directory not found
            MsgBox "Mapping failed"

        Case -2147024811
            'drive already mapped
            Resume Next

        Case -2147022646
            'drive not found and thus cannot be closed

        Case -2147022495
            MsgBox "This network connection has files open or requests pending." & vbNewLine & vbNewLine & _
                "Either close the files or wait until the files are closed, then try to cancel the connection."

        Case Else
            MsgBox "Error " & Err.Number & ": " & vbNewLine & Err.Description
    End Select

End Sub

推荐阅读