this is my variables and my input and result excel file are in different workbooks and I want to show the output workbook only. I want to show Targetworkboo1 in output but with this code, sourceworkbook and Targetworkbook1 will be shown in output. how should I do it?
Excel.Application xlApp = new Excel.Application();
Excel.Workbook SourceWorkbook;
xlApp.Visible = true;
Excel.Workbook TargetWorkbook1;
Excel.Worksheet SourceWorksheet;
Excel.Worksheet TargetWorksheet1;
SourceWorksheet = SourceWorkbook.Worksheets[1];
TargetWorkbook1 = xlApp.Workbooks.Add();
TargetWorksheet1 = TargetWorkbook1.Worksheets[1];
Hide any sheets in excel workbook using this code:
SourceWorksheet.Visible=Excel.XlSheetVisibility.xlSheetHidden;
Hide the window of the workbook:
SourceWorkbook.Windows[1].Visible = false;
Related
How do I change the view style of Excel Worksheet In Excel interop to a Normal style?
Like this:
Just have no idea :(
Does someone know?
i see just one solution, you have to use ActiveWindow.View: a sample to use it
using Excel = Microsoft.Office.Interop.Excel;
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbooks wbs = excel.Workbooks;
Excel.Workbook wb = wbs.Open(#"d:\test.xlsm");
Excel.Worksheet sheet = wb.ActiveSheet;
// set the view style
excel.ActiveWindow.View = XlWindowView.xlNormalView;
object filename = #"d:\test1.xlsm";
wb.SaveAs(filename);
wbs.Close();
excel.Quit();
if you have more Worksheets, you have to do that on each Worksheet...
I started creating an Excel-Add-IN with C#.
what I need to do is simple, I need to set a workbook to a variable, the workbook is already running, I tried this but did not work
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
wb.SaveAs("C:\\Users\\ro_sg\\Desktop\\Pasta1.xlsx");
Excel.Worksheet ws = wb.Worksheets["Plan1"];
Excel.Range range = ws.Range["A1"];
range.Value = "Success";
wb.Save();
The wb variable cannot find the workbook (gets null), and I can't see why.
Please, if any of you spot the mistake let me know.
Thanks!
I believe your issue is it may not be finding the active Excel application upstream from when you set your workbook variable. It appears that your code is trying to create a new excel application (without a workbook) rather than get the existing one that is open.
Give this a try:
Excel.Application excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
Excel.Workbook wb = (Excel.Workbook)excel.ActiveWorkbook;
wb.SaveAs("D:\\WeeeDueceDuece.xlsx");
I don't know if you need to get a specific Sheet but if you try this: Excel.Worksheet ws = wb.Worksheets[1]; It will get the first Sheet of your Workbook
If your actual code doesn't have more in-between steps, it will always fail. I'm surprised this line didn't error:
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
It's because a new instance of Excel does not necessarily create a new workbook. You can check this with the following lines:
Excel.Application application = new Excel.Application();
Excel.Workbooks workbooks = application.Workbooks;
Console.WriteLine(workbooks.Count); // "0"
The new workbook, however, should create the default number of worksheets (usually 3, but editable).
I have a macro-enabled Excel file "D:\MyTests\ExcelTests\template.xlsm" with no data in it, only the VBA code, and my C# code needs to output a workbook data over there. Normally I output workbook data like this:
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Sheets[1];
Excel.Worksheet worksheet2 = workbook.Sheets[2];
// populate worksheets with some data
DataTable2Worksheet(tableMain, worksheet, verSize);
DataTable2Worksheet(tableExtra, worksheet2, 0);
string fileName = #"D:\MyTests\ExcelTests\newFile";
if (File.Exists(fileName ))
{
File.Delete(fileName );
}
workbook.SaveAs(fileName);
workbook.Close();
Marshal.ReleaseComObject(application);
but this creates a new file (which cannot be macros enabled programmatically). If I want to output the workbook to existing file
string existingFile = #"D:\MyTests\ExcelTests\template.xlsm"
the method
workbook.SaveAs(existingFile );
won't work. So, what should I do instead? Thanks.
Save the file specifically in xlOpenXMLWorkbookMacroEnabled format:
string existingFile = #"D:\MyTests\ExcelTests\template.xlsm"
workbook.SaveAs(existingFile, 52);
I am opening an Excel file, and if it does not meet certain conditions, I am then creating a new workbook. Problem I am having is that when the new workbook is created it is not by default set to the activeworkbookso when I try to use activeworkbook.saveas()it saves the 1st workbook, with inaccurate data.
This is what I am using to create the new workbook:
Excel.Workbook newWorkbook = this.Application.Workbooks.Add();
Can someone show me how to change activeworkbook in Excel when multiple workbooks are opened?
BTW this is Excel 2007 I am working with.
Copy worksheet to new workbook
ws = wb.Sheets["Mitchell"];
((Excel.Worksheet)ws).Copy();
Configuration Details:
Code is written in vb.net using Visual Studio 2010 Professional
All example code is placed in the ThisAddIn_Startup event handler of the
Application-Level Add-In for Excel 2010
In project references (right click project > properties >
references) make sure there is a check beside Excel = Microsoft.Office.Interop.Excel
When multiple worksheets are open, I use For Each to loop through the collection and see the names...
For Each wrkbk As Excel.Workbook In Me.Application.Workbooks
MsgBox(wrkbk.Name)
Next
Combine this with a Select Case and this allows you get at any workbook that is open.
For Each wrkbk As Excel.Workbook In Me.Application.Workbooks
Select Case wrkbk.Name
Case "Book1"
wrkbk.Activate()
Case "Book2"
Case Else
End Select
Next
Now, tying it all together, you can do this:
Create a new workbook named CustomWorkbook.xlsx
Activate Book1, copy Sheet1, and paste it into CustomWorkbook.xlsx (rename sheet to CopiedWorksheet)
Activate CustomWorkbook.xlxs
//Hide alerts so no prompts display when using SaveAs to rename new workbook
Me.Application.DisplayAlerts = False
//Programatically create a new workbook
Dim myWrkbk As Excel.Workbook
myWrkbk = Me.Application.Workbooks.Add()
//Name new workbook
myWrkbk.SaveAs("CustomWorkbook")
//Set Book1 as the active workbook (so can copy sheet)
Application.Workbooks("Book1").Activate()
//Create a new worksheet variable
Dim sourceWrksht As Excel.Worksheet
sourceWrksht = Application.Worksheets("Sheet1")
sourceWrksht.Range("A1").Value = "Text in a Cell"
//Select Sheet1 and copy into CustomWorkbook and paste as first sheet
sourceWrksht.Select()
sourceWrksht.Copy(Before:=Application.Workbooks("CustomWorkbook.xlsx").Sheets(1))
//Rename copied worksheet
Dim destinationWrksht As Excel.Worksheet
destinationWrksht = Application.Workbooks("CustomWorkbook.xlsx").Sheets(1)
destinationWrksht.Name = "CopiedWorksheet"
//Loop through all open workbooks in Excel Application
For Each wrkbk As Excel.Workbook In Me.Application.Workbooks
Select Case wrkbk.Name
Case "CustomWorkbook.xlsx"
wrkbk.Activate()
Case "Book1"
Case Else
End Select
Next
//Set Excel to show alerts again
Me.Application.DisplayAlerts = True
Simply call the Activate() method on the workbook you want to show.
When you create a new workbook you could use this:
Excel.Wokrbook oldWorkbook = this.Application.ActiveWorkbook;
Excel.Workbook newWorkbook = this.Application.Workbooks.Add();
oldWorkbook.Activate();
And after a while:
newWorkbook.Activate();
See the docs at MSDN.
Except:
The Activate method of the Workbooks collection activates a Microsoft Office Excel workbook and selects the first sheet in the workbook.
EDIT:
This works for me:
Excel.Workbook oldWorkbook = this.Application.ActiveWorkbook;
this.Application.ActiveWorkbook.Sheets["name"].Copy();
Excel.Workbook newWorkbook = this.Application.ActiveWorkbook;
old = Book1
new = Book2
I am working on MS Excel 2013 generating report where all the worksheets in workbook should have freeze pane at column 6 and row 1. I have searched on Google but could not find any solution as for freezing the pane, workbook has to be active. I have tried a lot of things but no success. I will really appreciate if someone can help me.
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open("filelocation");
foreach (Excel.Worksheet ws in workbook.Worksheets)
{
ws.Application.ActiveWindow.SplitColumn = 6;
ws.Application.ActiveWindow.SplitRow = 1;
ws.Application.ActiveWindow.FreezePanes = true;
}
excel.Visible = true;
I Hope it help others. I have used ClosedXML Library for Excel and after creating each worksheet I used
worksheet.SheetView.Freeze(1,6);
This freezes the Row 1, Col 6. You can freeze any row/column.
Here is link to ClosedXML. It's widely supported and very good documentation.
To freeze the panes on each worksheet you need to modify your for loop to add a line to activate the current sheet prior to setting the other properties. Here is my solution:
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open("filelocation");
foreach (Excel.Worksheet ws in workbook.Worksheets)
{
ws.Activate();
ws.Application.ActiveWindow.SplitColumn = 6;
ws.Application.ActiveWindow.SplitRow = 1;
ws.Application.ActiveWindow.FreezePanes = true;
}
excel.Visible = true;