3

Recently an old project of .Net 1.1 is migrating to .Net4.0. The project is using the following method to open excel file:

Process process = new Process();

try
{
  process.StartInfo.FileName = marco_file;
  process.Start();
}
catch(Exception)
{
}
finally
{
  process.Close();
}

And the marco_file is the path of excel file with macros, like "C:\project\test.xls" The macro in it will run immediately when the excel file open.

In .Net1.1 both the codes and the macro runs well. In .Net4, the codes runs without exceptions. But the macro didn't work.

Why the macro in the excel file didn't run?

My environment is Win7 64bit. Office 2010 64bit. IIS 7.5

Thanks

When I change to the following codes and run the debug mode

process.StartInfo.FileName = marco_file;
if (process.Start())
{
    Debug.WriteLine("-->start ");
}
else
{
    Debug.WriteLine("-->failed ");
}

The result is that it goes into the else block, which means the process fails to start.

Any one knows why?

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
macemers
  • 2,194
  • 6
  • 38
  • 55

1 Answers1

0

Check the security in Excel. By default Excel 2010 will disable Macros when you open a workbook containing them, and you have to explicitly enable them.

To enable macros you need to go to:

File tab on the ribbon => Options
                       => Trust Center
                       => Trust Center Settings...
                       => Macro Settings

The only option here that will allow the macro to run automatically is:

Enable all macros (not recommended; potentially dangerous code can run)

Note the warning, though.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116