Click here to Skip to main content
15,887,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, this is Vikash Gohil.

I have to make an application where by i need to open excel file and run the macros written in the excel file from my VB.net application.

I want to know how can i get the return value of a function written in Macro in to my Application.

I have the code to run macros from excel file.

Can anyone guide me in this, or is it possible to do this.

Thanks in advance.

Awaiting a reply soon.
Posted

1 solution

Open Excel, click Developer|Visual Basic (Alt-F11), Insert|Module then paste this code:
VB
Public Function ExcelHandshake(Parameter As String) As String
    ExcelHandshake = "Hello " & Parameter
End Function


Note that you must save the file as excel Macro-Enabled Workbook (.xlsm). The remaining code will open the file, call the macro with a parameter and return a result:
VB
Imports Microsoft.Office.Interop
Module Module1
    Sub Main()
        Dim xlApp = New Excel.Application
        Dim xlBook = xlApp.Workbooks.Open("c:\projects\book1.xlsm")
        Dim result = xlApp.Run("ExcelHandshake", "My Application")
        Console.Write(result)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
        xlBook = Nothing
        xlApp.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
        xlApp = Nothing
        Console.ReadKey()
    End Sub
End Module


You will find more samples here http://support.microsoft.com/kb/306682[^]

I hope this is of some help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900