How to call excel function with a byref array parameter from VB.Ne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top