How to get Attachment in Excel sheet

Hi @asad.ali ,

       I have a sceanrio where my .xlsx excel sheet is having .msg as a atttachment, But the Issue is my code does not extract the child attachment. On contrary if i have .eml file with .xlsx attachment in it , it is extracted success fully. 

Can you share me the Code snippet of how to Extract xlsx file child Object ??

Currently i have below Code snippet which does not execute switch statement coz it comes out since This condition becomes true.

!oleObject.DisplayAsIcon

also note when i try to see Extension valeu of (oleObject.FileFormatType) it shows Ole10Native instead of msg .

public override List GetDirectChilds()
{
using (var workbook = LoadWorkbook())
{
var childs = new List();

            foreach (var worksheet in workbook.Worksheets)
            foreach (var oleObject in worksheet.OleObjects)
            {
                if (oleObject.IsLink || !oleObject.DisplayAsIcon)
                {
                    continue;
                }

                string extension;
                switch (oleObject.FileFormatType)
                {
                    case FileFormatType.MsEquation:
                        continue;
                    case FileFormatType.MapiMessage:
                        extension = ".msg";
                        break;
                    case FileFormatType.Unknown:
                        extension = Path.GetExtension(oleObject.ObjectSourceFullName);
                        break;
                    default:
                        extension = string.Format(".{0}", oleObject.FileFormatType.ToString().ToLower());

                        if (!IsSupported(extension))
                        {
                            throw new BadRequestException($"No converter found for file extension: {extension}");
                        }

                        break;
                }

                childs.Add(new DirectChild(extension, true, new MemoryStream(oleObject.ObjectData),
                    originalFileName: oleObject.ObjectSourceFullName));
            }

            return childs;
        }
    }\\\Kindly do not ask me to share the Whole code snippet as here i am not asking you for any fix and i am just asking to Provide me solution so that i can get msg extracted from the xlsx file .

Kindly you provide me the Code snippet where it will show Correctly that this extension is of type msg and not ole10native type

@GayatriNaik
1,If the type of embedded file is ole10native type,please check OleObject.SourceFullName property.
2,Could share your template file here? We will check it soon.

Hi I am Unable to add attachment here . But i have shared steps and how it looks like
Its a normal xlsx sheet containing

  1. text as Test data
  2. Embedded Test.msg file

In Second Pic , i have share how the Outlook msg looks like its Simplet email containing Test message . Thats it .

Embedded File Created 2.png (25.7 KB)

Embedded File Created.png (49.0 KB)

hO tHANKS FOR pROVIDING HINT , i AM NOW ABLE TO CONVERT AND GET THE ATTACHMENTS TOO But Just wondering What was the significance of 1) !oleObject.DisplayAsIcon
2) oleoleObject.FileFormatType which is used in switch statement in above code. Please reply yas i need to comment out this code and i need confiramtion before doing so.

@GayatriNaik,

DisplayAsIcon - This Boolean attributes is used to check whether the specified (OLE) object is displayed as an icon or not. You may also set the specified OLE object as an icon.

@GayatriNaik
MS Excel embed the msg attachment as Ole10Native file now,so MS Excel will call the corresponding program to open the file based on the file’s suffix name.
We do not support further detection the content types of Ole10Native object.
Please check it as the following:

OleObject oleObject = workbook.Worksheets[0].OleObjects[0];
byte[] data = oleObject.ObjectData;
if(oleObject.FileFormatType == FileFormatType.Ole10Native)
{
//1, check the file extension as MS Excel
    string fileName = oleObject.ObjectSourceFullName;
   string extension = Path.GetExtension(fileName);
    //2, detect the file type by the content with  FileFormatUtil.DetectFileFormat
   // FileFormatInfo fileFormatInfo = FileFormatUtil.DetectFileFormat(new MemoryStream(data));
}