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.

2
  • 1
    When you say "for a web application" - should the macro be executed on the client or on the server? Commented 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). Commented Sep 5, 2018 at 5:45

1 Answer 1

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

    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.