0

Hi i am trying to automatically open a excel workbook and run its macro for a web application using C# (visual studio) , can someone suggest if this is possible.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    When you say "for a web application" - should the macro be executed on the client or on the server? – Heinzi Sep 05 '18 at 05:43
  • 1
    (Note: If the answer is "on the server", [this is not supported](https://stackoverflow.com/a/23265750/87698) and you should look for an alternative solution to your problem). – Heinzi Sep 05 '18 at 05:45

1 Answers1

0

Hi I have found a solution on web for how to run Excel macros of aldready existing application, and runs its macro.

Here name of macro is : macro from workbook Macro.xlsm

Below is the code snippet :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;


 namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_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\\d.b.venkatachalam\\Desktop\\Macro.xlsm");
        xlApp.Visible = true;

        //~~> Run the macros by supplying the necessary arguments
        xlApp.Run("Macro");

    }
  }
}