1

I'm trying to run an Excel Macro called Sheet1.Workbook_Open from a .NET web system.

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;

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.

17
  • Is Microsoft Office and Excel installed on the server? If it is, is it the same version as what you have on your local machine? Commented Dec 16, 2014 at 10:16
  • Excel is installed on the server, and it is the same version. Commented Dec 16, 2014 at 10:17
  • What account is used to run the code? On your machine it might be you as local admin and on the server? Have you checked event logs? Maybe there you could find some more information. Commented Dec 16, 2014 at 10:34
  • 1
    stackoverflow.com/questions/2136768/… ... you probably do not need the single quotes, but try to specify the name of the workbook, like this Application.Run "TempFile.xlsm!MyMacroName" Commented Dec 16, 2014 at 10:55
  • 1
    dude, Workbook_Open is a Private event procedure. It runs as soon as the workbooks opens. You're getting an error because it is Private and you can't call private macros through Application.Run. Commented Dec 16, 2014 at 11:18

2 Answers 2

3

I'm afraid that's not the answer you'd like to hear, but the correct answer is... you don't.

Microsoft explicitly states that Office automation is not supported from unattended code, which includes web applications:

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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://www.nullskull.com/articles/create_macro_at_runtime_in_dotnet.asp

http://support.microsoft.com/kb/306682

http://www.siddharthrout.com/2012/03/20/calling-excel-macros-programmatically-in-vb-net/

I hope it helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.