Exports Ole2Storage to specific file.
Following code will extract all embedded XLS or DOC files to valid separated files. PPT files can't be extracted by this method.
Note that sometimes excel workbook can be hidden after extraction, so you will have to unhide it (View->Unhide).
[Visual Basic]
Private fileCounter As Integer
Private Sub ExtractAllEmbeddedFiles(ByVal fileName As String)
Dim ole2file As New Ole2CompoundFile
ole2file.Load(fileName, True)
ole2file.Root.VisitAll(New VisitDirectoryEntryHandler(AddressOf Me.ExtractToFileIfNeeded))
End Sub
Private Sub ExtractToFileIfNeeded(ByVal args As VisitDirectoryEntryArgs)
If (TypeOf args.CurrentEntry Is Ole2Stream AndAlso (args.Level > 1)) Then
If (args.CurrentEntry.Name = "WordDocument") Then
DirectCast(args.ParentEntry, Ole2Storage).Export(("embeddedWordDoc" & Me.fileCounter.ToString & ".doc"))
Me.fileCounter += 1
ElseIf (args.CurrentEntry.Name = "Workbook") Then
DirectCast(args.ParentEntry, Ole2Storage).Export(("embeddedExcelBook" & Me.fileCounter.ToString & ".xls"))
Me.fileCounter += 1
End If
End If
End Sub
[C#]
private int fileCounter = 0;
private void ExtractAllEmbeddedFiles(string fileName)
{
Ole2CompoundFile ole2file = new Ole2CompoundFile();
ole2file.Load(fileName, true);
ole2file.Root.VisitAll(new VisitDirectoryEntryHandler(this.ExtractToFileIfNeeded));
}
private void ExtractToFileIfNeeded(VisitDirectoryEntryArgs args)
{
if ((args.CurrentEntry is Ole2Stream) && (args.Level > 1))
{
if (args.CurrentEntry.Name == "WordDocument")
{
((Ole2Storage)args.ParentEntry).Export("embeddedWordDoc" + this.fileCounter.ToString() + ".doc");
this.fileCounter++;
}
else if (args.CurrentEntry.Name == "Workbook")
{
((Ole2Storage)args.ParentEntry).Export("embeddedExcelBook" + this.fileCounter.ToString() + ".xls");
this.fileCounter++;
}
}
}
Ole2Storage Class | GemBox.CompoundFile Namespace | Ole2Storage.Export Overload List