Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Windows form Application, Facing this exception System.Runtime.InteropServices.COMException (0x800A03EC): Cannot insert object.

at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)

at Microsoft.Office.Interop.Excel.Shapes.AddOLEObject(Object ClassType, Object Filename, Object Link, Object DisplayAsIcon, Object IconFileName, Object IconIndex, Object IconLabel, Object Left, Object Top, Object Width, Object Height)

Using:
Office 365 and OS windows 10
Application:Windows Application
Target framework: .NET Framework 4.5
Reference: Microsoft.Office.Interop.Excel (version: 14.0.0.0)


I write a function in window application that add pdf file into Excel sheet.
But I got this error System.Runtime.InteropServices.COMException (0x800A03EC): Cannot insert object
at method AddOLEObject() .



C#
//Insert ISO_LAYOUT pdf-drawing as object
if (utilities.ctos(drCurrent["drawings"]) != "" && IncludeDetailAttachments)
{
Excel.Shape shape = wsCurrent.Shapes.AddOLEObject(
ClassType: Type.Missing,
Filename: copyToFile,
Link: false,
DisplayAsIcon: true,
IconFileName: "",
IconIndex: 0,
IconLabel: "",
Left: Type.Missing,
Top: Type.Missing,
Width: Type.Missing,
Height: Type.Missing);
shape.Fill.ForeColor.RGB = 500;
//shape.Fill.UserPicture(WSCommon.Properties.Settings.Default.surfaceDrawingPath + "Binders.png");
shape.Width = 15;
shape.Height = 17;
shape.Left = (float)iconCell.Left + (float)345;
shape.Top = (float)iconCell.Top + (float)1;
shape.Locked = false;
shape.Name = "Detail attachment " + drCurrent["lv1"].ToString() + drCurrent["lv2"].ToString() + drCurrent["lv3"].ToString() + drCurrent["lv4"].ToString() + drCurrent["lv5"].ToString() + drCurrent["lv6"].ToString();
Marshal.ReleaseComObject(shape);
}


What I have tried:

Online suggestion

like
- add/modify Excel workbook SaveAs() method,Interop.Excel.Shapes method AddOLEObject(),
- PROVIDE PERMISSION UNDER DCOMCNFG
- UPDATE version Microsoft.Office.Interop.Excel (latest to lowest one by one, execute with each)
Posted
Comments
Richard MacCutchan 8-Feb-21 9:07am    
Do PDF files support OLE? Try manually inserting it into a simple Excel file to check.
FullStackVale 10-Feb-21 5:39am    
Hi Richard, I tried it manually inserting it into a simple Excel file and it is working fine it.

But facing above mentioned exception while generating windows form application.
Richard MacCutchan 10-Feb-21 5:45am    
You need to check your parameters to see that they are all valid. Compare what you have with the documentation at Shapes.AddOLEObject method (Excel) | Microsoft Docs[^].

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