Office Automation: extract embedded Word Documents


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

 


Links to this post

Comments

Tuesday, 27 Jan 2015 12:49 by George M
hey. i m trying to find a solution for a VBA automatisation. i am working on a little VBA code that verifies if a folder exists in my sharepoint database and if not, automaticaly creates it. my problem is that i don t know how to link the content type metadata to the mkdir statement. My sharepoint database has several content types (range folder, project folder, etc.), along with other proprieties for the folders. Could you please help me with some code to create a folder with the content type "project folder" for example. Also, if possible, to integrate those other proprieties at the creation of the folder. i could n find a solution on the web for this issue i have. Thank you in advance for your answer!

Sunday, 1 Feb 2015 04:43 by Steven Van de Craen
Hi George, you better ask for help on the MSDN Forums or StackExchange on this issue.

Friday, 12 May 2017 10:11 by Paul
Hi we have software that embeds bmp files into a table in word. Sometimes there can be 20 or 30 pictures in a cell. Which is time consuming to view them. To overcome this we manually creating a photo album in powerpoint from the embeded images in our word document I would like to semi automate this process ie have a script or macro to do this. So i am looking at something like this - 1) User selects the images in word document document , right clicks images in our word document to bring up new menu option 'create photo album; 2) Your script or macro copies images selected from the word document into a newly created folder on users desktop computer (folder called 'photo albums' a subfolder is created to hold each new photo album and associated images to copied from word to facilitate the photo album) 3 Then powerpoint opens photo album wizard automatically selecting the newly created subfolder and displays images held in the subfolder copied from word. 4 Now the user selects all or some of the images , names photo album and clicks the create button ie uses photo album as normal. 5 On click create the photo album is saved back to the created subfolder which held the origional images Next Phase User then deletes pictures they highlighted before in the word document , right clicks in cell where pictures were and selects (new menu option) insert photo album which inserts the last created photo album in the cell as an object . Hope this makes sense

Sunday, 14 May 2017 06:50 by Steven Van de Craen
Hi Paul, sounds like a good scenario. Recently I implemented an object extract from Word documents using C# and Aspose Words. It ran "outside" Word so users could select multiple documents and then extract embedded files alongside. Cheers.

CAPTCHA Image Validation