'-------------------------------------------------------------------
'Extrait le PDF Acrobat en passant par un ZIP du Workbook et les BIN
'-------------------------------------------------------------------
Private Sub AcrobatPDFShow(Shape As Shape, pdfFileName As String)
Dim oShell As Object
Dim embbededFilesFolder As Object
Dim binItems As Object
Dim binItem As Object
Dim zipPath As String
Dim binPath As String
Dim pdfPath As String
Dim fileNum As Integer
Dim fileSize As Long
Dim Text As String
Dim startPos As Long
Dim endPos As Long
Dim ErrNumber As Long
'Shell Object
Set oShell = CreateObject("Shell.Application")
'Nom complet du fichier PDF à afficher
pdfPath = Environ("TEMP") & "\" & pdfFileName
On Error Resume Next
Kill pdfPath
ErrNumber = Err.Number
On Error GoTo 0
Select Case ErrNumber
'Pas d'erreur
Case 0
'OK
'Fichier introuvable
Case 53
'OK
'Permission refusée (File in use)
Case 70
GoTo OpenPDFFile
'Autre erreur ?
Case Else
MsgBox "Erreur #" & Err.Number & " " & Err.Description
Stop
End Select
Application.ScreenUpdating = False
'Ajout d'un nouveau classeur
Application.Workbooks.Add
Shape.Copy
ActiveSheet.Paste
'ZIP file full name en répertoire TEMP
zipPath = Environ("TEMP") & "\" & ActiveWorkbook.Name & ".zip"
'Kill existing zipPath
If Not Len(Dir(zipPath)) = 0 Then Kill zipPath
'Save the Workbook as ZIP zipPath file
ActiveWorkbook.SaveAs zipPath
ActiveWorkbook.Close savechanges:=False
'localisation des fichiers .bin du .zip
Set embbededFilesFolder = oShell.Namespace(zipPath & "\xl\embeddings")
'Set binItem = embbededFilesFolder.items(1) -> Erreur !
Set binItems = embbededFilesFolder.items
'Il n'y en a qu'un seul mais obligé de faire cette boucle
For Each binItem In binItems
binPath = Environ("TEMP") & "\" & binItem.Name
If Not Len(Dir(binPath)) = 0 Then Kill binPath
'Copy le .bin du ZIP dans le répertoire TEMP
oShell.Namespace(Environ("TEMP")).CopyHere binItem, 4
'Lecture du fichier .bin du répertoire TEMP
fileNum = FreeFile
Open binPath For Binary Access Read As #fileNum
fileSize = LOF(fileNum)
Text = String(fileSize + 1, Chr(0))
Get #fileNum, , Text
Close #fileNum
'Start of PDF
startPos = InStr(Text, "%PDF")
'End of PDF
endPos = InStrRev(Text, "%%EOF")
endPos = endPos + 4
'Extraction et écriture du fichier PDF
If startPos >= 0 And endPos > startPos Then
Text = Mid(Text, startPos, endPos - startPos + 1)
fileNum = FreeFile
Open pdfPath For Binary Access Write As #fileNum
Put #fileNum, , Text
Close #fileNum
End If
Next binItem
'Suppression du ZIP
If Not Len(Dir(zipPath)) = 0 Then Kill zipPath
OpenPDFFile:
'Affichage du fichier
oShell.Open (pdfPath)
End Sub