简体   繁体   中英

VBA Error 1004 - Call function from VB.Net (Visual Studio)

I am calling a Macro in an excel file from VB.Net. Everytime I call it I get an error 1004 on the following line of Code

Application.Run "ATPVBAEN.XLAM!Fourier", Sheets("Sheet2").Range("$Q$5:$Q$260"), _
        Sheets("Sheet2").Range("$R$1:$R$256"), True, False

When Running the code straight from excel it works perfectly. But when it's runn from Visual Studio, the error occurs.

I have it working from a button click and cell change in excel and both these methods don't work from Visual Studio. Does anyone know why this error is occuring.

This problem is documented in the article Add-ins do not load when using the CreateObject command in Excel ( web archive link in-case the main link dies ).

The following demonstrates the methodology outlined in the referenced article. The example includes the usage of empty Catch blocks . Get over it, this example is just to demonstrate one way to load the addin Workbook and not meant as a treatise on how to follow someone's programming ideology.

Sub DemoExcelAddinLoading()
    Dim app As New Excel.Application
    ' you must have an open Workbook before trying to open the 
    ' addin.  if no Workbook is open, opening the addin will fail

    Dim wb As Excel.Workbook = app.Workbooks.Open("path to your workbook")

    ' a big annoyance is that the addin seems to be loaded
    ' and installed if the current user-interactive Excel has it as such.
    ' this is useful to retrieve the addin file path though
    Dim toolPakAddin As Excel.AddIn = Nothing
    Try
        ' will throw if "Analysis ToolPak" not installed
        toolPakAddin = app.AddIns("Analysis ToolPak")
    Catch ex As Exception
    End Try

    Dim wbToolPak As Excel.Workbook = Nothing
    If toolPakAddin IsNot Nothing Then
        Try
            wbToolPak = app.Workbooks.Open(toolPakAddin.FullName)
        Catch ex As Exception
        End Try
    End If

    If wbToolPak IsNot Nothing Then
        ' register the addin
        Dim res As Boolean = app.RegisterXLL(toolPakAddin.Name)
        ' AutoRun macros are disabled under automation, so
        ' allow the addin to initialize
        wbToolPak.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoOpen)
    End If

    Dim rngIn As Excel.Range
    Dim rngOut As Excel.Range
    Dim ws As Excel._Worksheet = CType(wb.Worksheets("Sheet2"), Excel._Worksheet)

    rngOut = ws.Range("$c$1:$c$8")
    rngOut.Clear()
    rngIn = ws.Range("$a$1:$a$8")
    Dim wbName As String = wb.Name

    app.Visible = True
    Try
        app.Run("ATPVBAEN.XLAM!Fourier", rngIn, rngOut, True, False)
    Catch ex As Exception
    End Try

    ' Note: do not attempt to close wbToolPak 
    wb.Saved = True
    wb.Close()
    app.Quit()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM