How to call excel macros programmatically in C#?

Hi,
I have a requirement where i need to call excel (2003) macros in C# program. Can anyone help me with a code snippet to do the same?
The excel macro function takes two input parameters? how can the parameters be passed?
Any code snippet to do the same in C# would be helpful.
Thanks.

Hey there, Sid.  I am tryin gto run your code, but I couldn't even gte it to fire.  Here's what I ahve now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication2
    public partial class Form1 : Form
        private void button1_Click(object sender, EventArgs e)
            //~~> Define your Excel Objects
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook;
            //~~> Start Excel and open the workbook.
            xlWorkBook = xlApp.Workbooks.Open("C:\\Users\\Ryan\\Desktop\\Coding\\Microsoft Excel\\Work Samples\\Work Samples\\Historical Stock Prices.xlsb");
            //~~> Run the macros by supplying the necessary arguments
            xlApp.Run("ShowMsg", "Hello from C# Client", "Demo to run Excel macros from C#");
            //~~> Clean-up: Close the workbook
            xlWorkBook.Close(false);
            //~~> Quit the Excel Application
            xlApp.Quit();
            //~~> Clean Up
            releaseObject(xlApp);
            releaseObject(xlWorkBook);
        //~~> Release the objects
        private void releaseObject(object obj)
            try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            catch (Exception ex)
                obj = null;
            finally
                GC.Collect();
When I hit the play button nothing happens.  When I hot F5 nothing happens.  Do you ahve any idea what I'm doing wrong.  I'd appreciate any advice with this!! 
Thanks!!
Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

Similar Messages

  • How to call excel macros in ODI

    Hi gurus,
    I am new In ODI please help me with this. i want to store sheet name of excel file in a variable.I have written a macro function for this please help me how to call that macro function in ODI
    Thanks & regards

    this are my thoughts ,since you will have to deal with multiple files and read multiple sheets . While dealing with Excel ,we have to deal with DSN and so multiple Excel can a be a little complex.
    I think we can make use of your macro function by calling via VB and once you find the correct value write a in separate txt File. Now this File can be called in ODI and read and processed as you wish to .
    Step 1. Call your Macro Function via VB
    Step 2. Call this VB program via OS Command, which write to a separate file.
    Step 3. Call this variable and read from the file via this methods http://odiexperts.com/?p=243 , http://odiexperts.com/?p=273.
    Does this helps you .

  • Call Excel Macros function from Java

    Hi All,
    can u guide me How to call excel macros from java.
    The thing is first I have to enable macros and then call the macors function.
    Is this possible to call from Java. If yes guide me.
    Thanks in Advance

    Seems like something you'd have to do through COM.
    http://sourceforge.net/projects/jacob-project/
    HOW you do that is something you'll really have to figure out yourself, its not a Java question.

  • How to access excel(macros defined) in Infoview?

    Hi All,
    Can anyone help me out to know how to view Excel with Macros in Infoview.
    Regards,
    Anisa

    Hi,
    So you mean to say, I just need to use the ConnectionManager.getConnection() method. It requires a pool connection name. Do I specify the same pool name as in the connectionmanager.xml config file ? And that will do it ? Please clarify. Following is a code snippet. Please verify if this is correct.
    Connection conn = null;
    try
    conn = ConnectionManager.getConnection(poolname);
    if(conn == null) {
    throw new IOException("No such connection found");
    thanks,
    Mainak

  • How to run Excel Macros using JDBC-ODBC

    Hi,
    I want to run the excel macros in the excel file, after I connected to excel file, using JDBC-ODBC bridge.
    How macros treated here?
    Help needed please..........
    - Ramesh

    How to run Excel Macros using JDBC-ODBCYou don't.
    As my fuzzy understanding goes.....
    Macros (excel, word, etc) run via a "OLE" extension of the script host system.
    So the only way to run them is via the OLE interface. That has nothing to do with ODBC. You can write your own JNI to do that, or you might get lucky and find that someone else has written a java library to do it for you.

  • How to run excel macro from ole

    hi to all experts.
    i need to run excel macro from ole

    Hi,
    *& Report  ZKC_TEST
    *& Description: Fancy report output in XL
    *&Programmer: Krishna Chauhan
    *&Date:       20 Feb 09
    REPORT  ZKC_TEST.
    * INCLUDES                                            *
    INCLUDE ole2incl.
    *&   TYPES                                            *
    TYPES: BEGIN OF ty_marc,
           matnr TYPE marc-matnr,
           werks TYPE marc-werks,
           pstat TYPE marc-pstat,
           lvorm TYPE marc-lvorm,
           ekgrp TYPE marc-ekgrp,
           END OF ty_marc.
    TYPES: BEGIN OF ty_titles,
           title(20) TYPE c,
           field(20) TYPE c,
           END OF ty_titles.
    *&   INTERNAL TABLES                                  *
    DATA: t_marc TYPE STANDARD TABLE OF ty_marc,
          t_titles TYPE STANDARD TABLE OF ty_titles.
    *&   FIELD-SYMBOLS                                    *
    FIELD-SYMBOLS: <fs_marc>   LIKE LINE OF t_marc,
                   <fs_titles> LIKE LINE OF t_titles,
                   <fs> TYPE ANY.
    *&   VARIABLES                                        *
    DATA: w_tabix TYPE sy-tabix,
          w_titles TYPE sy-tabix,
          w_line TYPE sy-tabix,
          w_field TYPE string,
          filename TYPE string,
          path TYPE string,
          fullpath TYPE string.
    DATA: data_titles TYPE REF TO data.
    DATA: e_sheet TYPE ole2_object,
          e_activesheet TYPE ole2_object,
          e_newsheet TYPE ole2_object,
          e_appl TYPE ole2_object,
          e_work TYPE ole2_object,
          e_cell TYPE ole2_object,
          e_color TYPE ole2_object,
          e_bold TYPE ole2_object.
    *&   SELECTION-SCREEN                                 *
    SELECTION-SCREEN BEGIN OF BLOCK b1.
    PARAMETERS: p_file TYPE rlgrap-filename.
    SELECTION-SCREEN END OF BLOCK b1.
    *&  START-OF-SELECTION                                *
    START-OF-SELECTION.
      PERFORM get_titles.
      PERFORM get_data.
      PERFORM create_excel.
    *& AT SELECTION-SCREEN                                *
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
      CALL METHOD cl_gui_frontend_services=>file_save_dialog
        EXPORTING
          window_title      = 'Select archivo'
          default_extension = 'xls'
          file_filter       = '*.xls'
        CHANGING
          filename          = filename
          path              = path
          fullpath          = fullpath.
      IF sy-subrc EQ 0.
        p_file = fullpath.
      ENDIF.
    *&      Form  get_titles                              *
    FORM get_titles.
      CREATE DATA data_titles TYPE ty_titles.
      ASSIGN data_titles->* TO <fs_titles>.
      <fs_titles>-title = 'Material'.
      <fs_titles>-field = 'MATNR'.
      APPEND <fs_titles> TO t_titles.
      <fs_titles>-title = 'Plant'.
      <fs_titles>-field = 'WERKS'.
      APPEND <fs_titles> TO t_titles.
      <fs_titles>-title = 'PSTAT'.
      <fs_titles>-field = 'PSTAT'.
      APPEND <fs_titles> TO t_titles.
      <fs_titles>-title = 'Deletion Flag'.
      <fs_titles>-field = 'LVORM'.
      APPEND <fs_titles> TO t_titles.
      <fs_titles>-title = 'EKGRP'.
      <fs_titles>-field = 'EKGRP'.
      APPEND <fs_titles> TO t_titles.
    ENDFORM.                    "get_titles
    *&      Form  get_data                                *
    FORM get_data.
      SELECT matnr werks pstat lvorm ekgrp
      INTO TABLE t_marc
      FROM marc
      WHERE matnr = '000000000001013351'.
    ENDFORM.                    " get_data
    *&      Form  create_excel                            *
    FORM create_excel.
      w_line = 1.
      CREATE OBJECT e_appl 'EXCEL.APPLICATION'.
      SET PROPERTY OF e_appl 'VISIBLE' = 1.
      CALL METHOD OF e_appl 'WORKBOOKS' = e_work.
      CALL METHOD OF e_work 'Add' = e_work.
      GET PROPERTY OF e_appl 'ActiveSheet' = e_activesheet.
      SET PROPERTY OF e_activesheet 'Name' = 'Material Plants'.
      LOOP AT t_marc ASSIGNING <fs_marc>.
        w_tabix = sy-tabix.
        w_line = w_line + 1.
        LOOP AT t_titles ASSIGNING <fs_titles>.
          w_titles = sy-tabix.
          CALL METHOD OF e_appl 'Cells' = e_cell
            EXPORTING
              #1 = 1
              #2 = w_titles.
          SET PROPERTY OF e_cell 'Value' =  <fs_titles>-title.
          GET PROPERTY OF e_cell 'Interior' = e_color.
          SET PROPERTY OF e_color 'ColorIndex' = 35.
          GET PROPERTY OF e_cell 'Font' = e_bold.
          SET PROPERTY OF e_bold 'Bold' = 1.
          CALL METHOD OF e_appl 'Cells' = e_cell
            EXPORTING
              #1 = w_line
              #2 = w_titles.
          CONCATENATE '<fs_marc>-' <fs_titles>-field
          INTO w_field.
          ASSIGN (w_field) TO <fs>.
          SET PROPERTY OF e_cell 'Value' = <fs>.
          GET PROPERTY OF e_cell 'Interior' = e_color.
          SET PROPERTY OF e_cell 'ColumnWidth' = 20.
          SET PROPERTY OF e_color 'ColorIndex' = 0.
          GET PROPERTY OF e_cell 'Font' = e_bold.
          SET PROPERTY OF e_bold 'Bold' = 0.
        ENDLOOP.
      ENDLOOP.
      CALL METHOD OF e_work 'SAVEAS'
        EXPORTING
          #1 = p_file.
      CALL METHOD OF e_work 'close'.
      CALL METHOD OF e_appl 'QUIT'.
      FREE OBJECT e_appl.
    ENDFORM.                    " create_excel

  • How to call HR Macros in ABAP-Webdynpro

    HI all,
    I want to extract HR data in webdynpro view.
    Where should I give Logical Database PNP?
    The ifnotypes key word is not accepting in Webdynpro coding?
    Where should I give GET PERNR event in Webdynpro?
    When I use RP_PROVIDE_FROM_LAST .... says error message
    Statement "RP_PROVIDE_FROM_LAST" is not defined. Check your spelling .
    How to use the Macros in Webdynpro (VIEW) methods.
    First time I'm working in ABAP-HR Webynpro.
    Please help me out..
    Thanks,
    Prasad

    Hi,
    How did u  solve this? pls provide the solution.
    Regards,
    Lakshmi.

  • How to run excel macros using lookout

    Hi,
    I want use Excel macros for generating custom reports.Is it possible to run macros using run object in lookout
    thanks

    Hi,
    I am pretty sure you can activate macros in Excel using the Run object in Lookout. I can think of two ways you can do this:
    1. You can setup your Excel to run macros on startup using the Auto_Activate method. In this case, you will just launch excel with your workbook using the Run object and that should run the macros automatically. No brainer!
    2. To better control as to when the macros are run we can assign in Excel some shortcut keys for their launching. We then would need to simulate these keystrokes to launch our macros. This can be done using the SendKeys method and some scripting, WHS Scripting for instance. See this pos
    t for a scripting example which simulates Alt+Tab keys to bring-to-front an app.
    You will launch the script from the Run object and this in turn launches Excel and then simulates the keys for luanching the appropriate macros. I admit this is kinda involved, but hey it works!
    Hope this helps,
    Khalid

  • How to call a macro(check for unsaved data) before calling refresh button of EPM TAB

    Hi,
    I want to show a popup message for any unsaved data before USER hit the refresh button on EPM tab.
    I write the macro like as below in module
    Function TestForUnsavedChanges()
        If ActiveWorkbook.Saved = False Then
            If MsgBox("There is unsaved data, save the data otherwise u will loose the data. Do You Want to Save the Data ?", vbYesNo, "Warning") = vbYes Then
            bSave = True
            Else
            bSave = False
            Application.Undo
            End If
        End If
    End Function
    so how to execute this macro before Refresh ?
    Thanking in Advance !!

    Hi Nilesh,
    Try to add this macro under Function BEFORE_REFRESH.
    Hope this helps.
    Regards,
    Astha

  • How to call an eCATT programmatically with import parameters

    Hi there,
    I am using function module ECATT_EXECUTE to call test scripts from within ABAP test code, like below. Unfortunately, I am not able to pass my actual import parameter values. It seems that the interface does not provide a way to pass import parameters to the eCATT.
    Although posted in November, this is still an issue for me. Does anybody know an answer?
      call function 'ECATT_EXECUTE'
        exporting
          to_execute              = lt_script
          display_log             = space
          i_supress_output        = 'X'
        importing
          executed                = lt_executed
          logid                   = ls_logid
          trace_prot              = lt_trace
        exceptions
          nothing_to_do           = 1
          too_many_scripts_called = 2
          others                  = 3.
    Does anybody reading this have a solution?
    Thanks and regards,
    Rüdiger
    Edited by: Rüdiger Plantiko on Jan 17, 2008 3:05 PM
    Edited by: Rüdiger Plantiko on Jan 17, 2008 3:05 PM

    Hello Rüdiger,
    direct passover would be fine - well, I've come to get used to finding workarounds in SAP though. Maybe, the function module will support the missing parameters in some forthcoming release.
    Btw I switched over to tables ECTC_VAR / ECTC_DATA and pass obj_type = 'ECTC' to ECATT_EXECUTE, since test configurations can directly be executed.
    You don't happen to know what has to be done, if the call of ECATT_EXECUTE results in a dump? After correctly executing the test configuration. Looks like eCatt tries to put the resulting log to nowhere. Perhaps I have to initialize some kind of a log receiver first or set some option in the start profile? I passed DISPLAY_LOG = SPACE, and SUPPRESS_OUTPUT = 'X'.
    Dump was:
    Laufzeitfehler         OBJECTS_OBJREF_NOT_ASSIGNED
    Datum und Zeit         05.03.2008 17:37:00
    Zeile Quelltext
         1 METHOD http_send_and_receive .
         2
         3   DATA: l_errortext       TYPE string.
         4   DATA: l_http_subrc      TYPE sy-subrc.
         5 *----------------------------------------------------------------
         6
    >>>>>   im_http_client->request->set_data( data = im_request ).
         8
    Aktive Aufrufe/Ereignisse
    Nr.   Art          Programm                            Include                             Zeile
          Name
        6 METHOD       CL_HTTP_TESTZONE_RESULT=======CP    CL_HTTP_TESTZONE_RESULT=======CM00Q     7
          CL_HTTP_TESTZONE_RESULT=>HTTP_SEND_AND_RECEIVE
        5 METHOD       CL_HTTP_TESTZONE_RESULT=======CP    CL_HTTP_TESTZONE_RESULT=======CM00O    16
          CL_HTTP_TESTZONE_RESULT=>HTTP_REQUEST_NEWRESULTID
        4 METHOD       CL_HTTP_TESTZONE_RESULT=======CP    CL_HTTP_TESTZONE_RESULT=======CM00T    34
          CL_HTTP_TESTZONE_RESULT=>SEND_TO_RESULT_REPOSITORY
        3 METHOD       CL_APL_ECATT_LOG_TO_RESREP====CP    CL_APL_ECATT_LOG_TO_RESREP====CM001   400
          CL_APL_ECATT_LOG_TO_RESREP=>GET_LOG_DATA
        2 FUNCTION     SAPLECATT_EXECUTE                   LECATT_EXECUTEU01                     888
          ECATT_EXECUTE
        1 EVENT        ZDSD_SL_VIA_CATT                    ZDSD_SL_VIA_CATT                       26
          START-OF-SELECTION
    Regards + have a nice weekend,
    Matthias

  • How to call fileDownload Listener programmatically

    Hey folks,
    I am working on jdev 11.1.1.4.0. I have a use case to call the file download listner on click of a command link.
    I want to call the action/action listener on the command link first and after that file download listener, is there any way to do that?
    Or, we can delay execute download listener to action/action listener on command link?
    Any suggestions will be appreciated. Thanks
    Regards
    Kanika

    Hi Timo,
    Thanks for your suggestion..
    The problem here is i am accessing both action listener and download listner on single click on a command link .
    As i click on command link it calls download listner ,,, here i have to delay the download lisnter for action listner to execute first.
    Is there any way to achieve that?
    Thanks
    Kanika

  • Excecting excel macro

    Hi,
    I have an excel macro, in which the user insert some inputs , press buttons
    and as a result, new file is being generated with list of suppliers.
    I would like to activate this excel application from web dynpro.
    Can someone tell me if it's possible to call excel macro from web dynpro
    and how.
    Thanks,
    Ronit.

    Hi Ronit,
    Open you excel file, go to VB editor (ALT F11).
    Add the following code to "ThisWorkbook"
    Private Sub Workbook_Open()
    your code
    End Sub
    p.s
    You must set the security low (Tools -> Macro -> Security -> Security Level)
    Omri

  • Call excell from RDF

    Hi
    can anyone help me how to call excell from RDF in reports6i (means i want to call reports in excel format)
    Thank you
    Kamaraj
    Edited by: user615671 on Jan 28, 2009 8:12 PM

    Hi,
    Google "concsub"
    Regards,
    Gareth
    Blog: http://garethroberts.blogspot.com/

  • How to call method excel subtotal in ABAP program

    I'm trying to call excel method subtotal, but i get an error (sy-subrc <> 0). How to correctly call this method? One of the parameters is an array. I've tried to use an internal table as the array, but ABAP syntax checker shows error.

    Hi Vytautas,
    The best way is to start recording a Macro(Tools->Macro->Record New Macro), and then do the operations (like entering data into cells, summing up, calculations,etc...)
    once you are done, display the macro and you will find all the needful information regarding what metohds are to be called and how...
    Regards,
    Raj

  • How do I make an Excel macro and Labview push to copy Labview calculated data into a cell on demand?

    Hey there, I am a chemist so Labview and programming aren't exactly my strong point. 
    We have a Labview program that we run to control and automated valve manifold that has a thermocouple and pressure transducer that displays the temperature and pressure on the "front panel" of the labview file we run.  The temperature and pressure of the manifold changes in real time while the labview is running.  We must attach a sample to the manifold, take the pressure and temperature, then record those values in an excel spread sheet, one at a time.
    What I would like to do is make a key board macro in excel to automatically type in the current read out of the temperature (and another for pressure) into which ever selected excel cell you are on, so that you don't have to switch back to the labview window to look at it.  I have a very basic understanding of macros in VB but I don't know how to get the labview to make the read outs available for the excel macros to find it.
    Thanks for the help, Here is a screen shot of the block layout of the program.
    Attachments:
    back.JPG ‏79 KB

    Why not have LV put the data in Excel for you?  You can use Actvie X to do this.  There is plenty of information on the forum and knowledge base on how to use Active X and reporting to Excel.  Plus you have the example finder.  Do you have the Report Generation Toolkit?  LV7 is quite old, so I don't know if this is an option for you.
    Reese, (former CLAD, future CLD)
    Some people call me the Space Cowboy!
    Some call me the gangster of love.
    Some people call me MoReese!
    ...I'm right here baby, right here, right here, right here at home

Maybe you are looking for

  • MR 11 not updating the invoicing for services

    Hi All, I have a scenario where the client uses MR11 for clearing the invoices. He does not use MIRO for invoicing. During our observation it is found that using MR11 does not update the invoices for services but the invoices for the material receive

  • Delete of 'Bookmarks Bar' folder in Safari

    Can anyone help to show me how to delete the 'Bookmarks Bar' folder in safari? I can't delete even with 'edit'. It seems to appear after I sunc my iphone with my computer. Thanks.

  • JCo getting truncated value from SAP when field data type is RAW

    We are trying to fetch data from a SAP-AII table by using JCo using the RFC RFC_READ_TABLE. We are getting incomplete data when the data type of the column is RAW in a particular table.A typical case is: Table Name: /AIN/DM_DEVCTR Field : CLIENT     

  • Creating a quiz in adobe flash professional cs 6

    I have created a list of MCQ questions for my flash quiz in adobe flash cs6 and I would like to create a maximum number of attempts for the quiz whereby users are only allowed to answer wrongly for only twice.What function should i apply in this case

  • My ipod screen needs repair. Will apple do that?

    i have an ipod touch 4th generation that needs repair. Will apple do that for me?