use c# to open workbook with ribbon - c#

I am trying to write a wrapper that will open up an excel doc with macros, pass the password through, enable the macros, enable editing, and save it as a new excel sheet so that the end user can just start working with the template without all the hassle.
here is my code:
using Microsoft.Office.Interop.Excel;
string path = #"\\\\home\\trevor$\\My Documents\\New\\_Blank_1cert.xlsm";
string savepath = #"\\\\home\\trevor$\\My Documents\\New\\"+txtName.Text.ToString()+".xlsm";
Excel.Application templatem = new Excel.Application();
Excel.Workbook templatemwb;
templatemwb = templatem.Workbooks.Open(Filename: path, Password: "news", ReadOnly: false);
Excel.Worksheet worksheet = templatemwb.Sheets["Begin Entry"];
worksheet.Activate();
templatemwb.SaveAs(Filename: savepath, FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled);
Everything works as expected except that there is no ribbon at the top of the excel window. I need the ribbon there; so how do I show it?

Set the Visible property on your Application object, templatem.

Related

automatically enabling macros in excel c#

I am accessing data from an embedded excel sheet through c#, but in order for the script to read the excel sheet I must manually press "enable macros" every time. How can I write a script such that it automatically enables macros every time? I am opening various excels so cannot change the settings on the excel sheet itself.
I saw something regarding the use of VBA but am unsure what to do? Would using this reference (using VBA = Microsoft.Vbe.Interop) be helpful?
The code below simply opens the excel, but I still have to click enable macros.
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
wordApp = new Word.Application();
xlApp = new Excel.Application();
wordDoc = wordApp.Documents.Open(#"Y:DocumentName");
Word.InlineShapes ils = wordDoc.InlineShapes;
ils[1].OLEFormat.Activate();
xlWorkBook = ils[1].OLEFormat.Object;
Have you tried setting the DisplayAlerts property to false?
xlApp.DisplayAlerts = false;

Open EXCEL (.xlsx) with password in C#

I try to open password protected .xlsx files (Excel 2007 format) without typing the password manually. I have installed Excel 2003 and the Microsoft Office Compatibility Pack that converts the file when opening.
The following code works, but it prompt for the password.
Microsoft.Office.Interop.Excel.Application ExcelApp;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook;
ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Object pwd = "xxx";
Object MissingValue = System.Reflection.Missing.Value;
ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, MissingValue, MissingValue,pwd);
If I use the same code to open a .xls File (Excel 2003), it works without prompting for the password. Opening .xlsx files without password protection also works fine.
How is it possible to open password protected .xlsx files without prompting for the password with Excel 2003 and the Microsoft Office Compatibility Pack?
The trick from a similar problem changing the readonly argument (3rd) to true
ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, true, MissingValue,pwd);
does not work here.
This may be late, but for any future person with Interop, for me it worked like this:
To open to write
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: false, Password: "mypassword");
To open as read-only
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: true, Password: "mypassword");
The Answer by #J1mm1995 works when trying to open files as readonly, and fails to open some excel files when you want to open the excel file for modification(ReadOnly is set to false).
I understood that this was because the Workbooks.Open() method also expected you to specify the WritePassword. The following code worked for me:
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open(path, ReadOnly: false, Password: "mypassword", WriteResPassword: "mypassword");

How can I save an .xlsx file as Unicode text using interop?

I have to build a report making desktop application . I need it to be really user friendly, the reports are in Hebrew so it has to be Unicode and not just a regular .csv file or I get question marks.. I want to receive the file path of the report (like c:\folder\file.xlsx) and be able to save a copy of the original file in another location as a Unicode text file.. Help?
Same problem as this guy.
"I need to use the saveas() from Microsoft.office.interop.excel but I can't figure how to do it "
Add this:
using Excel = Microsoft.Office.Interop.Excel;
Then use this code:
Excel.Application app = new Excel.Application();
try
{
app.DisplayAlerts = false;
app.Visible = false;
Excel.Workbook book = app.Workbooks.Open("D:\\test.xlsx");
book.SaveAs(Filename: "D:\\test.txt", FileFormat: Excel.XlFileFormat.xlUnicodeText,
AccessMode: Excel.XlSaveAsAccessMode.xlNoChange,
ConflictResolution: Excel.XlSaveConflictResolution.xlLocalSessionChanges);
}
finally
{
app.Quit();
}
"D:\\test.xlsx" is your input excel filename and "D:\\test.txt" is your unicode text output filename.

Excel 2013 does not display default sheet after adding in C#

In Excel 2013 I can't add a Worksheet after creating the workbook. My C#-Program did this, but if I set the Application on Visible = true, there is no Worksheet in it. My Program can rename Sheets, copy Sheets, but can't Show me them.
After all is done in my program, it need to be saved, but it doesnt work, because there is nothing displayed what can be saved!
This is my code:
Excel.Application appversion = new Excel.Application();
appversion.Visible = true;
Excel.Workbook dataBook = excelApp.Workbooks.Add();
dataSheet = dataBook.Worksheets[1]; // <- this sheet is not visible
dataSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible; // does not work
dataSheet.Name = Seiten[0]; // but my program continues
After adding the workbook Excel starts with this:
workbook without sheet
But where is the default sheet? Without that, I can't save anything. In Excel 2010 all Sheets I need appear and I can see how my program works.

Open Excel workbook in background using interop

I'm wanting to import data from an Excel workbook without actually displaying the open workbook.
I could have sworn I had used the following code on a previous project and it had worked:
var excelApp = new Excel.Application { Visible = false };
var workbook = excelApp.Workbooks.Open(filePath);
Unfortunately when the workbook opens it is displayed to the user which is unnecessary for this application.
I'm using Microsoft Excel 15.0 Object Library on this project when previously I think it was version 12 or 13. Maybe this is the problem, or is my memory fading and the code is incorrect?
I know this is old but just in case anybody still needs this answer...
excelApp.Visible = false;
(excelApp being the name of the variable used for the excel application)

Categories

Resources