使用 VBA 从 Word 文档中提取图像

Extracting images from Word document using VBA

我需要遍历一些word文档,并从word文档中提取图像并将它们保存在一个单独的文件夹中。 我试过将它们保存为HTML文档的方法,但它不太适合我的要求。

现在,我使用 inlineshapes 对象遍历图像,然后将它们复制粘贴到发布者文档中,然后将它们另存为图像。但是,当我 运行 脚本时遇到运行时自动化错误。 为了使用 Publisher 运行时库,我尝试了早期和晚期绑定,但我都遇到了错误。

谁能告诉我问题出在哪里?另外,如果有人能解释为什么我会遇到这个错误,那就太好了。据我了解,这是由于内存分配,但我不确定。

这是我一直在处理的代码块(fp、dp 是文件夹路径,而文件名是单词文档名称。我在另一个循环遍历所有文件的子中调用这个子文件夹):

Sub test(ByVal fp As String, ByVal dp As String, ByVal filename As String)
Dim doc As Document
Dim pubdoc As New Publisher.Document
Dim shp As InlineShape
'Application.Screenupdating = False
'Dim pubdoc As Object
'Set pubdoc = CreateObject("Publisher.Document")
Set doc = Documents.Open(fp)
With doc
    i = .InlineShapes.Count
    Debug.Print i
End With
For j = 1 To i
    Set shp = doc.InlineShapes(j)
    shp.Select
    Selection.CopyAsPicture
    pubdoc.Pages(1).Shapes.Paste
    pubdoc.Pages(1).Shapes(1).SaveAsPicture (dp & Application.PathSeparator & j & ".jpg")
    pubdoc.Pages(1).Shapes(1).Delete
Next
doc.Close (wdDoNotSaveChanges)
pubdoc.Close
'Application.Screenupdating = True

End Sub

除此之外,如果有人有任何建议可以加快速度,我会洗耳恭听。提前致谢!

从原始源文档创建的过滤 HTML 文档中提取图片会更快。但是,您说这不适合您的需要,所以...这里是示例代码,可以在您的源文档中找到图片并将它们粘贴到第二个文档中。

此类代码的速度问题是由选择命令中的 CopyPicture 引起的,因此我建议改用范围。当然,无论如何,所需的 For/Next 循环都比较慢。

Sub CopyPasteAsPicture()
    Dim doc As Word.Document, iShp As Word.InlineShape, shp As Word.Shape
    Dim i As Integer, nDoc As Word.Document, rng As Word.Range
    Set doc = ActiveDocument
    
    If doc.Shapes.Count > 0 Then
        For i = 1 To doc.Shapes.Count
            Set shp = doc.Shapes(i)
            If shp.Type = msoLinkedPicture Or shp.Type = msoPicture Then
                'if you want only pictures extracted then you have
                'to specify the type
                shp.ConvertToInlineShape
                'if you want all extracted pictures to be in the sequence
                'they appear in the document then you have to convert
                'floating shapes to inline shapes
            End If
        Next
    End If
    
    If doc.Content.InlineShapes.Count > 0 Then
        Set nDoc = Word.Documents.Add
        Set rng = nDoc.Content
        For i = 1 To doc.Content.InlineShapes.Count
            doc.Content.InlineShapes(i).Range.CopyAsPicture
            rng.Paste
            rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            rng.Paragraphs.Add
            rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
        Next
    End If
End Sub

如果您想将所有形状(浮动或内联)作为图像文件放入文件夹,那么最好的方法是将源文档另存为过滤后的 HTML 文档。这是命令:

htmDoc.SaveAs2 文件名:=LGPWorking & strFileName,AddToRecentFiles:=False,文件格式:=Word.WdSaveFormat.wdFormatFilteredHTML

在上面,活动文档被分配给变量 htmDoc。我给这个新文档一个特定的名称和位置。它的输出不仅是 HTML 文件,而且是一个带有附加“_Files”标签的同名目录。在“x_Files”目录中是所有图像文件。

如果您只想从原始源文档中提取选择性图像,或者如果您想要从多个源文档中提取图像...那么您需要使用我共享的上述代码来仅放置您想要的图像将一个或多个源文档转换为新的 Word 文档,然后将该新文档另存为 Filtered HTML.

例程完成后,您可以杀死 HTML 文档,只保留 Files 目录。

只需在文件名末尾添加.zip,展开文件并在word/media文件夹中查找.所有文件都在那里,无需编程。

我不得不改变一些东西,但这将允许在 word 文档上保存单个图像并经过几个循环,然后在另一面变成 jpg,没有任何白色 space

filename = ActiveDocument.FullName
saveLocaton = "z:\temp\"
FolderName = "test"
On Error Resume Next
Kill "z:\temp\test_files\*"  'Delete all files
RmDir "z:\temp\test_files"  'Delete folder

ActiveDocument.SaveAs2 filename:="z:\temp\test.html", FileFormat:=wdFormatHTML

ActiveDocument.Close
Kill saveLocaton & FolderName & ".html"
Kill saveLocaton & FolderName & "_files\*.xml"
Kill saveLocaton & FolderName & "_files\*.html"
Kill saveLocaton & FolderName & "_files\*.thmx"

Name saveLocaton & FolderName & "_files\image00" & 1 & ".png" As saveLocaton & FolderName & "_files\" & test2 & "_00" & x & ".jpg"

Word.Application.Visible = 真 Word.Application.Activate