Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to delete specific image from excel file sheet , but i found nothing or most code i found doesn't match with my methode excel.Interop !

What I have tried:

 Excel.Application APPXL = new Excel.Application();      
    Excel.Workbook RapportXlWorkBook = APPXL.Workbooks.Open(excelfilePath)
    Excel.Worksheet RapportXlSheet = (Excel.Worksheet)RapportXlWorkBook.Worksheets[1];

/// remove code 
 //RapportXlSheet.Shapes(imageName).Delete !!! ?? 


I found only how to delete ALL shapes (pictures/images) in excel file , but not a specific shape !
foreach (Excel.Shape sh in RapportXlSheet.Shapes)
            {
                sh.Delete();
            }
Posted
Updated 6-Feb-18 22:02pm
v2

1 solution

Looks like you've got a VBA / VB.NET code sample, and you need to convert it to C#. Try:
C#
RapportXlSheet.Shapes[imageName].Delete();

EDIT: As discussed in the comments, the indexer isn't supported properly. You have to use the Item method instead:
C#
RapportXlSheet.Shapes.Item(imageName).Delete();
 
Share this answer
 
v2
Comments
Maciej Los 2-Feb-18 15:03pm    
5ed!
EM_Y 6-Feb-18 3:19am    
Thank you for your help and time , but it doesn't work ! :) @Richard Deeming
Richard Deeming 6-Feb-18 8:04am    
"It doesn't work" is not a description of a problem that anyone can help you with.
EM_Y 7-Feb-18 2:53am    
you are right ,
I'll try to explain better.

When I used
 RapportXlSheet.Shapes[imageName].Delete(); 


Error message shows : Cannot apply indexing with [] to an expression of type 'Microsoft.Office.Interop.Excel.Shapes' !

*I appreciate your attention :) .
Richard Deeming 7-Feb-18 10:35am    
OK, try:
RapportXlSheet.Shapes.Item(imageName).Delete();
Sometimes Interop can't handle the indexer properly, and exposes it as a method instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900