How to open embedded excel sheet in Powerpoint presentation using c# - 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.

Related

C# vsto Powerpoint pastespecial fails when using ppPasteMetafilePicture

I'm trying to paste metapicture from clipboard (from excel) into a powerpoint slide using the code part below. It works fine when I use ppPasteDefault or ppPasteBitmap paste types but fails when using ppPasteMetafilePicture or prety much anything else.
I receive error; "Shapes (unknown member) : Invalid request. The specified data type is unavailable."
Bitmap image = new Bitmap(Clipboard.GetImage());
s.Application.Activate();
var p = s.Shapes.PasteSpecial(Microsoft.Office.Interop.PowerPoint.PpPasteDataType.ppPasteDefault, Microsoft.Office.Core.MsoTriState.msoFalse, "", 0, "", Microsoft.Office.Core.MsoTriState.msoFalse);
I found the reason after investigating for hours.
It seems what I copied into clipboard should be enhancedmetapicture instead of a bitmap.
So I changed my range copying excel part picture type from xlBitmap to xlPicture and then it worked.

Excel workbook import errors

I'm working on a Jeopardy application for some practice, and I'm having trouble understanding why I am getting this error. I've narrowed it down to what I believe the issue is, but for now, here's my code...
if (questionPath != "")
{
Excel.Application myapp = new Excel.Application();
Excel.Workbook wb = myapp.Workbooks.Open(questionPath);
Excel.Worksheet sheet = (Excel.Worksheet)wb.Worksheets.get_Item(1);
var cell = (Excel.Range)sheet.Cells[1, 1];
MessageBox.Show(cell.Value);
}
Basically, sets up a new Excel, pulls in the workbook, gets the first sheet, and then looks at whatever cell I tell it to look at and displays the value (like I said, just testing/playing around).
However, if the value in cell A1 is an integer, I get a "RuntimeBinderException was unhandled" error. It says that
"An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll
Additional information: The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments"
However if the value is a STRING, then it works fine. I've tried using Cell.ToString(), which obviously just gives the cell as a COM. I've tried using Cell.Value.ToString, which doesn't exist.... Looking at the intellisense it says that the value IS actually being read in (I used '11' as the value).
Also, I noticed that if I ran my code below, and then tried to open up the Excel to edit it, it tells me that the Excel is locked for editting by myself. I'm guessing I need to close the connection string, but I can't figure out where to do that.
Would I close myapp, or wb, or sheet? I tried a variety of _.Close()'s and other things I thought seemed obvious, but nada. Any tips on this as well?
Thanks everyone!
For anyone else reading this, 6 years later ...
I would recommend against using ToSting() if there is a possibility of null data.
Convert.ToString() handles null, while ToString() doesn't.
In the above scenario, this:
var temp = cell.Value.ToString();
Would be much safer being this:
var temp = Convert.ToString(cell.Value);
That way, it can deal with any null data, unlike ToString().
So I found out the reason it was doing that... it's pulling in an integer. Duh. I guess you can't use Cell.Value.ToString(); in the MessageBox.Show event, so here's what I ended up needing to do, and it worked perfectly.
if (questionPath != "")
{
Excel.Application myapp = new Excel.Application();
Excel.Workbook wb = myapp.Workbooks.Open(questionPath);
Excel.Worksheet sheet = (Excel.Worksheet)wb.Worksheets.get_Item(1);
var cell = (Excel.Range)sheet.Cells[1, 1];
var temp = cell.Value.ToString();
MessageBox.Show(temp);
}
Thanks for your help! I don't seem to be getting any issues with the Excel being locked to read-only, but I didn't change anything for that either... Assuming it was user error

How can I change the Row-Color of an excel-row with NetOffice?

I'm struggling with a simple problem, but I can`t figure it out.
I have an excel document, which I do some processing with (using the NetOffice API). This works fine, but I want to change the row-color after the processing, so each row within the range should have the same color after being processed.
Im getting a COMException (HRESULT: 0x800A03EC) with the following code:
foreach (Excel.Range row in rg)
{
//do the processing...
...
row.Interior.Color = XlRgbColor.rgbAliceBlue;
}
I also googled for this HRESULT and tried to solve the problem, within the
Open()-Method by setting readOnly to false and editable and corruptLoad to true. That didn`t
work. I have also tried to set the interactive property to true and saved the excel file in different formats (.xls, .xlsx) but nothing worked out.
I found that the excelfile/workbook is protected. So I tried to unprotect the ActiveWorkbook like so
app.ActiveWorkbook.Unprotect();
But this also went wrong and throws a COMException that the unprotect-property of the Workbook object can not be assigned.
I hope that someone can help me with that.
Thanks in advance,
Cordell
On Workbook open pass read only parameter to false and also pass password of excel file.
Excel.Workbook workBook = excelApplication.Workbooks.Open(sMyExcelPath,0,
False,5,123,123,True,XlPlatform.xlWindows,"\t",False,False,0,True,1,0)
You can change color of Row-Color of excel-row using below code.
Excel.Worksheet workSheet = workBook.Worksheets(1);
workSheet.Rows.Interior.Color = XlRgbColor.rgbAliceBlue;
To Set Border Try below codes:
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlDouble;
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).Weight = 4;
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).Color = ToDouble(Color.Black);
Above code change color of all rows of excel sheet. If you want to change only used range colors then try with below code.
workSheet.UsedRange.Interior.Color = XlRgbColor.rgbAliceBlue;
To set Border Try below Codes:
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlDouble;
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).Weight = 4;
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).Color = ToDouble(Color.Black);

Does ClosedXML support setting a worksheet's zoom level?

I am using closedXML to generate an Excel file from C# and I am trying to see if there is anyway to set the zoom level of a worksheet. I can't find this in any of the documentation or from googling?
Does anybody know how to do this?
It's now possible, starting with ClosedXML version 0.87.0, via the IXLSheetView.ZoomScale property.
using Excel = ClosedXML.Excel;
var wb = new Excel.XLWorkbook();
Excel.IXLWorksheet ws = wb.AddWorksheet("zoom");
Excel.IXLSheetView view = ws.SheetView;
/* Value can be set between 10 and 400 */
view.ZoomScale = 85;
You can check the IXLSheetView source code for more information.
Update for version 0.87+: https://stackoverflow.com/a/52755386/2610249
No, ClosedXML does not support setting the zoom. The option that johny links to is only for scaling of the pages when printing.
There is a feature request on the ClosedXML page, but no answer from the developer.
As previously answered, you can't, but I've found a way around it which works well:
Create a template Excel file in advance, with all sheets' zoom levels set to how you want them.
When you create your workbook, instead of:
public XLWorkbook CreateWorkbook()
{
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.AddWorksheet("First sheet");
// ...
return workbook;
}
do this:
public XLWorkbook CreateWorkbookWithZoom()
{
XLWorkbook workbook = new XLWorkbook(#"C:\your template file.xlsx");
IXLWorksheet worksheet = workbook.Worksheet(1);
worksheet.Name = "First sheet";
// ...
return workbook;
}
where C:\your template file.xlsx is the path of your template file.
I think you can also handle having a variable number of sheets, by copying existing (blank) worksheets instead of creating new ones. You can get creative with having different blank template worksheets to choose from, if you need to set the zoom level dynamically.
A pull request for this has been logged at https://github.com/ClosedXML/ClosedXML/pull/180

C# COM Interop Creating New Excel Workbook

I am using Microsoft.Office.Interop.Excel in a C# winform but I can't figure out how to create a new Excel workbook. I have tried everything in the documentation but I get nothing but errors. Workbook wb = new Workbook() compiles just fine but throws an incomprehensible runtime error.
You are probably getting the same error as you would get if you tried to instantiate a Workbook in Excel VBA using
Dim w As Workbook
Set w = New Workbook
Try instantiating the Application object, then call the Application.Workbooks.Add method. It takes an optional parameter, which is the name of a template.

Categories

Resources