Read Embedded OLE Object File Name from Word Document

I am able to not get the embedded OLE object file name for few files in the Word Document.'I am not able to get the File Name when shape.OleFormat.IconCaption is empty or null.

I came to know we can read from shape.ImageData.ImageBytes. Can any one guide me how to read the file name.Please help

public void Extract(string SourceFile, string DestinationFolder)
        {
            Document doc = new Document();
            License wordsLicense = new License();
            string licenseFileName = "Aspose.Total.Product.Family.lic";
            wordsLicense.SetLicense(licenseFileName);
            doc = new Document(SourceFile);
            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
            int i = 0; string FileName = string.Empty;
            foreach (Shape shape in shapes)
            {
                if (shape.OleFormat != null)
                {
                    if (!shape.OleFormat.IsLink)
                    {
                        // Get extension of th eOL object.
                        string ext = "object";
                        switch (shape.OleFormat.ProgId)
                        {
                            case "Excel.Sheet.8":
                                ext = "xls";
                                break;
                            case "Excel.Sheet.12":
                                ext = "xlsx";
                                break;
                            case "Excel.SheetMacroEnabled.12":
                                ext = ".xlsm";
                                break;
                            case "PowerPoint.Show.8":
                                ext = "ppt";
                                break;
                            case "PowerPoint.Show.12":
                                ext = "pptx";
                                break;
                            case "AcroExch.Document.7":
                                ext = "pdf";
                                break;
                            case "Word.Document.8":
                                ext = "doc";
                                break;
                            case "Word.Document.12":
                                ext = "docx";
                                break;
                            case "Visio.Drawing.11":
                                ext = "docx";
                                break;
                            default:
                                ext = "jpg";
                                break;
                        }
                        if (!string.IsNullOrEmpty(shape.OleFormat.IconCaption))
                        {
                            FileName = shape.OleFormat.IconCaption;
                            shape.OleFormat.Save(String.Format(DestinationFolder + "/{0}", FileName));
                        }
                        else
                        {
                            byte[] imageName = shape.ImageData.ImageBytes;
                            FileName = i.ToString();
                            shape.OleFormat.Save(String.Format(DestinationFolder + "/out_{0}.{1}", FileName, shape.OleFormat.SuggestedExtension));
                        }
                    }
                    else if (shape.OleFormat.IsLink)
                    {
                        string filePath = shape.OleFormat.SourceFullName;
                        string fileName = shape.OleFormat.IconCaption;
                        // FileStream file = File.Create(DestinationFolder + "/" + fileName);
                        // byte[] data = shape.OleFormat.Save(;
                        // file.Write(data, 0, data.Length);
                        // file.Close();
                    }
                    i++;
                }
            }
        }

@vjlourdu84,

Thanks for your inquiry. Please note you can extract embedded OLE object from the document using Aspose.Words but you cannot get original name of an embedded document by using Aspose.Words. You can get source file name if the OLE object is linked. Furthermore, you can get icon caption of OLE object by OleFormat.IconCaption property. In case of OLE object is not embedded as icon or caption, this property returns empty string.

However, if there is any difference in your requirement and my understanding then please share some more details of your requirement along with your sample document. We will further look into it and will guide you accordingly.

Best Regards,

ahmad,

I want to read all the embedded OLE objects from a word document and store it in a NAS share. There is a Web interface which displays all these embedded objects as attachments to the document.

I am curious is there a chance to display the same name from the word document as attachment name if the OLE object is not embedded as icon or link.

For ex: i don’t want to display Embedded Content1.docx,Embedded Content1.pptx which is a complete different name than the one that is inserted by the user.

When i am trying to save the document which is linked i am getting error.

Can you share some sample code for extracting the OLE Objects and storing it which is linked in the word document.Doc1.zip (626.5 KB)

@vjlourdu84

Thanks for sharing your source document. We have tested the scenario and noticed there is only one linked OLE object in your document. However, it is not being extracted as expected, so we have logged a ticket WORDSNET-15658 in our issue tracking system for further investigation. We will keep you updated about the issue resolution progress.

We are sorry for the inconvenience.

Best Regards,

tilal.ahmad
Thanks for the help. When ever a document is linked i am getting this shape.OleFormat.IsLink as true.
I am get to get the File Name as well. I am not able to use shape.OleFormat.Save() method and it is giving error stating that linked files cannot be saved.Doc1.zip (1.1 MB)

else if (shape.OleFormat.IsLink)
{
    Log.Debug("Embedded File:{0} found for the DocumentId:{1} is Linked", shape.OleFormat.IconCaption, DocumentId);
    string filePath = shape.OleFormat.SourceFullName;
    string fileName = shape.OleFormat.IconCaption;
}

I need to solve two problems

  1. How to save the Linked document inside another word document when (shape.OleFormat.IsLink) is true
  2. If the embedded document is OLE Object and it is embedded without providing IconCaption is there a way to get the actual file name.

Please find the attached document for your reference.

@vjlourdu84

Thanks for your inquiry. In reference to above logged issue please note in case of OLE object is not embedded as icon or caption couldn’t be retrieved and it returns empty string value for IconCaption. For the linked object you can get OleFormat.SourceFullName to get file name.

I am afraid Aspose.Words does not support to save linked object. Please check Exceptions in Save method.

As suggested above, I am afraid you can not get file name if IconCaption is empty.