3

I have a problem when I try to run a vba script in excel file from C# application. My code is:

public void RunMacro(string path, string macro)
{
    Excel.Application app = null;
    Excel.Workbooks workbooks = null;
    Excel.Workbook workbook = null;

    app = new Excel.Application();
    if (app == null)
        return;
    app.Visible = false;
    ChangeCulture();
    workbooks = app.Workbooks;
    workbook = workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

    LogWrapper.Debug("Run Macro start");

    app.Run(macro, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
        , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
        , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
        , Type.Missing, Type.Missing, Type.Missing);
    LogWrapper.Debug("Run macro finish");

    workbook.Close(false, Type.Missing, Type.Missing);
    app.Quit();
    ReleaseObject(app);
    ReleaseObject(workbook);
}

I run this method after click on the button in my web browser and the browser works all the time ("waiting for localhost..."). I can't see any requests in Firebug. My LogWrapper logs only first information ("Run Macro start"). In the Event Viewer has not any information about this execution. In the task manager process EXCEL.EXE uses 100% of processor. And I'm waiting for the result a half hour and still is nothing.

When I change vba script name to running I get error that the vba script was not found in the excel file. My vba script is very simple:

Sub start()
    Range("A3").Select
    ActiveCell.Value = "def"
    Range("A4").Select
End Sub

Somebody know where I should search for information about this problem?

Community
  • 1
  • 1
dzzdr
  • 71
  • 5
  • 1
    Try just `app.Run(macro);` – Siddharth Rout Dec 17 '13 at 10:00
  • Ok. Problem is in macro content. Can not edit cell value, somebody knows why? – dzzdr Dec 17 '13 at 10:14
  • What exception you receive? – etaiso Dec 17 '13 at 10:18
  • I don't see any problem with the macro code. It will not write to the cell if the worksheet is protected. BTW you can change that one line to `Range("A3").Value = "def"` [INTERESTING READ](http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select) Also you are taking a big risk by not fully qualifying your range object. Which Sheet are you writing to? One last point. Do you really need this one line macro? You can do that from C# as well. – Siddharth Rout Dec 17 '13 at 10:25
  • @SiddharthRout thx for advice with Range. This macro in only example. In the future will run more complicated macros. I don't receive any exception. – dzzdr Dec 17 '13 at 10:34
  • Did you try my first suggestion? – Siddharth Rout Dec 17 '13 at 10:35
  • Of course, but not helped. – dzzdr Dec 17 '13 at 10:47
  • Are you using Excel on the server side from a web application (why else Firebug)? Office is not meant to be used on a server! And you can't use server-side code to control a client-side Excel. – Hans Kesting Dec 19 '13 at 08:37

1 Answers1

0

It was a problem with permissions of IIS USER. When I change permission or turn on Windows authentication, everything is works.

dzzdr
  • 71
  • 5