将嵌入文件的内容合并为原始文档

时间:2011-07-25 17:37:47

标签: vba ms-word word-vba

我正在尝试编写一个VBA脚本,找到Word文档中的所有嵌入(.docx)文件,并将其内容复制到父文档中,用文本替换嵌入的对象。 我可以使用以下方法找到嵌入对象:

Selection.GoTo What:=wdGoToObject, Which:=wdGoToNext, Count:=1, Name:= _
    "Word.Document.12"

但是我不清楚如何打开这个选定的对象,更重要的是 - 通过相同的脚本与打开的文件进行交互。在我走得太远之前,这甚至可能吗?

1 个答案:

答案 0 :(得分:2)

这对我有用(经过轻微测试......)

Sub Tester()

    Dim cDocs As Collection
    Dim o As InlineShape

    Set cDocs = GetEmbeddedDocs(ActiveDocument)
    For Each o In cDocs
        o.OLEFormat.Open
        With ActiveDocument
            .Content.Copy
            .Close
        End With
        o.Select
        Selection.Paste
    Next o
End Sub


Function GetEmbeddedDocs(oDoc As Word.Document) As Collection
    Dim o As InlineShape
    Dim c As New Collection

    For Each o In oDoc.InlineShapes
        If o.Type = wdInlineShapeEmbeddedOLEObject Then
            If o.OLEFormat.ProgID Like "Word.Document.*" Then
                c.Add o
            End If
        End If
    Next o
    Set GetEmbeddedDocs = c
End Function