Quand vous écrivez un livre, votre éditeur vous demande de livrer un fichier Word avec les images dans des fichiers séparés. Comme je me voyais mal sauvegarder chaque image et que je suis un peu faignant comme tout bon programmeur, j'ai écrit une petite macro VBA.
Dans Word, les images alignées sur le texte peuvent être retrouvées via la propriété InlineShapes
du document. Je pensais qu'un logiciel aussi sophistiqué que Word me permettrait de sauvegarder directement l'image dans le fichier, mais ce n'est pas le cas. Il y a cependant une méthode CopyAsPicture
qui permet de copier l'image dans le presse-papier. Avec un peu de code récupéré sur StackOverflow, on peut récupérer le contenu du presse-papier et l'enregistrer au format .bmp. J'aurai préféré du .png, mais bon on peut facilement trouver des logiciels pour faire la conversion, comme l'excellent FastStone Photo Resizer
Les fichiers sont enregistrés dans C:\Temp\
avec un nom incrémenté automatiquement (001.bmp, 0002.bmp etc...). Vous pouvez modifier facilement cette partie pour utiliser votre propre schéma de nommage.
Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
(PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, _
IPic As IPicture) As Long
' Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
' Declare a UDT to store the bitmap information
Private Type uPicDesc
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type
Private Const CF_BITMAP = 2
Private Const PICTYPE_BITMAP = 1
Private Sub SaveClipboardToFile(FilePathName As String)
Dim IID_IDispatch As GUID
Dim uPicinfo As uPicDesc
Dim IPic As IPicture
Dim hPtr As Long
OpenClipboard 0
hPtr = GetClipboardData(CF_BITMAP)
CloseClipboard
' Create the interface GUID for the picture
With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
' Fill uPicInfo with necessary parts.
With uPicinfo
.Size = Len(uPicinfo) ' Length of structure.
.Type = PICTYPE_BITMAP ' Type of Picture
.hPic = hPtr ' Handle to image.
.hPal = 0 ' Handle to palette (if bitmap).
End With
' Create the Range Picture Object
OleCreatePictureIndirect uPicinfo, IID_IDispatch, True, IPic
' Save Picture Object
stdole.SavePicture IPic, FilePathName
End Sub
Public Sub SaveImages()
Dim ish As inlineShape
Dim i As Integer
i = 1
For Each ish In Application.ActiveDocument.InlineShapes
If ish.Type = wdInlineShapePicture Then
ish.Range.CopyAsPicture
SaveClipboardToFile "C:\Temp\" & Format(i, "000") & ".bmp"
i = i + 1
End If
Next ish
End Sub
Besoin d'un développeur ? Contactez nous
Réactivité, coûts modérés.
Maxence DELANNOY - maxence.delannoy@wiip.fr - Tél. : 09.70.46.32.55
|
Comments
Toe (not verified)
Thu, 26/10/2023 - 10:30
Permalink
Merci Maxence pour cette
Add new comment