C
CyberNain
Hello,
I am trying to do the following.
I got this Excel function (this is a simple example):
Public Function foo(a() As Integer) As Integer
Dim i As Integer
foo = 4
ReDim a(0 To 4) As Integer
For i = 0 To 4
a(i) = i + 5
Next i
End Function
I would like to call this function from VB.Net using code such as:
Public Sub CallFoo()
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New
Microsoft.Office.Interop.Excel.Application()
Dim wb As Workbook = xlApp.Workbooks.Open("C:\Book1.xls")
''''''''''''''''''
Dim arr() as Integer
MsgBox(xlApp.Run("bar", arr))
MsgBox(VarType(arr))
''''''''''''''''''
wb.Close(Nothing, Nothing, Nothing)
xlApp.Workbooks.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
wb = Nothing
xlApp = Nothing
GC.Collect()
End Sub
This code does not work, as arr does not contain anything after calling the
Excel function.
Question 1: is it possible to do what I describe here?
Question 2: if yes, how?
Thank you
I am trying to do the following.
I got this Excel function (this is a simple example):
Public Function foo(a() As Integer) As Integer
Dim i As Integer
foo = 4
ReDim a(0 To 4) As Integer
For i = 0 To 4
a(i) = i + 5
Next i
End Function
I would like to call this function from VB.Net using code such as:
Public Sub CallFoo()
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New
Microsoft.Office.Interop.Excel.Application()
Dim wb As Workbook = xlApp.Workbooks.Open("C:\Book1.xls")
''''''''''''''''''
Dim arr() as Integer
MsgBox(xlApp.Run("bar", arr))
MsgBox(VarType(arr))
''''''''''''''''''
wb.Close(Nothing, Nothing, Nothing)
xlApp.Workbooks.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
wb = Nothing
xlApp = Nothing
GC.Collect()
End Sub
This code does not work, as arr does not contain anything after calling the
Excel function.
Question 1: is it possible to do what I describe here?
Question 2: if yes, how?
Thank you