Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sun Jan 30, 2011 4:04 pm

Hi, I have tons of word document with OLE object, someone is PDF and others are simple images.
Is there a way to extract OLE from documents and save in a path?
I see in your example and made some changes but ... I ask you HELP.
Thanks..
Flavio

Dim document As New Document("Sample.doc")
3: Dim index As Integer = 0
4:
5: 'Create a new queue.
6: Dim containers As New Queue(Of ICompositeObject)()
7:
8: 'Put the document objects in the queue.
9: containers.Enqueue(document)
10: While (containers.Count > 0)
11: Dim container As ICompositeObject = containers.Dequeue()
12: Dim docObjects As DocumentObjectCollection = container.ChildObjects
13: For Each docObject As DocumentObject In docObjects
14:
15: 'Judge the object type.
16: If (docObject.DocumentObjectType = DocumentObjectType.Picture) Then
17: Dim picture As DocOleObject = docObject
18:
19: 'Name the image.
20: Dim imageName As String = String.Format("OLE{0}.pdf", index) 'I know this is a PDF
21:
22: 'Find type
23: Dim ll As String = picture.ObjectType.ToString()
MsgBox(ll)
'Find storage name
ll = picture.OleStorageName.ToString
MsgBox(ll)
'Find packagefilename (if it's a package)
ll = picture.PackageFileName.ToString
MsgBox(ll)

'HOW CAN I SAVE ?

24: index = index + 1
25: Else
26: If TypeOf docObject Is ICompositeObject Then
27: containers.Enqueue(TryCast(docObject, ICompositeObject))
28: End If
29: End If
30: Next
31: End While

docwhosv
 
Posts: 2
Joined: Thu Aug 05, 2010 9:49 am

Mon Jan 31, 2011 5:46 am

Dear Flavio Balbo,

Thanks for your inquiry.
The DocumentObjectType of embedded OLE object is OleObject, so you could collect all OLE objects as:
Code: Select all
Document document = new Document(@"..\..\01-test.doc");

//document elements, each of them has child elements
Queue<ICompositeObject> nodes = new Queue<ICompositeObject>();
nodes.Enqueue(document);

//embedded objects list.
IList<DocOleObject> oleObjects = new List<DocOleObject>();

//traverse
while (nodes.Count > 0)
{
    ICompositeObject node = nodes.Dequeue();
    foreach (IDocumentObject child in node.ChildObjects)
    {
        if (child is ICompositeObject)
        {
            nodes.Enqueue(child as ICompositeObject);
        }
        else if (child.DocumentObjectType == DocumentObjectType.OleObject)
        {
            DocOleObject oleObject = child as DocOleObject;
            oleObjects.Add(oleObject);
        }
    }
}

The NativeData property of DocOleObject represents the data of the OLE object, you could save it to a file as:
Code: Select all
File.WriteAllBytes(fileName, oleObject.NativeData);

A full demo is attached, please check out. ExtractOLE.zip is for C# and ExtractOLE2.zip is for VB.NET
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc