Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to use a macro in c#

how to use a macro in c#

Scheduled Pinned Locked Moved C#
csharptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ajithevn
    wrote on last edited by
    #1

    how to use the same macro of excel in c# Private Sub Worksheet_Activate() Range("A1").Select End Sub

    P 2 Replies Last reply
    0
    • A Ajithevn

      how to use the same macro of excel in c# Private Sub Worksheet_Activate() Range("A1").Select End Sub

      P Offline
      P Offline
      Programm3r
      wrote on last edited by
      #2

      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: ^_^

      A 1 Reply Last reply
      0
      • P Programm3r

        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: ^_^

        A Offline
        A Offline
        Ajithevn
        wrote on last edited by
        #3

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

        P T 3 Replies Last reply
        0
        • A Ajithevn

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

          P Offline
          P Offline
          Programm3r
          wrote on last edited by
          #4

          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: ^_^

          1 Reply Last reply
          0
          • A Ajithevn

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

            P Offline
            P Offline
            Programm3r
            wrote on last edited by
            #5

            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: ^_^

            1 Reply Last reply
            0
            • A Ajithevn

              how to use the same macro of excel in c# Private Sub Worksheet_Activate() Range("A1").Select End Sub

              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #6

              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: ^_^

              1 Reply Last reply
              0
              • A Ajithevn

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

                T Offline
                T Offline
                thecodedemon
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups