Option Explicit
Sub FeuillesParXML_Amelioree_Test6()
Dim chemin As String, tempZip As String, tempFolder As String
Dim xmlPath As String
Dim xDoc As Object, nodes As Object
Dim i As Long, Affichage As String
Dim objShell As Object, fso As Object, cmd As String
Dim ext As String
' === <--- Ajuste le chemin ici
chemin = "C:\Users\Chemins\Downloads\LeFichier.xlsx"
' =====================================================
ext = LCase(Right$(chemin, 5))
If ext <> ".xlsx" Then
MsgBox "Le fichier doit être un .xlsx (format OpenXML). Extension détectée: " & ext, vbExclamation
Exit Sub
End If
tempZip = Environ("TEMP") & "\classeur.zip"
tempFolder = Environ("TEMP") & "\classeur_xml"
xmlPath = tempFolder & "\xl\workbook.xml"
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If fso.FileExists(tempZip) Then fso.DeleteFile tempZip, True
If fso.FolderExists(tempFolder) Then fso.DeleteFolder tempFolder, True
On Error GoTo 0
' Copier et renommer en .zip
FileCopy chemin, tempZip
' Extraire le zip avec PowerShell — on attend la fin de l'opération
Set objShell = CreateObject("WScript.Shell")
cmd = "powershell -NoProfile -NonInteractive -Command ""Try { Expand-Archive -Path '" & Replace(tempZip, "'", "''") & "' -DestinationPath '" & Replace(tempFolder, "'", "''") & "' -Force } Catch { exit 1 }"""
If objShell.Run(cmd, 0, True) <> 0 Then
MsgBox "Erreur lors de l'extraction (PowerShell). Vérifie que PowerShell est disponible.", vbCritical
GoTo Nettoyage
End If
If Dir(xmlPath) = "" Then
MsgBox "workbook.xml introuvable après extraction : " & vbCrLf & xmlPath, vbExclamation
GoTo Nettoyage
End If
' Charger le XML et vérifier parseError
Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
xDoc.async = False
xDoc.validateOnParse = False
If Not xDoc.Load(xmlPath) Then
MsgBox "Erreur de chargement du XML : " & xDoc.parseError.reason, vbCritical
GoTo Nettoyage
End If
' Mapper le namespace puis tenter la sélection des noeuds
xDoc.SetProperty "SelectionNamespaces", "xmlns:ns='http://schemas.openxmlformats.org/spreadsheetml/2006/main'"
Set nodes = xDoc.SelectNodes("/ns:workbook/ns:sheets/ns:sheet")
If nodes Is Nothing Or nodes.Length = 0 Then Set nodes = xDoc.SelectNodes("//ns:sheet")
If nodes Is Nothing Or nodes.Length = 0 Then
MsgBox "Aucun noeud <sheet> trouvé dans workbook.xml. Le fichier XML peut utiliser un autre namespace ou être corrompu.", vbExclamation
' Afficher un aperçu du début du XML pour debug (utile pour comprendre la structure)
Dim aperçu As String
aperçu = Left$(xDoc.XML, 1000)
MsgBox "Aperçu début de workbook.xml :" & vbCrLf & aperçu, vbInformation
GoTo Nettoyage
End If
' Construire l'affichage numéroté
For i = 0 To nodes.Length - 1
Affichage = Affichage & (i + 1) & " - " & nodes.Item(i).getAttribute("name") & vbCrLf
Next i
MsgBox nodes.Length & " feuille(s) trouvée(s) :" & vbCrLf & vbCrLf & Affichage, vbInformation, "Liste des feuilles"
Nettoyage:
On Error Resume Next
If Not fso Is Nothing Then
If fso.FolderExists(tempFolder) Then fso.DeleteFolder tempFolder, True
If fso.FileExists(tempZip) Then fso.DeleteFile tempZip, True
End If
On Error GoTo 0
End Sub