how to use a macro in c#
-
how to use the same macro of excel in c# Private Sub Worksheet_Activate() Range("A1").Select End Sub
Hi, See How To Create an Excel Macro by Using Automation from Visual C# .NET[^] ... hope I understood the question correctly.... Kind regards,
The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
-
Hi, See How To Create an Excel Macro by Using Automation from Visual C# .NET[^] ... hope I understood the question correctly.... Kind regards,
The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
the problem is im having a xls file and im opening it using workbook.open() and i select cell A10 and close it next time when i open the file i want the focus back on A1. i.e always the file is open the focus should be on cell A1 for that i used the macro which i stated in the previos thread. but i feel it would be better if i can do that also in the application. below is the code private void button1_Click(object sender, EventArgs e) { openexcel(); } private static void openexcel() { Excel.Application xlApplication = new Excel.Application(); Excel.Workbooks xlBooks; Excel.Worksheet xls; xlBooks = xlApplication.Workbooks; xlBooks.Open(Application.StartupPath + @"\ExcelFile01.xls", Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xls = xlApplication.ActiveWorkbook.Worksheets[2] as Excel.Worksheet; xls.Visible = Excel.XlSheetVisibility.xlSheetHidden; xls = xlApplication.ActiveWorkbook.ActiveSheet as Excel.Worksheet; xls.Rows.AutoFit(); //todo focus back to cell A1 xlApplication.Visible = true; System.Runtime.InteropServices.Marshal.ReleaseComObject(xls); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication); }
-
the problem is im having a xls file and im opening it using workbook.open() and i select cell A10 and close it next time when i open the file i want the focus back on A1. i.e always the file is open the focus should be on cell A1 for that i used the macro which i stated in the previos thread. but i feel it would be better if i can do that also in the application. below is the code private void button1_Click(object sender, EventArgs e) { openexcel(); } private static void openexcel() { Excel.Application xlApplication = new Excel.Application(); Excel.Workbooks xlBooks; Excel.Worksheet xls; xlBooks = xlApplication.Workbooks; xlBooks.Open(Application.StartupPath + @"\ExcelFile01.xls", Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xls = xlApplication.ActiveWorkbook.Worksheets[2] as Excel.Worksheet; xls.Visible = Excel.XlSheetVisibility.xlSheetHidden; xls = xlApplication.ActiveWorkbook.ActiveSheet as Excel.Worksheet; xls.Rows.AutoFit(); //todo focus back to cell A1 xlApplication.Visible = true; System.Runtime.InteropServices.Marshal.ReleaseComObject(xls); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication); }
Hi, Wouldn't something like
Cells(YourRow,YourColumn).Select
work? Kind regards,The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
-
the problem is im having a xls file and im opening it using workbook.open() and i select cell A10 and close it next time when i open the file i want the focus back on A1. i.e always the file is open the focus should be on cell A1 for that i used the macro which i stated in the previos thread. but i feel it would be better if i can do that also in the application. below is the code private void button1_Click(object sender, EventArgs e) { openexcel(); } private static void openexcel() { Excel.Application xlApplication = new Excel.Application(); Excel.Workbooks xlBooks; Excel.Worksheet xls; xlBooks = xlApplication.Workbooks; xlBooks.Open(Application.StartupPath + @"\ExcelFile01.xls", Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xls = xlApplication.ActiveWorkbook.Worksheets[2] as Excel.Worksheet; xls.Visible = Excel.XlSheetVisibility.xlSheetHidden; xls = xlApplication.ActiveWorkbook.ActiveSheet as Excel.Worksheet; xls.Rows.AutoFit(); //todo focus back to cell A1 xlApplication.Visible = true; System.Runtime.InteropServices.Marshal.ReleaseComObject(xls); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication); }
Have a look Developers Guide to the Excel 2007 Application Object[^], most of it is in VB.NET, but that's just syntax .... I'm sure that you'll find something useful there ....
The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
-
how to use the same macro of excel in c# Private Sub Worksheet_Activate() Range("A1").Select End Sub
This should do it: Range.Activate Method[^] (Activates a single cell, which must be inside the current selection. To select a range of cells, use the Select method.) Kind regards,
The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
-
the problem is im having a xls file and im opening it using workbook.open() and i select cell A10 and close it next time when i open the file i want the focus back on A1. i.e always the file is open the focus should be on cell A1 for that i used the macro which i stated in the previos thread. but i feel it would be better if i can do that also in the application. below is the code private void button1_Click(object sender, EventArgs e) { openexcel(); } private static void openexcel() { Excel.Application xlApplication = new Excel.Application(); Excel.Workbooks xlBooks; Excel.Worksheet xls; xlBooks = xlApplication.Workbooks; xlBooks.Open(Application.StartupPath + @"\ExcelFile01.xls", Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xls = xlApplication.ActiveWorkbook.Worksheets[2] as Excel.Worksheet; xls.Visible = Excel.XlSheetVisibility.xlSheetHidden; xls = xlApplication.ActiveWorkbook.ActiveSheet as Excel.Worksheet; xls.Rows.AutoFit(); //todo focus back to cell A1 xlApplication.Visible = true; System.Runtime.InteropServices.Marshal.ReleaseComObject(xls); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication); }
Is this going to be the same excel file you'll be opening every time? You could put an 'AutoOpen' macro in the excel file (which would run anytime the file is opened, just enable macros when you open it) to do all this preliminary stuff you're doing in C#. It would be much easier in VBA anyway.
The Code Demon Rises.