2017-05-08 2 views
1

Je suis en train de télécharger un fichier de OneDrive. Je suis nouveau Concepts API et voici le code,VBA télécharger un fichier de OneDrive

Sub OneDrive_Download() 

'Declare the Object 
Dim oRequest As Object 

'Create and Assign Object 
Set oRequest = CreateObject("MSXML2.XMLHTTP") 

'Input User OneDrive URL 
URL = "https://xxxx-my.sharepoint.com/personal/sidxxx_ie/" 

'Post the URL in the Object 
oRequest.Open "POST", URL, False 

'Send Keys to the API 
oRequest.send ("{""client_id"":myclientid,""CLIENT_SECRET"":myclientsecret}") 


'Print the Response in the Immediate Window 
Debug.Print oRequest.ResponseText 

End Sub 

Et voici la réponse que je suis arrivé de Debug.Print sur ma fenêtre immédiate,

// Setup cta message fields. 
window.$Do.when("User", 0, function() 
{ 
    User.setupCallToActionMessages(); 
}); 

// Other tile 
var Tiles = Tiles || {}; 
Tiles.otherJSON = { 
    'name': 'Use another account', 
    'login': '', 
    'imageAAD': 'other_glyph.png', 
    'imageMSA': 'other_glyph.png', 
    'isLive': false, 
    'link': 'other', 
    'authUrl': '', 
    'sessionID': '', 
    'domainHint': 'other' 
}; 
</script> 
</body> 
</html> 

Maintenant, je cherche à télécharger un fichier nommé comme test.xlsx dans mon OneDrive. Quel est le moyen de s'y prendre?

MISE À JOUR - CODE

Sub DownloadFile() 

'Declare the Object and URL 
Dim myURL As String 
Dim WinHttpReq As Object 

'Assign the URL and Object to Variables 
myURL = "https://xxx-my.sharepoint.com/personal/Sidxxx/Documents/test.xlsx" 
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") 

'Provide Access Token and PWD to the URL for getting the service from API 
WinHttpReq.Open "GET", myURL, False, "abcdef", "12345" 
WinHttpReq.send 

Debug.Print WinHttpReq.Status 

myURL = WinHttpReq.responseBody 

    If WinHttpReq.Status = 200 Then 

     Set oStream = CreateObject("ADODB.Stream") 

     oStream.Open 

     oStream.Type = 1 

     oStream.SaveToFile "C:\testdownload.xlsx", 2 

     oStream.Close 

    End If 

End Sub 

Le fichier est téléchargé maintenant obtenir. Mais il semble être vide.

+0

'oRequête.Ouvrez" GET ", URLofFile, Faux' Je pense. Je ne sais pas ce que le faux est pour. – GibralterTop

+0

OneDrive n'est-il pas associé à une lettre de lecteur sur votre ordinateur? Si c'est le cas, vous devriez être en mesure d'utiliser des commandes de fichiers régulières pour copier un fichier de celui-ci sur votre PC. –

+0

@RichHolton oui, il est mappé à mon local. Mais l'application doit télécharger le fichier partagé avec moi dossier. C'est là que le problème commence. OneDrive ne synchronise pas les fichiers partagés avec les fichiers locaux et la synchronisation. – Sid29

Répondre

2

Ce code fonctionne pour moi. Merci à tous pour vos conseils.

Sub DownloadFile() 

'Declare the Object and URL 
Dim myURL As String 
Dim WinHttpReq As Object 

'Assign the URL and Object to Variables 
myURL = "https://xxx-my.sharepoint.com/personal/Sidxxx/Documents/test.xlsx" 
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") 

'Provide Access Token and PWD to the URL for getting the service from API 
WinHttpReq.Open "GET", myURL, False, "abcdef", "12345" 
WinHttpReq.send 

Debug.Print WinHttpReq.Status 

myURL = WinHttpReq.responseBody 

    If WinHttpReq.Status = 200 Then 

     Set oStream = CreateObject("ADODB.Stream") 

     oStream.Open 

     oStream.Type = 1 

     oStream.Write WinHttpReq.responseBody 

     oStream.SaveToFile "C:\testdownload.xlsx", 2 

     oStream.Close 

    End If 

End Sub