开发者

Consolidate contents of embedded file into original document

I'm trying to write a VBA script that finds all embedded (.docx) files within a Word Document, and copies their contents into the parent document, replacing the embedded object with text. I am able to find the embedded objects using:

Selection.GoTo What:=wdGoToObject, Which:=wdGoToNex开发者_如何学Ct, Count:=1, Name:= _
    "Word.Document.12"

However it is unclear to me how to open this selected object, and -- more importantly -- interact with the opened file through the same script. Before I get too far ahead of myself, is this even possible?


This worked for me (lightly tested...)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜