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.
-
1When you say "for a web application" - should the macro be executed on the client or on the server?Heinzi– Heinzi2018-09-05 05:43:40 +00:00Commented Sep 5, 2018 at 5:43
-
1(Note: If the answer is "on the server", this is not supported and you should look for an alternative solution to your problem).Heinzi– Heinzi2018-09-05 05:45:11 +00:00Commented Sep 5, 2018 at 5:45
Add a comment
|
1 Answer
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");
}
}
}