Option Explicit
Sub Export_CommentairesPicturesVPat()
'fonction et macros patricktoulon
Dim oApp As Object, sourceZip$, folderZipimage$, DestFolderimage$, drawing1VML$, drawing1VMLREL$, i&
Dim UnZipeur As Object, bm As New cBenchmark
tim = Timer
'les path
sourceZip = ThisWorkbook.Path & "\zzz.zip"
DestFolderimage = ThisWorkbook.Path & "\media"
ThisWorkbook.SaveCopyAs sourceZip 'zippage du classeur dans son etat actuel(c'est un peu plus long mais a jour)
'suppression des fichier existants si le dossier existe
If Dir(DestFolderimage, vbDirectory) <> "" Then
If Dir(DestFolderimage & "\*.*") <> "" Then Kill DestFolderimage & "\*.*"
RmDir DestFolderimage
End If
'-------------------------------------------------------
'dézippage by patricktoulon(france)
Set UnZipeur = CreateObject("Shell.Application")
folderZipimage = UnZipeur.Namespace(sourceZip & "\xl").Items.Item("media").Path
'extraction du dossier media en entier
UnZipeur.Namespace(ThisWorkbook.Path & "\").CopyHere (folderZipimage)
Do While Dir(DestFolderimage, vbDirectory) = "" Or i < 1000: i = i + 1: DoEvents: Loop
If Dir(DestFolderimage, vbDirectory) = "" Then MsgBox "l 'extraction du dossier media c'est Mal passée" & vbCrLf & "sortie du programe!!": Set oApp = Nothing: Exit Sub
'chemin du vmlDrawing1.vml.rels
drawing1VMLREL = UnZipeur.Namespace(sourceZip & "\xl\drawings\_rels").Items.Item("vmlDrawing1.vml.rels").Path
UnZipeur.Namespace(DestFolderimage & "\").CopyHere (drawing1VMLREL)
drawing1VMLREL = DestFolderimage & "\vmlDrawing1.vml.rels"
drawing1VML = UnZipeur.Namespace(sourceZip & "\xl\drawings").Items.Item("vmlDrawing1.vml").Path
UnZipeur.Namespace(DestFolderimage & "\").CopyHere (drawing1VML)
drawing1VML = DestFolderimage & "\vmlDrawing1.vml"
Set UnZipeur = Nothing
Kill sourceZip
'fin de dézippage les VML compris
'----------------------------------------------------------
'copier les images
'fonction et macro @jurassic pork
Dim DictShp As Object, DictFile As Object, Ccom As Object, Cel, Fname$
Set DictShp = CreateObject("Scripting.Dictionary")
Set DictFile = CreateObject("Scripting.Dictionary")
ChargerDicts drawing1VML, drawing1VMLREL, DictShp, DictFile
For Each Cel In [Tbl_INVENDUS[Désignation]]
Set Ccom = Cel.Comment
Select Case True
Case Ccom Is Nothing
Case Not Ccom.Shape.Fill.Type = msoFillPicture
Case Else
Fname = DictFile(DictShp(CStr(Ccom.Shape.id)))
FileCopy DestFolderimage & "\" & Fname, _
DestFolderimage & "\" & Cel & ".jpg"
End Select
Next
'Kill DestFolderimage & "\image*.*"
'Kill drawing1VMLREL
'Kill drawing1VML
Set DictShp = Nothing: Set DictFile = Nothing
MsgBox "Extraction des images de commentaires terminées" & vbCrLf
End Sub
Sub ChargerDicts(vmlFile, vmlrelFile, DictShp, DictFile)
'@jurassic pork
Dim fileName$, objName, relId$, shapeId$, XmlNamespaces$, res
Dim fso As Object, xmlDoc As Object, nodes As Object, node As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
res = xmlDoc.LoadXML(GetavailableXmlCode(vmlFile))
If Not res Then MsgBox ("erreur Lecture fichier xml : " & vmlFile): End
Set nodes = xmlDoc.getElementsByTagName("v:shape")
For Each node In nodes
shapeId = Right(node.getAttribute("id"), 4)
relId = node.ChildNodes(0).getAttribute("o:relid")
'Debug.Print shapeId,relId
DictShp.Add shapeId, relId
Next
res = xmlDoc.Load(vmlrelFile)
If Not res Then MsgBox ("erreur Lecture fichier xml : " & vmlrelFile): End
Set nodes = xmlDoc.SelectNodes("//Relationship")
For Each node In nodes
fileName = fso.GetFileName(node.getAttribute("Target"))
relId = node.getAttribute("Id")
'Debug.Print relId, fileName
DictFile.Add relId, fileName
Next
Set fso = Nothing: Set nodes = Nothing: Set xmlDoc = Nothing
End Sub
Function GetavailableXmlCode(vml)
'issue de la fonction de @laurent950
Dim x&, lines$, regEx
x = FreeFile: Open vml For Input As #x: lines = Input$(LOF(x), #x): Close #x
Set regEx = CreateObject("VBScript.RegExp")
' Configurer la regex
' Cette expression recherche une première occurrence de o:relid="rIdX" suivie d'une ou plusieurs autres occurrences
' et remplace tout par la première occurrence.
regEx.Pattern = "(o:relid=""rId\d+"")(\s+o:relid=""rId\d+"")+"
regEx.Global = True
regEx.IgnoreCase = True
' Remplacer toutes les occurrences répétées par une seule
lines = regEx.Replace(lines, "$1")
GetavailableXmlCode = lines
'Debug.Print lines
End Function
Sub testxx()
Debug.Print GetavailableXmlCode("C:\Users\patricktoulon\Desktop\media\vmlDrawing1.vml")
End Sub