Office Automation: extract embedded Word Documents


September 18, 2008 - 14:09, by Steven Van de Craen - 4 Comments

I'm normally not into Office Automation but today I needed to extract all embedded files from a Word Document. Those files were Word, Excel and PDF documents. Luckily the majority were Word documents, because the quick solution I whipped up only works for those types, not for Excel or PDF.

Here's the VBA script that loops through the embedded objects, checks if it's a Word document and goes to save it in a new folder.

Sub ExtractFiles()
'
' ExtractFiles Macro
'
'
Dim shape As InlineShape
Dim folderName As String
Dim a As Document

folderName = Replace(ThisDocument.Name, ".", "_")
MkDir folderName

For Each shape In ThisDocument.InlineShapes
    If (shape.Type = wdInlineShapeEmbeddedOLEObject) And (InStr(LCase(shape.OLEFormat.IconLabel), ".doc") > 0) Then
            shape.OLEFormat.Object.SaveAs (folderName & "\" & shape.OLEFormat.IconLabel)
    End If
Next shape

End Sub