I am trying to invoke one macro which is in Excel file. Below is the code which I got, but on running below it throws exception, with below message.
我正在尝试调用Excel文件中的一个宏。下面是我得到的代码,但是运行下面的代码会抛出异常,下面是消息。
Code:
代码:
// Define your Excel Objects
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = null;
try
{
//Start Excel and open the workbook.
xlWorkBook = xlApp.Workbooks.Open(@"C:\MyFolder\Junk\macro\xxxxxReport.xlsm");
//Run the macros by supplying the necessary arguments
xlApp.Run("LookupHLink");
//xlWorkBook.Save();
//Clean-up: Close the workbook
xlWorkBook.Close(false);
//Quit the Excel Application
xlApp.Quit();
}
catch (Exception ex)
{
}
finally
{
//~~> Clean Up
releaseObject(xlApp);
releaseObject(xlWorkBook);
}
There is no parameter for the macro.
宏没有参数。
Public Sub LookupHLink()
Error message:
错误信息:
Cannot run the macro 'LookupHLink'. The macro may not be available in this workbook or all macros may be disabled.
无法运行宏“LookupHLink”。该宏可能在此工作簿中不可用,也可能禁用所有宏。
Please let me know if I am doing anything wrong.
如果我做错了什么事,请告诉我。
1 个解决方案
#1
1
As far as I can tell the possible causes of this error are:
就我所知,造成这种错误的可能原因是:
- The system located the file but could not load it.
- 系统找到了文件,但无法加载它。
- The file was loaded but it has no macros.
- 该文件已加载,但没有宏。
- The file was loaded but the macros are disabled.
- 加载了文件,但禁用了宏。
- The file was loaded, macros exists... but you misspelled the name.
- 文件被加载,宏存在……但是你把名字拼错了。
Given the available information, it seems to be that the more likely cause is disabled macros.
给定可用的信息,似乎更可能的原因是禁用宏。
You may try moving your file to a trusted location:
您可以尝试将文件移动到可信的位置:
You can see the trusted locations by doing Click File > Options then Click Trust Center > Trust Center Settings > Trusted Locations.
您可以通过单击File >选项查看可信位置,然后单击Trust Center > Trust Center设置>可信位置。
See Change macro security settings in Excel [2010].
参见Excel[2010]中的更改宏安全设置。
You may also try changing the security settings:
您也可以尝试更改安全设置:
In Excel 2007
在Excel 2007
- Click the Microsoft Office Button, and then click Excel Options.
- 单击Microsoft Office按钮,然后单击Excel选项。
- Click Trust Center.
- 单击“信任中心。
- Click Trust Center Settings.
- 单击“信任中心设置。
- Click Macro Settings.
- 单击宏设置。
- Click to select the Trust access to the VBA project object model check box.
- 单击以选择对VBA项目对象模型复选框的信任访问。
- Click OK to close the Excel Options dialog box.
- 单击OK关闭Excel选项对话框。
源代码:当您以编程方式允许访问Excel 2003和Excel 2007中的Visual Basic项目时,您可能会收到一个运行时错误——在Microsoft知识库中。
Note: optional parameters in C# are a relatively new feature... you may try the old way and use Type.Missing
to fill the infamous 30 args of the Run method. Something like this:
注意:c#中的可选参数是一个相对较新的特性……你可以试试老办法,用打字。丢失以填充臭名昭著的运行方法的30个args。是这样的:
xlApp.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);
#1
1
As far as I can tell the possible causes of this error are:
就我所知,造成这种错误的可能原因是:
- The system located the file but could not load it.
- 系统找到了文件,但无法加载它。
- The file was loaded but it has no macros.
- 该文件已加载,但没有宏。
- The file was loaded but the macros are disabled.
- 加载了文件,但禁用了宏。
- The file was loaded, macros exists... but you misspelled the name.
- 文件被加载,宏存在……但是你把名字拼错了。
Given the available information, it seems to be that the more likely cause is disabled macros.
给定可用的信息,似乎更可能的原因是禁用宏。
You may try moving your file to a trusted location:
您可以尝试将文件移动到可信的位置:
You can see the trusted locations by doing Click File > Options then Click Trust Center > Trust Center Settings > Trusted Locations.
您可以通过单击File >选项查看可信位置,然后单击Trust Center > Trust Center设置>可信位置。
See Change macro security settings in Excel [2010].
参见Excel[2010]中的更改宏安全设置。
You may also try changing the security settings:
您也可以尝试更改安全设置:
In Excel 2007
在Excel 2007
- Click the Microsoft Office Button, and then click Excel Options.
- 单击Microsoft Office按钮,然后单击Excel选项。
- Click Trust Center.
- 单击“信任中心。
- Click Trust Center Settings.
- 单击“信任中心设置。
- Click Macro Settings.
- 单击宏设置。
- Click to select the Trust access to the VBA project object model check box.
- 单击以选择对VBA项目对象模型复选框的信任访问。
- Click OK to close the Excel Options dialog box.
- 单击OK关闭Excel选项对话框。
源代码:当您以编程方式允许访问Excel 2003和Excel 2007中的Visual Basic项目时,您可能会收到一个运行时错误——在Microsoft知识库中。
Note: optional parameters in C# are a relatively new feature... you may try the old way and use Type.Missing
to fill the infamous 30 args of the Run method. Something like this:
注意:c#中的可选参数是一个相对较新的特性……你可以试试老办法,用打字。丢失以填充臭名昭著的运行方法的30个args。是这样的:
xlApp.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);