vba 如何从 .Net Web 系统运行 Excel 宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27502176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run an Excel Macro from a .Net web system?
提问by Dr R Dizzle
I'm trying to run an Excel Macro called Sheet1.Workbook_Open
from a .NET web system.
我正在尝试运行Sheet1.Workbook_Open
从 .NET Web 系统调用的 Excel 宏。
Below is my current code.
下面是我目前的代码。
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("C:\Testing\TempFile.xlsm")
xlWorkSheet = xlWorkBook.Worksheets("TestSpreadsheet")
Dim MacroName As String = "Sheet1.Workbook_Open"
xlApp.Run(MacroName)
Dim FileDate As String = SysdateTextBox.Text
FileDate = FileDate.Replace(" ", "")
FileDate = FileDate.Replace("/", "")
FileDate = FileDate.Replace(":", "")
FileName = "C:\Testing\File" & FileDate & ".xlsm"
xlWorkBook.SaveAs(FileName)
xlWorkBook.Close(True)
xlApp.Quit()
However, it fails on the line xlApp.Run(MacroName)
with the error;
但是,它失败了xlApp.Run(MacroName)
,并出现错误;
Exception from HRESULT: 0x800A01A8
System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A01A8
Why is this happening, and how can I fix it? This error doesn't happen when the project is hosted on my local machine - only when it is on the server.
为什么会发生这种情况,我该如何解决?当项目托管在我的本地机器上时不会发生此错误 - 仅当它位于服务器上时。
回答by Heinzi
I'm afraid that's not the answer you'd like to hear, but the correct answer is... you don't.
恐怕这不是你想听到的答案,但正确的答案是……你不想。
Microsoft explicitly statesthat Office automation is not supported from unattended code, which includes web applications:
微软明确指出的是办公自动化不是从无人值守的代码支持,其中包括Web应用程序:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Microsoft 当前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office 可能表现出不稳定的行为和/或在此环境中运行 Office 时出现死锁。
If you need to interact with Excel files, the correct solution is to use a .NET library that supports the Excel file format. Unfortunately, that means that you won't be able to use your existing legacy Excel VBA code.
如果需要与 Excel 文件进行交互,正确的解决方案是使用支持 Excel 文件格式的 .NET 库。不幸的是,这意味着您将无法使用现有的旧 Excel VBA 代码。
回答by Maciej Los
Define new public procedure (add new module):
定义新的公共程序(添加新模块):
Public Sub MyMacro()
'do something
End Sub
then call it this way:
然后这样称呼它:
Dim sMacroName As String = "TempFile.xlsm!MyMacro"
xlApp.Run(sMacroName)
More at:
更多在:
http://forums.asp.net/t/1366665.aspx?How+to+run+a+Macro+of+an+Excel+file+opened+on+web+server
http://forums.asp.net/t/1366665.aspx?How+to+run+a+Macro+of+an+Excel+file+opened+on+web+server
http://www.nullskull.com/articles/create_macro_at_runtime_in_dotnet.asp
http://www.nullskull.com/articles/create_macro_at_runtime_in_dotnet.asp
http://support.microsoft.com/kb/306682
http://support.microsoft.com/kb/306682
http://www.siddharthrout.com/2012/03/20/calling-excel-macros-programmatically-in-vb-net/
http://www.siddharthrout.com/2012/03/20/calling-excel-macros-programmatically-in-vb-net/
I hope it helped.
我希望它有所帮助。