+ Reply to Thread
Results 1 to 5 of 5

Downloading file from sharepoint via VBA problem/oddity

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Downloading file from sharepoint via VBA problem/oddity

    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?

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Downloading file from sharepoint via VBA problem/oddity

    Guessing:

    Sub test()
                            test2
    
        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
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Downloading file from sharepoint via VBA problem/oddity

    Thank you for taking a look. I'm sure I'm missing something but I'm not seeing any difference between my original code snippet and your suggestion.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Downloading file from sharepoint via VBA problem/oddity

    If you are getting an error on WinHttpReq.Send, it is most likely that the URL is wrong.

  5. #5
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Downloading file from sharepoint via VBA problem/oddity

    Hi and thank you for taking a look.

    The URL is definitely correct - I'm pretty sure this is down to authentication, as after I have logged into the sharepoint service manually the code runs smoothly. But this then defeats the purpose of my attempts at automation.

    I've tried a couple of different methods of sending my user credentials, but both have failed, someone managerd to get a 0 return on WinHttpReq.Status

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to select SharePoint Content Type when saving Excel file to SharePoint
    By Luffk73 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2017, 05:22 PM
  2. Replies: 9
    Last Post: 12-23-2016, 10:54 PM
  3. Replies: 0
    Last Post: 10-08-2013, 10:54 AM
  4. Problem Populating Excel File With Values From Sharepoint DropDown
    By MrIceGuy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-17-2013, 09:32 AM
  5. problem with finding a file in https sharepoint
    By MKS2012 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-29-2012, 01:55 PM
  6. Problem downloading excel file from outlook
    By dahal0514 in forum Excel General
    Replies: 2
    Last Post: 08-13-2010, 12:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1