Excel VBA - 在登录共享点并访问文件之前,在共享点中打开工作簿失败

标签 excel sharepoint-2010 excel-2010 vba

编辑 2014 年 3 月 31 日 -- 没有回复...仍然对这种行为感到困惑。撞!有什么想法吗?

我在本地硬盘上的 Excel 工作簿中设置了一些代码,该代码可以导航到公司共享点站点,浏览一组文件夹,当遇到文件时,它会打开该文件并提取一些数据。我从其他帖子和我之前提出的问题中拼凑了很多内容。

这是我遇到的问题。如果我在没有先进入 Sharepoint 站点并打开文件的情况下运行它,则会抛出错误。然而,一旦我这样做了,它就工作得很好。我唯一的想法是 Sharepoint 网站需要我的公司凭据(用户名和密码),因为我没有将其传递到我的

Set oWB = Workbooks.Open(MyPath)

命令,它拒绝我访问。错误是抛出一个描述...只是一个 long int 值。

这是完整的代码,其中删除了 secret 内容。它基本上使用递归来到达根子节点。所以我想我的问题有两个......1)是什么导致了这个问题; 2)如果是网络凭据,我可以以某种方式传递用户名和密码吗?:

Public Stack As New Collection
Public PrintLine As String
Public Spaces As String
Public fnum As Integer
Public outputFile As String


Sub NavigateSharepointSite()

    On Error Resume Next
    Dim spSite As String, spDir As String, spFile As String, url As String

    spSite = "https://myteamssite"
    spDir = ""
    spFile = ""
    url = spSite & spDir & spFile
    Stack.Add (Array(spSite, spDir, spFile, url, "d", 0))

    NavigateFolder spSite, spDir, url, 0

End Sub

Sub NavigateFolder(spSite As String, spDir As String, url As String, level As Integer)

    Dim davDir As New ADODB.Record
    Dim davFile As New ADODB.Record
    Dim davFiles As New ADODB.Recordset
    Dim isDir As Boolean
    Dim tempURL As String
    On Error GoTo showErr
    tempURL = "URL=" & url
    davDir.Open "", tempURL, adModeReadWrite, adFailIfNotExists, adDelayFetchStream
    If davDir.RecordType = adCollectionRecord Then      
        Set davFiles = davDir.GetChildren()  ''Returns recordset of all child records from parent
        Do While Not davFiles.EOF


            davFile.Open davFiles, , adModeRead
            isDir = davFile.Fields("RESOURCE_ISCOLLECTION").Value
            If Not isDir Then ''if not children

                spFile = Replace(davFile.Fields("RESOURCE_PARSENAME").Value, "%20", " ")
                url = spSite & spDir & "/" & spFile
                Stack.Add (Array(spSite, spDir, spFile, url, "f", level))
                If spFile Like "Quarterly*" Then

                   testthis (url)
                End If

            Else

                level = level + 1
                url = Replace(davFile.Fields("RESOURCE_ABSOLUTEPARSENAME").Value, "%20", " ")
                spDir = Right(url, Len(url) - Len(spSite))
                Stack.Add (Array(spSite, spDir, "", url, "d", level))

                NavigateFolder spSite, spDir, url, level
                level = level - 1
            End If
            davFile.Close
            davFiles.MoveNext
        Loop
    End If
    Set davFiles = Nothing
    davDir.Close
    Set davDir = Nothing


    GoTo noErr
    showErr:
    Call MsgBox(Err.Number & ": " & Err.Description & Chr(10) _
    & "spSite=" & spSite & Chr(10) _
    & "spDir= " & spDir & Chr(10) _
    & "spFile=" & spFile, vbOKOnly, "Error")

    noErr:
End Sub



Private Function testthis(MyPath As String)
    Dim oWB As Workbook '', MyPath As String

    Debug.Print MyPath
    If Workbooks.CanCheckOut(MyPath) = True Then          
        Set oWB = Workbooks.Open(MyPath)
        oWB.Application.DisplayAlerts = False
        Debug.Print (oWB.Worksheets(1).Name)
        oWB.Close False
        Set oWB = Nothing

    Else
        MsgBox ("File on Sharepoint can NOT be checked out." + Chr(13) + _
                "Make sure no one else is working in the file." + Chr(13) + _
                "Including yourself.")
        Exit Function
    End If


End Function

最佳答案

您可以尝试使用您的凭据发送 HTTP 请求,但当您打开工作簿时,系统可能仍会要求您提供凭据,这实际上只是启动 SharePoint session 服务器端,这可以可能会阻止您当前遇到的问题(如果该问题是由于身份验证引起的)。在尝试打开工作簿之前尝试以下代码:

With CreateObject("WinHTTP.WinHttpRequest.5.1")
    .Open "GET", spSite, False
    .SetCredentials  "Domain\username", "Password", 0 'Change as required.
    .Send
End With

我倾向于与 SharePoint 一起使用的另一个选项是将持久网络驱动器映射到 SharePoint 目录并在连接驱动器时保存您的凭据 - 然后只需在代码中指向该驱动器,就像指向任何其他驱动器一样.

关于Excel VBA - 在登录共享点并访问文件之前,在共享点中打开工作簿失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22078727/

相关文章:

sharepoint - 用于列出所有 sharepoint 2010/2007 页面及其布局的 Powershell 脚本

sharepoint - 在 SharePoint 2010 中通过标签搜索

excel - 删除整列数据中的前导或尾随空格

excel - 将列与分隔符 ',' 合并,如果不存在值则不渲染分隔符

excel - VBA 问题,尝试让我的宏工作时出现私有(private)函数错误

java - XSLT 从两个不同的源读取(XML 文件和 Excel 工作表)

sharepoint-2010 - 在SharePoint 2010中启用发布基础结构功能

Excel:搜索列中是否存在特定文本

excel - 如何在Excel中的每个单元格中添加时间范围?

vba - Excel 2010 VBA : How to export the current worksheet asking user where to save?