Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract embedded files from Word document 2

Status
Not open for further replies.

sglab

Technical User
Jul 29, 2003
104
US
Hello everyone,

I hope somebody could help me with my problem.
I need to be able to extract file(s) embedded in Word document or RTF file and displayed as icon, and save it to specified location.
This is a general idea. Embedded file(s) could be of diferrent formats: Microsoft Office suite files, Adobe, images and so on.
I alredy make it work with embedded Word documents, but having problems with PDFs and ZIPs, for example. So I need some generic way to do that.

Any help would be greatly appreciated.

Thank you in advance.
 
Is there anybody out there?

Guys, it's kind of an emergency.

Thanks.
 
Hi sglab,

What do you do for Word files - and what problems do you have using the same code for other files?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony,

Thanks for your response.
Below is a piece of code - just a few lines to try to get it work:
Code:
 Dim iShape As InlineShape
 Dim objType As String
 Dim emDoc As Object
 Dim icLab As String
 
  
 For Each iShape In ActiveDocument.InlineShapes
  icLab = Mid(iShape.OLEFormat.IconLabel, InStrRev (iShape.OLEFormat.IconLabel, "\") + 1)
  objType = iShape.OLEFormat.ClassType
  iShape.OLEFormat.ActivateAs (objType)
  iShape.OLEFormat.Open
  [red]Set emDoc = iShape.OLEFormat.Object[/red]
  emDoc.SaveAs "C:\" & icLab
  emDoc.Close
  Set emDoc = Nothing
      
 Next iShape


This code works for embedded Word docs because, I think the line in red returns object native to Word and SaveAs method is native to it to. But when it comes to, say PDF file, which was even inserted as a package and not as OLEObject, I don't seem to find any way to work with it.
The same red line returns an error.

Thanks.
 
Hi sglab,

Yes, you are more or less correct.

You cannot do it generically the way you are doing it (I think you'ld be better off using DoVerb rather than ActivateAs and Open - but it won't help the generic case). What you are doing relies on you knowing how to interact with the particular class which may be OK if you only have a couple of types and know what they are but in general, unless (a) the Class supports Automation and (b) you know the commands to issue this can't work.

You could copy the object and paste it in the folder you want it in (not that I know the code for that off the top of my head but PHV will if he's around) but that will only work for some objects - others I think rely totally on you knowing what they are and interacting with them through the appropriat eapplication which, as I said, may not be possible. If all your objects have been created from files (rather than as new and possibly not proper file types - for example, Excel Worksheet) then you might be able to do it this way - if so, come back and I'll try and root out the script that's needed.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony,

[blue]>>>You cannot do it generically the way you are doing it...
unless (a) the Class supports Automation and (b) you know the commands to issue[/blue]

Well, I totally understand that and that's why I posted my question. Opening these files and re-saving them was never my choice. It's just I can't find in tons of Office objects and methods the one that could help me do what you said later:

[blue]>>>You could copy the object and paste it in the folder you want it in [/blue]

I hope of course, that you'll think of something and somebody like PHV will join discussion. So for now, thanks a lot for your help and I go back and try to knock this thing out.

Best regards.
 
As a cheat, perhaps you could create a new word document, embed the object in the new (otherwise empty) document, and save the new document with the embedded object.

That way you don't have to worry about what the object's native application is. It also gets around the "scrap" object problem that you will have with .pdf files otherwise.
 
Hi mintjulep,

Thanks for your input.
Maybe I just didn't fully understand what you were trying to say, but I do need to extract these native embedded files. Not to create another word document with embedded objects.
Could you come up with a few lines of code to illustrate this approach?
And also I need to make it efficient since I'm talking about quite a few such documents with embedded files. So I think speed is also an issue here.

Thanks.
 
sglab,

If you need to save the embedded file object as a file with the proper extension of the object's native application, then my suggested cheat won't work for you.

I was playing around with a similar need some time ago, but got main tracked back to my real job, and never got anthing working.

One problem I had was with .pdf files. Attempting to extract an embedded .pdf resulted in a "scrap" object, and even though this could be saved with the .pdf extension, Adobe failed to recognize it. You may run into similar problems with other types.
 
mintjulep,

Yes, I do need to have these embedded files saved in their native format, which could be anything. So the approach has to be as generic as possible.
Basically we're processing Outlook mailboxes where emails could have attachments, which in turn could have embedded files; and we need to be able to catch these and process them as well.
As far as "scrap" objects I've encountered them, too. Have no idea what to do with them.

So, I guess I'm back to square one.

Thank you.

Best regards.
 
I hope someone comes up with something, because I do not think this is fully possible. And I would like to be wrong.

Gerry
 
One generic way may be putting the object in Clibpoard, using API to find the type and Pate it (not so easy)...
The scrap (.shs) problem must be solved, too.

Fane Duru
 
A collegue of mine has also looked into this, and concluded that it is not possible.

I think my suggested cheat may be the best work-around. You could add:

Code:
public sub autoopen()
activedocument.inlineshapes.item (1).activate
end sub

to the new container document, which would open the embedded object when the document opens.
 
Thank you guys for your replies.

Whoa, I'm even rhyming. I think this is consequences of my battle with this task.

FaneDuru, could you illustrate your idea by pasting a few lines. I really don't know how embedded file can be put on Clipboard. As far as I remember from previous experiments with VBA, it (VBA) has access to Clipboard only through DataObject, which only supports text format. So may be it should be done in VB and not from within Word? What API are you referring to? Also, how Scrap object could be taken care of?

mintjulep, you're right Activate method does open embebbed file in proper application, at least I know it's true for DOCs, PDFs and ZIPs, but the problem for me is that I don't know how to manipulate them - save with the correct file name (the one that's shown in the icon label) when IconLabel property doesn't return the file's name especially when ClassType is Package; close application they were opened with. And again, why would I need to create a new container document since I already have one?

Thank you.
 
sglab,

I'm guilty of projecting my needs on your unknown to me application.

In my case various submittals come in embedded in a word doc. The word doc is the transmittal letter. We want to file each submitted item separately, so the new container document works for us.
 
In order to put the object in Clipboard:
Code:
ActiveDocument.InlineShapes(1).Select
Selection.Copy

If you paste now in a folder (manualy) you will save the object (like it is - for pictures). For documents you will obtain so named scrap document (.shs).

There are APIs able do paste from Clipboard (finding previously the object format...).
For instance:
Code:
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" _
    (ByVal wFormat As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias _
    "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, _
    ByVal hMem As Long) As Long
Using such API may be it is possible to paste...

Fane Duru

 
mintjulep and FaneDuru,

I have to apologize for delayed response: had a knee surgery on Monday and wasn't really up to doing anything. Just decided to check if there was any progress in the matter.
Thank you guys for trying to help. I will definitely continue to try to find the solution, using your input or something else. Just not now.

Best regards.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top