从Excel宏返回值到VB.net应用程序。 [英] Return Value From Excel Macro to VB.net Application.

查看:91
本文介绍了从Excel宏返回值到VB.net应用程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是Vikash Gohil。



我必须创建一个应用程序,我需要打开excel文件并运行excel文件中写的宏我的VB.net应用程序。



我想知道怎样才能将用Macro编写的函数的返回值输入到我的应用程序中。



我有从excel文件运行宏的代码。



任何人都可以指导我这个,还是可以做的这个。



提前致谢。



等待回复。

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.

推荐答案

打开Excel,单击开发人员| Visual Basic(Alt-F11),插入|模块然后粘贴此代码:

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





请注意,您必须将文件另存为excel启用宏的工作簿(.xlsm)。剩下的代码将打开文件,使用参数调用宏并返回结果:



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:

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





您将在此处查找更多示例 http://support.microsoft.com/kb/306682 [ ^ ]



我希望这是对你有所帮助。



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

I hope this is of some help to you.


这篇关于从Excel宏返回值到VB.net应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆