
'-------------------------------------------------------------------- ' 定義セクション '-------------------------------------------------------------------- Private Type FLTIMAGE StructSize As Integer Type As Byte Reserved1(0 To 8) As Byte hImage As Long Reserved3(0 To 19) As Byte End Type Private Type FLTFILE Reserved1 As Integer Ext As String * 4 Reserved2 As Integer Path As String * 260 Reserved3 As Currency End Type Private Declare Function GetFilterInfo Lib _ "C:\Program Files\Common Files\Microsoft Shared\Grphflt\JPEGIM32.FLT" _ (ByVal Ver As Integer, ByVal Reserved As Long, _ phMem As Long, ByVal flags As Long) As Long Private Declare Function ExportGr Lib "JPEGIM32.FLT" _ (ff As FLTFILE, fi As FLTIMAGE, ByVal hMem As Long) As Long Private Declare Function OpenClipboard Lib "user32" _ (ByVal hWndNewOwner As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long Private Declare Function GetClipboardData Lib "user32" _ (ByVal uFormat As Long) As Long Const CF_ENHMETAFILE = 14 Private Declare Function CopyEnhMetaFile Lib "gdi32" _ Alias "CopyEnhMetaFileA" _ (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long Private Declare Function DeleteEnhMetaFile Lib "gdi32" _ (ByVal hemf As Long) As Long Private Declare Function GlobalFree Lib "kernel32" _ (ByVal hMem As Long) As Long '-------------------------------------------------------------------- ' 関数名 : SaveClipToJpg ' 機能 : 指定図形をJPGファイルとして保存する ' 引数 : img : シェイプオブジェクト[I] ' path :格納先パス(ファイル名も含む) ' 戻り値 : 成否 ' 作成日 : 2008/03/08 ' 作成者 : lily(http://vbatips.blog37.fc2.com/) '-------------------------------------------------------------- Function SaveClipToJpg(img As Shape, Path As String) As Boolean Dim tFltImg As FLTIMAGE Dim tFltFile As FLTFILE Dim hemf As Long Dim hMem As Long SaveClipToJpg = False 'クリップボードにコピー img.CopyPicture 'Selection.CopyPicture If OpenClipboard(0) Then hemf = CopyEnhMetaFile( _ GetClipboardData(CF_ENHMETAFILE), _ vbNullString) CloseClipboard End If If hemf = 0 Then Exit Function ' パラメータ設定 tFltFile.Path = Path & vbNullChar With tFltImg .StructSize = LenB(tFltImg) .Type = 1 .hImage = hemf End With ' フィルタ呼び出し If GetFilterInfo(3, 0, hMem, &H10000) And &H10 Then If ExportGr(tFltFile, tFltImg, hMem) = 0 Then SaveClipToJpg = True End If End If If hMem Then GlobalFree hMem DeleteEnhMetaFile hemf End Function
Sub testsave() Dim bSaveFlg As Boolean Dim sShapeName As String Dim sSavePath As String sShapeName = Selection.Name sSavePath = "E:/test/shape.jpg" bSaveFlg = SaveClipToJpg(ActiveSheet.shapes(sShapeName), sSavePath) If bSaveFlg = True Then MsgBox "保存に成功しました" Else MsgBox "保存に失敗しました" End If End Sub
Author:lily
作業を楽にする為の苦労を惜しみません。![]()