简体   繁体   中英

How to open embedded excel sheet in Powerpoint presentation using c#

I have a powerpoint slide in which an embedded excel object is present, which is embedded in the following way: insert-object-create from file-browse-display as icon. I'm trying to open this excel sheet programmatically using c#. The problem I'm facing is that I'm receiving the error: "Unhandled Exception: System.Runtime.InteropServices.COMException: OLEFormat (unknown member) : Invalid request. The window must be in slide or notes view. at Microsoft.Office.Interop.PowerPoint.OLEFormat.Activate()" I have tried changing the viewtype to slide as follows: presentation.Application.ActiveWindow.ViewType = PowerPoint.PpViewType.ppViewSlide; But this doesn't seem to help and I'm receiving the same error.

`

Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
                Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
                Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(@"D:\test.pptx");
foreach (var item in presentation.Slides[1].Shapes)
                    {
                        var shape = (PowerPoint.Shape)item;
if(shape.Name.Contains("Object")) {
 presentation.Application.ActiveWindow.ViewType = PowerPoint.PpViewType.ppViewSlide;
                presentation.Application.Activate();
                presentation.Application.ActiveWindow.Activate();
 shape.OLEFormat.Activate();
Microsoft.Office.Interop.Excel.Workbook wb = (Excel.Workbook)shape.OLEFormat.Object;
}
}`

This is the code which I'm using. Could anyone please help me on this. Thanks in advance!

It makes the job simple while working with clean presentations using Office.Interop lib. But it gets tricky when we try to access and edit OLEObjects in the presentation.

Try this!:

PowerPoint.OLEFormat ole = shape.OLEFormat;
ole.Object.Activate();
Excel.Workbook workbook = ole.Object;

this will create a workbook instance. Note: OLEFormat.Object is dynamic type, no need of type cast the object. but yes you need handle Error when the type cast fails.

Hope this helps.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM