Excel VBA - I cannot open the workbook in sharepoint until I enter sharepoint and the access file

EDIT 3/31/14 - There are no answers ... are still confused by this behavior. Hit! Any thoughts?

I installed some code in an Excel workbook on my local hard drive, which moves to the corporate sharepoint site, scans a set of folders, and when it comes to a file, it opens it and pops some data. I gathered a lot of them along with other posts and from the previous question I asked.

That is the question I have. If I started it without first accessing the Sharepoint website and opening the file, it gives an error message. However, as soon as I did this, it works fine. My only thought is that the Sharepoint site is expecting my corporate credentials (username and password), and since I do not transmit this in my

Set oWB = Workbooks.Open(MyPath)

it robs me of access. The error produces a description ... just a long int value.

Here's the full code, with classified material removed. It mainly uses recursion to access the root child nodes. Therefore, I think my question is doubled ... 1) what causes the problem; 2) if these are network credentials, can I somehow pass the username and password ?:

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
+4
2

HTTP- , , , - , , , , - SharePoint, , , ( ). , :

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

, SharePoint, SharePoint - , .

+1

, , ...

"Microsoft Internet Controls"

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False

ie.Navigate (YourSite)

ie.quit

, ...

0

Source: https://habr.com/ru/post/1529284/


All Articles