Hi! I have code to save images based on names from the column:
'http://www.ozgrid.com/forum/showthread.php?t=43380
'Many thanks to Jaafar on the Mr. Excel forum for this code.
'***********************************************************
'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 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
Const CF_BITMAP = 2
Const CF_PALETTE = 9
Const IMAGE_BITMAP = 0
Const LR_COPYRETURNORG = &H4
Const PICTYPE_BITMAP = 1
Dim strPictureFile As String
Sub SaveSelectionAsBMP()
Dim oImageIcon As CommandBarControl
Dim intFaceId As Integer
Dim IID_IDispatch As GUID
Dim uPicinfo As uPicDesc
Dim IPic As IPicture
Dim hPtr As Long
Dim FilePathName As Variant
For Each sh In ActiveSheet.Shapes
sh.Select
filename = Sheet1.Range("b" & sh.TopLeftCell.Row).Value ' sorry! change "a" to "b"
Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
strPictureFile = "C:\Pics\" & filename & ".jpg"
'strPictureFile = Application.GetSaveAsFilename("", "JPEG Files (*.jpeg), *.jpeg", , "Save as JPEG")
If strPictureFile = "False" Then Exit Sub
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 Picture Object
OleCreatePictureIndirect uPicinfo, IID_IDispatch, True, IPic
'Save Picture
Debug.Print strPictureFile
SavePicture IPic, strPictureFile
'fix the clipboard (it seems to go messed up)
Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
Next
End Sub
However, I don't know how to adjust it for it to save images.
I have an Excel file which includes pictures in column A and I would like to export them into several files as .jpg. The name of the file should be generated from text in column B.
Can someone help me with this?
Thanks a lot!
Bookmarks