3

最近 .Net 1.1 的一个旧项目正在迁移到 .Net4.0。该项目正在使用以下方法打开excel文件:

Process process = new Process();

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

而marco_file是带有宏的excel文件的路径,比如“C:\project\test.xls”,其中的宏会在excel文件打开时立即运行。

在 .Net1.1 中,代码和宏都运行良好。在 .Net4 中,代码无异常运行。但是宏不起作用。

为什么excel文件中的宏没有运行?

我的环境是Win7 64位。办公室 2010 64 位。IIS 7.5

谢谢

当我更改为以下代码并运行调试模式时

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

结果是它进入了 else 块,这意味着进程无法启动。

有谁知道为什么?

4

1 回答 1

0

检查 Excel 中的安全性。默认情况下,当您打开包含宏的工作簿时,Excel 2010 将禁用宏,您必须明确启用它们。

要启用宏,您需要转到:

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

此处允许宏自动运行的唯一选项是:

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

不过请注意警告。

于 2012-03-20T03:16:03.500 回答