Hi there,
I am trying to download a file from sharepoint and I'm running into a little oddity.
If I try and run this code:
Sub test()
Dim myURL, FileName, FilePath As String
'Dim ReturnArray As Variant
myURL = "SharePointUrl\Filename.Ext"
FilePath = "\\FilePath"
FileName = "test.xlsx"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
If FileExists(FilePath & "\" & FileName) Then 'See above
' First remove readonly attribute, if set
SetAttr FilePath & "\" & FileName, vbNormal
' Then delete the file
Kill FilePath & "\" & FileName
End If
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
Set ostream = CreateObject("ADODB.Stream")
ostream.Open
ostream.Type = 1
ostream.Write WinHttpReq.ResponseBody
ostream.SaveToFile (FilePath & "\" & FileName)
ostream.Close
End If
End Sub
I get an access denied message on the line WinHttpReq.Send
however if run this first.
Sub test2()
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "sharepointURL\Filename.ext"
.AllowMultiSelect = False
.Show
End With
End Sub
and close the window, and then run the first script it works flawlessly. I'm guessing its some sort of authentication check or something.
is there anyway I combine whatever is working in the second routine in the first at all?
Bookmarks