A tester:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| Sub CopierVersSharePoint()
Dim objFSO As Object
Dim objFile As Object
Dim objIE As Object
Dim strSourcePath As String
Dim strDestinationURL As String
Dim strUsername As String
Dim strPassword As String
' Chemin du fichier source sur votre poste
strSourcePath = "C:\Chemin\vers\votre\fichier.csv"
' URL du dossier SharePoint de destination
strDestinationURL = "https://nomdevotresharepoint.com/sites/votresite/DocumentLibrary/"
' Identifiants d'authentification pour SharePoint
strUsername = "votre_nom_d_utilisateur"
strPassword = "votre_mot_de_passe"
' Créer une instance de l'objet FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Vérifier si le fichier source existe
If objFSO.FileExists(strSourcePath) Then
' Créer une instance de l'objet File
Set objFile = objFSO.GetFile(strSourcePath)
' Créer une instance de l'objet InternetExplorer
Set objIE = CreateObject("InternetExplorer.Application")
' Ouvrir Internet Explorer en mode invisible
objIE.Visible = False
' Naviguer vers le dossier SharePoint
objIE.Navigate strDestinationURL
' Attendre que la page se charge complètement
Do While objIE.Busy Or objIE.ReadyState <> 4
Application.Wait Now + TimeValue("0:00:01")
Loop
' Remplir le formulaire de connexion avec les identifiants
objIE.Document.getElementById("username").Value = strUsername
objIE.Document.getElementById("password").Value = strPassword
' Soumettre le formulaire de connexion
objIE.Document.getElementById("loginButton").Click
' Attendre que la page se charge complètement après la connexion
Do While objIE.Busy Or objIE.ReadyState <> 4
Application.Wait Now + TimeValue("0:00:01")
Loop
' Copier le fichier vers SharePoint
objFSO.CopyFile strSourcePath, objIE.Document.getElementById("uploadInput").Value
' Fermer Internet Explorer
objIE.Quit
' Afficher un message de succès
MsgBox "Le fichier a été copié avec succès vers SharePoint.", vbInformation
Else
' Afficher un message d'erreur si le fichier source n'existe pas
MsgBox "Le fichier source n'a pas été trouvé.", vbExclamation
End If
' Libérer les objets
Set objFSO = Nothing
Set objFile = Nothing
Set objIE = Nothing
End Sub |
Le problème ne vient pas de la macro mais des autorisations d'accès.
J'utilise l'objet InternetExplorer pour naviguer vers le dossier SharePoint de destination et remplir le formulaire de connexion avec les identifiants fournis.
Ensuite, on attend que la page se charge complètement après la connexion. Ensuite, on copie le fichier vers SharePoint en utilisant l'objet FileSystemObject.
Ne pas oublier de modifier les variables strSourcePath, strDestinationURL
Partager