VBA 错误 1004 - 从 VB.Net (Visual Studio) 调用函数
VBA Error 1004 - Call function from VB.Net (Visual Studio)
我在 VB.Net 的 excel 文件中调用宏。每次调用它时,我都会在以下代码行中收到错误 1004
Application.Run "ATPVBAEN.XLAM!Fourier", Sheets("Sheet2").Range("$Q:$Q0"), _
Sheets("Sheet2").Range("$R:$R6"), True, False
当 运行 直接来自 excel 的代码时,它可以完美运行。但是当它从 Visual Studio 运行时,会发生错误。
我在 excel 中通过单击按钮和更改单元格来实现,而这两种方法在 Visual Studio 中都不起作用。有谁知道为什么会出现这个错误。
文章 Add-ins do not load when using the CreateObject command in Excel (web archive link in-case the main link dies) 中记录了此问题。
下面演示了参考文章中概述的方法。该示例包括使用空 Catch 块。克服它,这个例子只是为了演示一种加载插件工作簿的方法,而不是关于如何遵循某人的编程思想的论文。
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:$c")
rngOut.Clear()
rngIn = ws.Range("$a:$a")
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
我在 VB.Net 的 excel 文件中调用宏。每次调用它时,我都会在以下代码行中收到错误 1004
Application.Run "ATPVBAEN.XLAM!Fourier", Sheets("Sheet2").Range("$Q:$Q0"), _
Sheets("Sheet2").Range("$R:$R6"), True, False
当 运行 直接来自 excel 的代码时,它可以完美运行。但是当它从 Visual Studio 运行时,会发生错误。
我在 excel 中通过单击按钮和更改单元格来实现,而这两种方法在 Visual Studio 中都不起作用。有谁知道为什么会出现这个错误。
文章 Add-ins do not load when using the CreateObject command in Excel (web archive link in-case the main link dies) 中记录了此问题。
下面演示了参考文章中概述的方法。该示例包括使用空 Catch 块。克服它,这个例子只是为了演示一种加载插件工作簿的方法,而不是关于如何遵循某人的编程思想的论文。
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:$c")
rngOut.Clear()
rngIn = ws.Range("$a:$a")
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