1

I want to run a .xlsm file from ASP.NET Web Page where it generates some .xml file using VBA code. The slution suggested here doesn't seem to work in my case.

I tried the following in nmy TestWebApp. My Webform1.aspx

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"  OnClientClick="return Callxlsm(); "/>
    </div>
    </form>
</body>
    <script type="text/javascript">
        function Callxlsm() {
            alert("Hello");
            var MyObject = new ActiveXObject('WScript.Shell');
            MyObject.Run('file:///K:\NLMData\POC\TestWebApp\TestWebApp\Files\AMM_ModelWizard.xlsm');
        }
    </script>
</html>

in my code behind

    using System;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("Hello");
        }
    }
}

Not sure whats the issue, any pointers pls.Also, can we pass in parameters to this Excel file from the Page so the excel file manipulates them in certain way and generates the XML file?

Community
  • 1
  • 1
Programmerzzz
  • 1,237
  • 21
  • 48
  • 1
    You can't. Macros require either Excel itself to run. What are you trying to do? There are probably far better ways to do this, eg using a Data Connection instead of a macro to load data. Running macros on a server is *not* a good idea, they are too heavy and typically (invariably) badly written for a high traffic environment like a web application. – Panagiotis Kanavos Dec 05 '16 at 08:26

1 Answers1

2

You should try to use EPPlus to achvieve this. Look at this question Writing and Executing VBA Macros on Excel without using Excel.Interop

Community
  • 1
  • 1
Renat Zamaletdinov
  • 1,210
  • 1
  • 21
  • 36
  • the post says there's no inbuilt support in EPPLUS for .xlsm files. I don't want to modify the .xlsm file . all i need is to ru that file from webform.aspx and get some output – Programmerzzz Dec 05 '16 at 06:14
  • @Programmerzzz If you carefully read the question you will see that you can create autorun macros that will generate desired XML files. After that you can parse (or whatever you want) as result – Renat Zamaletdinov Dec 05 '16 at 06:20
  • If I understood right we need to recreate the same .xlsm file by adding the VBA code through epplus .pls correct if I am wrong..but in my case user is already using this file from long ago all he wants is to call this file from a web app so no user needs a copy of it on their machine. The second option I have is create same UI in web page and mimic the VBA code that excel uses in c# and dump the .XML file which I think is more difficult... – Programmerzzz Dec 05 '16 at 06:31
  • So, if I understood right you already got xlsm file, so you can easily edit VBA code to run on open file "event" – Renat Zamaletdinov Dec 05 '16 at 06:35
  • Yes I have the xlsm file what's the open file event and how would it relate to asp.net page life cycle.because in the end I need to invoke xlsm through web form.pls clarify – Programmerzzz Dec 05 '16 at 06:45
  • First of all look at this http://analysistabs.com/excel-vba/run-macro-automatically-opening-workbook/ article. There is macro that run automatically on workbook open. After that you should try to open xlsm through code (Interop, EPPlus etc.) to check autorun. Second part of your comment (about life cycle) deserves separate question. IMO – Renat Zamaletdinov Dec 05 '16 at 06:51
  • Why should we need to open workbook twice once manually and once through code? That's what I can't understand. Like I said user wanna run that file through web app instead on his machine..which should also open once when a user clicks a button on the page. What am I missing here? – Programmerzzz Dec 05 '16 at 06:56
  • Aww, I missed that user will run macros on his machine. So, I can't suggest you any client-side solution. But according to mine solution, user have to upload file to server, server will proccess and return result (via AJAX) – Renat Zamaletdinov Dec 05 '16 at 07:01
  • Thats fine..Lets say I upload the xlsm to a Files subdir in the Webapp. But even then the IIS should run the .xlsm file in code behind file and return/create the .xml somewhere accessible to the user. That brings us to the same question on how to run the xlsm through C#/codebehind which is what I am trying to solve. The solution to post that I mentioned in my question don't seem to work for me which is why I posted the code hoping to find a solution here. – Programmerzzz Dec 05 '16 at 07:05
  • The other option would be to create the xlsm UI and macros in web app and process it in code behind and return XML I need which I think is more difficult..basically a rework of what we already have in xlsm. What do u think is a better solution of these two – Programmerzzz Dec 05 '16 at 07:09
  • So, if you have file on server-side you can try modify macro to make it autorun, then you can try to open file through C# or even .bat file. About your solution, I think it is better approach, because mine is kinda weird – Renat Zamaletdinov Dec 05 '16 at 07:11
  • Which approach of mine is better u think the first one or creating everything from macro in web app?..as a follow up if we make macro as auto run..I am not sure on how to invoke it from c# or .bat any samples in this regard? – Programmerzzz Dec 05 '16 at 07:14
  • Better to recreate macro in web app. I'm quite sure that's not so difficult to implement. And this solution will be easy to maintain in future. – Renat Zamaletdinov Dec 05 '16 at 07:17
  • 1
    @Programmerzzz macros are bad, for security *and* performance. Which is why SharePoint doesn't run macros in published Excel files, even though it supports data connections and formulas. What is the *actual* problem you try to solve with macros? Have you considered that macros would lock you out of Office 365 and SharePoint online? – Panagiotis Kanavos Dec 05 '16 at 08:32