|
楼主 |
发表于 2018-1-14 21:50
|
显示全部楼层
以下代码来自本论坛:
Sub test()
Dim shp As InlineShape, pic
For Each shp In ActiveDocument.InlineShapes
shp.ScaleHeight = 100
shp.ScaleWidth = 100
shp.Select
Selection.Copy
pic = CliptoJPG(ThisDocument.Path & "\test.jpg")
Next shp
End Sub
'------------------------------------------------------------------------------------
Option Explicit
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Const CF_BITMAP = 2
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type
Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type
Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hPal As Long, BITMAP As Long) As Long
Sub test()
Select Case CliptoJPG("c:\test.jpg")
Case 0:
MsgBox "剪贴板图片已保存"
Case 1:
MsgBox "剪贴板图片保存失败"
Case 2:
MsgBox "剪贴板中无图片"
Case 3:
MsgBox "剪贴板无法打开,可能被其他程序所占用"
End Select
End Sub
Private Function CliptoJPG(ByVal destfilename As String, Optional ByVal quality As Byte = 100) As Integer
Dim tSI As GdiplusStartupInput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long
Dim hBmp As Long
If OpenClipboard(0) Then
hBmp = GetClipboardData(CF_BITMAP)
If hBmp = 0 Then
CliptoJPG = 2
CloseClipboard
Exit Function
End If
CloseClipboard
Else
CliptoJPG = 3
Exit Function
End If
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI, 0)
If lRes = 0 Then
lRes = GdipCreateBitmapFromHBITMAP(hBmp, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
tParams.Count = 1
With tParams.Parameter
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.type = 4
.Value = VarPtr(quality)
End With
lRes = GdipSaveImageToFile(lBitmap, StrPtr(destfilename), tJpgEncoder, tParams)
If lRes = 0 Then
CliptoJPG = 0
Else
CliptoJPG = 1
End If
GdipDisposeImage lBitmap
End If
GdiplusShutdown lGDIP
End If
End Function
Sub savejpg()
Dim m&, mc$, shp As Shape
Dim nm$, n&, myFolder$
Dim w, h, w1, h1, endn
myFolder = ThisWorkbook.Path & "\图片\"
If Len(Dir(myFolder, vbDirectory)) = 0 Then
MkDir myFolder
End If
For Each shp In ActiveSheet.Shapes
If shp.type = 13 Then
w = shp.Width
h = shp.Height
shp.ScaleHeight 1, True
shp.ScaleWidth 1, True
w1 = shp.Width
h1 = shp.Height
n = n + 1
m = shp.TopLeftCell.Row
mc = Cells(m, 2).Value
nm = mc & "-" & Format(n, "00") & ".jpg"
shp.Copy
endn = CliptoJPG(myFolder & nm)
shp.Width = w
shp.Height = h
End If
Next
End Sub
|
|