Exception while opening an Excel file - c#

I have an Excel file which gives error while opening it manually:
excel found unreadable content in *****.xlsx. Do you want to recover the content of this workbook? If you trust this workbook click yes.
If I click yes, I can open it in usual way, but if I use:
Excel.Application oExcelApp;
Excel.Workbook excelWorkbook = oExcelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
I am getting a COMException:
Exception from HRESULT: 0x800A03EC
How to handle this exception. How can I make it open or show that error which is shown everytime I open it manually.
I am using MS Office 2010.

Try to change your 4th parameter type to XlFileFormat (enumeration) and choose the a fitting value (enum description). Maybe 5 (xlWK1 value in enum) is the wrong one...
for example
Excel.Application oExcelApp;
Excel.Workbook excelWorkbook = oExcelApp.Workbooks.Open(workbookPath, 0, false, Excel.XlFileFormat.xlWorkbookDefault, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

Related

How to open an Excel file in PrintPreview without suspending the code

I'm new to C#/OpenXML and could not find an answer to this. Apologies in advance if it's a stupid question...
Basically, I am writing an application that creates Excel files from an input string. This input string may contain information about multiple files that need to be created and opened in the print preview dialog simultaneously. However, using the following function, the code is suspended on the printpreview.show() method as it waits for the user to close the preview.
public static void ExcelOpen(string fileName)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, false, 0, false, false);
Excel.Worksheet ws = (Excel.Worksheet)excelWorkbook.Worksheets[1];
excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrintPreview].Show();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
return;
}
How can I avoid this and make sure the window stays opened in print preview but my program continues to run and create/display further files?

Saving Excel File as Read Only (Interop.Excel) C#

All,
I have the below code which opens a specified excel file deletes the first row and then saves it as a specified CSV.
However when it saves the CSV down it saves it in a read-only format. Can anyone advise how I would ensure the file is not saved down in read only format.
EDIT
I have tried;
To set the ReadOnly property to false.
I also am aware two instances of Excel may be opening which may cause the read only status from googling previous posts however including myApp.Quit()
I believe it would close all instances of excel.
public void DeleteRows(string OriginalFileName,String NewFileName)
{
Microsoft.Office.Interop.Excel.Application myApp;
Microsoft.Office.Interop.Excel.Workbook myWorkBook;
Microsoft.Office.Interop.Excel.Worksheet myWorkSheet;
Microsoft.Office.Interop.Excel.Range range;
myApp = new Microsoft.Office.Interop.Excel.Application();
myWorkBook = myApp.Workbooks.Open(OriginalFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets.get_Item(1);
range = (Microsoft.Office.Interop.Excel.Range)myWorkSheet.Application.Rows[1, Type.Missing];
range.Select();
range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
myApp.DisplayAlerts = false;
myWorkSheet.SaveAs(NewFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
myWorkBook.Close(true);
myApp.Quit();
}
}
}
Try to set ReadOnlyRecommended as false while saving the excel file.
The solution was to change myWorkBook.Close(true); to myWorkBook.Close(false);

Excel read-only popup occurs after save as script c#

My below code opens, deletes the first row in excel and then saves the file as a CSV.
However when the next user wishes to open the CSV they are presented with the screen shot below;
Can anyone advise how I would remove this from happening and the pop-up will not happen when the user next enters the file. (Enabling Editing).
I have figured it could be a setting when I am saving the file down however I cannot see anything to resolve this out after extensive stack overflow & Code project searching.
public void DeleteRows(string OriginalFileName,String NewFileName)
{
Microsoft.Office.Interop.Excel.Application myApp;
Microsoft.Office.Interop.Excel.Workbook myWorkBook;
Microsoft.Office.Interop.Excel.Worksheet myWorkSheet;
Microsoft.Office.Interop.Excel.Range range;
myApp = new Microsoft.Office.Interop.Excel.Application();
myWorkBook = myApp.Workbooks.Open(OriginalFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets.get_Item(1);
range = (Microsoft.Office.Interop.Excel.Range)myWorkSheet.Application.Rows[1, Type.Missing];
range.Select();
range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
myApp.DisplayAlerts = false;
myWorkSheet.SaveAs(NewFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, false, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
myWorkBook.Close(false);
myApp.Quit();
}
According to the docs, the true in:
myWorkSheet.SaveAs(NewFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing,
Type.Missing, true, (etc etc)
means:
ReadOnlyRecommended - True to display a message when the file is
opened, recommending that the file be opened as read-only.
I suspect you want that value to be false.

How to edit the data of an excel worksheet without affecting the other worksheets?

Suppose I have an excel file containing 4 sheets, Sheet 1, Sheet 2 and so on. I need to read data from a List object, truncate all the data of Sheet 1, and write the data from the List object into that Sheet 1, without affecting any other sheet..
This is what I have been trying..
string pathFileSource = "C:\\Temp\\Output.xls";
string pathFileDestination = "C:\\Temp\\Performance Testing.xls";
Excel.Application excel = new Excel.Application();
Excel.Workbook wbSource = excel.Workbooks.Open(pathFileSource, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Workbook wbDestination = excel.Workbooks.Open(pathFileDestination, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet WorksheetSource = wbSource.Sheets[1];
//Copy all range in this worksheet
WorksheetSource.UsedRange.Copy(Missing.Value);
Excel.Worksheet WorksheetDestination = wbDestination.Sheets[1];
// Select used Range, paste value only
WorksheetDestination.UsedRange.PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);
wbSource.Close();
wbDestination.Save();
wbDestination.Close();
//Quit application
excel.Quit();
Although I am getting alerts stating that the data has been added to Clipboard, the destination file is not getting updated with the correct data. Any pointers as to where I am going wrong?
I will be really grateful if someone can provide an actual working code, and not pseudocodes.
This is (default)normal mode of MS Excel to edit one worksheet at a time and separately or independently of other worksheets.
However, if you have created a Worksheet Group by selecting worksheet tabs, then first of all, you have to un-group the worksheets. Then you can handle every worksheet independently.
I hope this may help!
You can make a Group of worksheets by:
Press and hold CTRL key and click on worksheet tabs(left button click).
And un-group your worksheets by the same process again.

unprotect excel workbook #2

I have one excel which is password protected. i am trying to add one macro to this excel..
My code is
oBook = oExcel.Workbooks.Open(FileName, 0, false, 5, "xyz", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
//oModule = oMOD.VBComponents("ThisWorkbook");
oBook.Unprotect("xyz");
// Create a new VBA code module.
oModule =
oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
oModule.CodeModule.AddFromString(sCode);
When i run this i am getting this error "Can't perform operation since the project is protected."
Any help how to get rid of this error
I encountered a similar problem. The solution is not found, but found workaround:
try to switch Application's Visibility property
oBook = oExcel.Workbooks.Open(FileName, 0, false, 5, "xyz", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true);
oExcel.Visible= true;
oExcel.Visible = false;
oBook.Unprotect("xyz");
I really don`t know how it works (seems like a bug), but this simple two lines of code helps me to solve my problem.

Categories