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
精彩评论