【问题标题】:Word VBA MkDir Invalid Path in SharepointSharepoint 中的 Word VBA MkDir 路径无效
【发布时间】:2018-08-07 16:13:36
【问题描述】:

我正在尝试使用 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 问题,我无法发布实际路径,但上述娱乐应该在路径中包含所有相关的可能问题。我没有从变量或输入中解析路径(虽然我稍后会),它们保存在一个私有子中。

感谢您提供的任何帮助。

【问题讨论】:

标签: vba sharepoint mkdir


【解决方案1】:

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

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

【讨论】:

  • 谢谢。起初我遇到了已经拥有 Z 驱动器的问题,但最终我记得我忘记了愚蠢的东西。我目前遇到用户身份验证问题,但这是另一个问题。感谢您的帮助。
  • @GarrettWilliamson 嗯,好吧,那么我可能应该改变我的错误 'drive already mapped' 因为你给出的例子
【解决方案2】:

对于那些希望继续临时映射 SharePoint 驱动器的人请注意:此代码无需任何用户名或密码即可工作(我的公司使用 Authenticator),但只有在您使用 Internet Explorer 登录到 SharePoint 后才能使用。我了解到,在使用 IE 时,“所有文档”下存在一个名为“在文件资源管理器中查看”的选项。 Chrome 或其他浏览器不存在(据我所知)。我的意图是永久映射驱动器,但是一旦我从 IE 登录,代码就可以工作了。您甚至不必一直登录到 IE,当您通过 IE 返回 SharePoint 时,您仍然处于登录状态(我已经在一天的不使用期间完成了此操作)。我认为这与 IE 是 Microsoft 产品有关,因此被信任保留登录凭据。

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多