unprotect excel workbook #2 - c#

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.

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?

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.

Exception while opening an Excel file

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);

how to identify that a worksheet is minimized?

How to identify the current active worksheet is minimized ?
I do not want to run my operation when the current worksheet in excel is minimized. I am using excel interop and C#
You can do a conditional check on the following property,
If Application.WindowState = xlMinimized Then
// do something
End If
This is in VBA though.
Here is a basic code in C#
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application myXL = new Excel.Application();
myXL.Visible = true;
//adding a new book
Excel.Workbook xlBookN = myXL.Workbooks.Add();
//opening exising book
String xlPath = "c:/myprojects/test.xls";
Excel.Workbook xlBookE = myXL.Workbooks.Open(xlPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Dim myWindowState As XlWindowState //you could also use a variable
myXL.WindowState = myWindowState
//or you can check the state directly
If (myXL.WindowState = xlMinimized)
// do something
End If
Please check on the syntax and references.
You could get better reference from :
MSDN
MSDN OFFICE INTEROP

C# Excel Interop: Opening and Showing CSV file

Hey I'm writing a wrapper for the excel interop, I want to be able to open a csv file in excel and show it to the user. I've got the basics down, but when i set visible to true and excel shows up, all columns are jammed into the first, and the separating commas are showing.
here's my helper.
public MyExcel(string filePath, bool readOnly)
{
_app = new Excel.Application();
_workbooks = _app.Workbooks;
_workbook = _workbooks.Open(_filepath, 0, _readOnly, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", !_readOnly, false, 0, true, true, true);
}
public void Show()
{
_app.Visible = true;
}
any suggestions?
When i open the file by double clicking Excel processes everything properly.
You will need to use the OpenText method, instead of Open, if you want Excel to parse for delimiters. Details: http://msdn.microsoft.com/en-us/library/bb223513%28v=office.12%29.aspx
An example in C#: http://msdn.microsoft.com/en-us/library/c9838808.aspx
It is MUCH easier than that if all you want to do is open the file...
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo("excel.exe", "output.csv");
proc.Start();

Categories