Exception while running macro

Hi All,

I am trying to run a macro using the following code.

Imports Excel = Microsoft.Office.Interop.Excel

Dim ExcelObj = New Excel.Application()

Dim WorkBook As Excel.Workbook

Dim WorkSheets As Excel.Sheets

Dim WorkSheet As Excel.Worksheet

WorkBook = DirectCast(ExcelObj.Workbooks.Open("C:\Honey.xls"), Excel.Workbook)

WorkSheets = WorkBook.Sheets

WorkSheet = DirectCast(WorkSheets.Item("Deals"), Excel.Worksheet)

ExcelObj.Run("Sheet1.mm_Click")

System.Runtime.InteropServices.Marshal.ReleaseComObject(WorkSheet)

System.Runtime.InteropServices.Marshal.ReleaseComObject(WorkSheets)

WorkBook.Close(False)

System.Runtime.InteropServices.Marshal.ReleaseComObject(WorkBook)

ExcelObj.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelObj)

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ExcelObj)

ExcelObj = Nothing

Getting this error.

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
Message="Exception from HRESULT: 0x800A03EC"
Source=""
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)

There is no problem in running macros in other Excel files. The only difference in this is, there are two Add-Ins. 1. Analysis Toolpak 2. Analysis Toolpak - VBA.

Please help in resolving this. Thanks in advance.

Best Regards




Answer this question

Exception while running macro

  • Upsetian


    Is Sheet1.mm_Click defined as a Public Function or Sub

  • nono92_b


    You won't be able to execute this routine from outside of the module (via automation) unless it is defined as Public.

    If there is some reason it is defined as Private, you could create another Public Sub or Function in the same module which calls this Private Sub, and then call this new Public Sub or Function from Visual Basic.



  • mluckham

    Thanks for your reply pclement and sorry for belated response. The spreadsheets are given by some other dept. That is why took some time to get answer from them.

    The Sheet1.mm_Click is defined as private sub.

    Thanks & regards



  • Sodan


    The error message indicates to me that the Sub or Function is out of scope. Is there any chance this routine is calling another Sub or Function that may be inaccessible Could you post the code for this routine

  • Gary 1981

    Sorry no luck, still getting the same error. I tried all the remedies that are pointed to resolve the issue like setting the culture info, DCOM settings etc. but no use. I can manually open this Honey.xls and run the macro, no issues, same as other excels in the application.

    One thing I noticed is when debugging the application by setting ExcelObj.Visible = True and breaking the codes at ExcelObj.Run(macroname) command, if I click macros on other excels they are executing fine but for this excel I get the following error.

    Run-time error '1004':

    The macro '' can not be found.



  • Exception while running macro