1. 程式人生 > >wdVBA保存圖片到本地API

wdVBA保存圖片到本地API

cli fun tca val nts number psav vba bitmap

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
    SuppresexternalCodecs 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 GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hpal As Long, Bitmap 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 IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Const CF_BITMAP = 2
Public Sub SavePictures()
    Call ConvertShapeToInline
    Call SavePictureByApi
End Sub

Private Sub SavePictureByApi()
    Dim tSI As GdiplusStartupInput
    Dim lRes As Long
    Dim lGDIP As Long
    Dim lBitmap As Long
    Dim hBitmap As Long
    Dim FileName As String

    Dim inSh As InlineShape, N As Long
    N = 0


    ‘Documents.Open ("C:\Brildo\Test.docx")
    ReDim PicName(1 To 1) As String
    For Each inSh In ActiveDocument.InlineShapes
        ‘如果是shape,此處改為activedocument.Shapes,當然inSh的聲明也要改

        N = N + 1
        ReDim Preserve PicName(1 To N)
        PicName(N) = FirstName & Format(N, "0000") & ".jpg"

        Debug.Print N

        FileName = ActiveDocument.Path & "\" & PicName(N)

        If Dir(FileName) <> "" Then Kill FileName

        inSh.Range.CopyAsPicture
        ‘如果為shape,CopyAsPicture方法無效,可能因為shape本身就是pic,
        ‘所以對於shape直接select,再copy,即以圖片形式裝入了粘貼板,
        ‘後續操作相同。
        OpenClipboard 0&
        hBitmap = GetClipboardData(CF_BITMAP)
        CloseClipboard
        tSI.GdiplusVersion = 1
        lRes = GdiplusStartup(lGDIP, tSI, 0)
        If lRes = 0 Then
            lRes = GdipCreateBitmapFromHBITMAP(hBitmap, 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(100)
                End With
                lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileName), tJpgEncoder, tParams)
                GdipDisposeImage lBitmap
            End If
            GdiplusShutdown lGDIP
        End If
    Next inSh
End Sub
Private Sub ConvertShapeToInline()
    Dim shp As Shape
    Dim N As Long
    If ActiveDocument.Shapes.Count = 0 Then Exit Sub
    On Error Resume Next    ‘防止一些轉化失敗
    For Each shp In ActiveDocument.Shapes
        Debug.Print "正在轉換"; N
        N = N + 1
        shp.ConvertToInlineShape
    Next shp
    On Error GoTo 0
End Sub

  

wdVBA保存圖片到本地API