如何用vba把word中的图片导出为独立的图片格式文件?

在word中的图形对象有InlineShape对象和Shape对象,其中InlineShape对象表示的是嵌入到文本层的图片,就像是文字的一部分一样,而Shape对象是悬浮于文本之上位于图形层,两者有区别。

如果想要把所有的图形对象导出为独立的图片格式文件,可以借组EnhMetaFileBits属性。

EnhMetaFileBits属性返回的是把word中的选择区域当做是一个图片,然后返回该图片的增强型图元文件字节流。

它相当于API函数GetEnhMetaFileBits ,由于增强型图元文件的文件格式默认是emf,所以在将字节流保存为图片时,默认指定扩展名为emf。

以下代码可以实现将word中的各种图形导出为独立的图片格式的

vbnet 复制代码
Sub QQ527524938()
    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
    sPath = oDoc.Path & "\"
    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
End Sub

文件。

相关推荐
骆驼爱记录10 小时前
Word三线表制作全攻略
自动化·word·wps·新人首发
盘古工具1 天前
告别重复劳动:Word 重复上一次操作的两种方法
windows·word
好名字08212 天前
Vue2转Word方法(html-docx-js库)
javascript·html·word
檐下翻书1732 天前
企业组织架构图导出Word 在线编辑免费工具
人工智能·信息可视化·去中心化·word·流程图·ai编程
涛涛北京2 天前
【word】- 宏操作 - 如何在word中插入宏
word
涛涛北京2 天前
【word】- 宏操作 - 如何在word中批量修改图片格式
word
usdoc文档预览2 天前
Word文件转PDF、WPS在线打印、js提取Word文件内容、轻松将Word文档转为PDF
pdf·word·wps·在线预览·word转pdf·在线编辑·ofd在线预览解析
身如柳絮随风扬2 天前
Spring Boot文档导出(Word,Excel,PPT),导入(Excel)
spring boot·word·excel·ppt
赵丙双2 天前
python-docx 报错 KeyError: “There is no item named ‘NULL‘ in the archive“
python·word·docx·python-docx
GJ_WL2 天前
word&endNote X9参考文献编号([1] [10] [100])右括号对齐
word