创建UDF使用VSTO和没有VBA [英] Create UDF using VSTO and no VBA

查看:168
本文介绍了创建UDF使用VSTO和没有VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此的问题(但对我来说不VSTO SE),但是,我只是想确认,它是不可能使用纯VSTO在Visual Studio 2005中的UDF创建和Excel 2003 - 所以,绝对清晰的,我的问题是:



是否有可能创建一个Excel 2003中UDF使用Visual Studio 2005,并没有使用任何VBA或其他的技巧一个VSTO解决方案



我知道ManagedXLL,ExcelDNA,Excel4Net等,但不想考虑那些时刻。



谢谢


解决方案

至于周围是否有COM或VBA的方式,我不认为这是可能的(至少在没有任何非常肮脏的把戏)。其原因是,只有这样,办公室可以执行外部代码(即您加载项)是通过COM。即使是VSTO仍在使用旧了IDTExtensibility2 COM接口下方。 IDTExtensibility2的是,所有加载项的Microsoft Office应用程序必须实现一个COM接口。



VSTO,办公室之前加载项必须实现此IDTExtensibility2接口本身。在这样的基于COM加载项(或COM可见的托管加载项),你可以简单地添加你的UDF所描述的here



但是,现在VSTO,有抽象的附加层:VSTO使用所谓的解决方案装载机的实现了IDTExtensibility2,这是由VSTO运行时提供的DLL。这意味着您的外接不再是COM可见。因此,如果您添加一个UDF到您VSTO加载它不会到Office可见。



保罗拔解释了他的博客如何与VSTO和VBA做: 如何在VSTO Excel中创建的UDF托管代码





  1. 创建与VSTO你的函数


    $类b $ b

     < System.Runtime.InteropServices.ComVisible(真)> 
    公共类MyManagedFunctions
    公共功能GetNumber()作为整数
    返回42
    端功能
    端类


  2. 线材你的类VBA在VSTO

     私人小组ThisWorkbook_Open()处理Me.Open 
    Me.Application.Run(RegisterCallback,新MyManagedFunctions)
    端子


  3. 创建托管代码挂钩,并在VBA



    在一个VBA模块中的电子表格功能的包装或文档

     昏暗managedObject作为对象


    公用Sub RegisterCallback(回调为对象)
    将managedObject =回调
    端子


    公共功能GetNumberFromVSTO()作为整数
    GetNumberFromVSTO = managedObject.GetNumber()
    端功能




现在,你可以输入 = GetNumberFromVSTO()
在细胞中,当Excel开始小区
值应为42。



Similar to this question (but in my case not VSTO SE), however, I just want to confirm that it is not possible to create a UDF using pure VSTO in Visual Studio 2005 and Excel 2003 - so, to absolutely clear, my question is:

Is it possible to create a Excel 2003 UDF using Visual Studio 2005 and a VSTO solution without using any VBA or other tricks?

I'm aware of ManagedXLL, ExcelDNA, Excel4Net etc but don't want to consider those for the moment.

Thanks

解决方案

Concerning whether there is a way around COM or VBA I don't think that it is possible (at least not without any very dirty tricks). The reason is that the only way Office can execute external code (i.e. you add-in) is via COM. Even VSTO is still using the old IDTExtensibility2 COM interface underneath. IDTExtensibility2 is a COM interface that all add-ins for Microsoft Office applications must implement.

Before VSTO, Office add-ins had to implement this IDTExtensibility2 interface themselves. In such a COM based add-in (or COM-visible managed add-in) you can simply add your UDF as described here.

However, now with VSTO, there is an additional layer of abstraction: VSTO uses a so-called Solution Loader implementing IDTExtensibility2, which is a dll provided by the VSTO runtime. This means that your add-in is no longer COM-visible. Hence, if you added a UDF to your VSTO add-in it won't be visible to Office.

Paul Stubbs explains on his blog how to do with VSTO and VBA: How to create Excel UDFs in VSTO managed code

  1. Create a class with your functions in VSTO

    <System.Runtime.InteropServices.ComVisible(True)>
    Public Class MyManagedFunctions
        Public Function GetNumber() As Integer
            Return 42
        End Function 
    End Class
    

  2. Wire up your class to VBA in VSTO

    Private Sub ThisWorkbook_Open() Handles Me.Open
        Me.Application.Run("RegisterCallback", New MyManagedFunctions)
    End Sub
    

  3. Create Hook for managed code and a wrapper for the functions in VBA

    In a VBA module in your spreadsheet or document

    Dim managedObject As Object
    
    
    Public Sub RegisterCallback(callback As Object)
        Set managedObject = callback
    End Sub
    
    
    Public Function GetNumberFromVSTO() As Integer
        GetNumberFromVSTO = managedObject.GetNumber()
    End Function
    

Now you can enter =GetNumberFromVSTO() in a cell, when excel starts the cell value should be 42.

这篇关于创建UDF使用VSTO和没有VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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