word VBA保存文档中所有图片

Sub saveAllPic()
    
    
    On Error Resume Next
    
    Const adTypeBinary = 1
    '默认文本数据
    Const adTypeText = 2
    '指定保存到文件时不覆盖,只新建
    Const adSaveCreateNotExist = 1
    '指定保存到文件时覆盖原文件,没有则新建
    Const adSaveCreateOverWrite = 2
    
    Dim oStream As Object
    Dim arr() As Byte
    Set oStream = VBA.CreateObject("adodb.stream")
    i = 1
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oSP As Shape
    Dim sPath As String
    If oDoc.Saved Then   '如果文档已保存,就把图片存放到文档相同路径.
        sPath = oDoc.Path & "\" & oDoc.Name & "_shape_"
    Else
        MsgBox ("文档未保存,无法将图片保存到文档所在文件夹.请先保存文档!")
        Exit Sub
    End If
    
    
    
    Dim oInLineSp As InlineShape
    With oDoc
        For Each oSP In .Shapes
            oSP.Select
            arr = Word.Selection.EnhMetaFileBits
            With oStream
                .Open
                .Type = adTypeBinary
                .Write arr
                .SaveToFile sPath & i & ".emf", adSaveCreateOverWrite
                .Close
            End With
            i = i + 1
        Next
        For Each oInLineSp In .InlineShapes
            arr = oInLineSp.Range.EnhMetaFileBits
            With oStream
                .Open
                .Type = adTypeBinary
                .Write arr
                .SaveToFile sPath & i & ".emf", adSaveCreateOverWrite
                .Close
            End With
            i = i + 1
        Next
    End With
    Shell "explorer.exe " & oDoc.Path, vbMaximizedFocus
    
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夕乃何夕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值