VB.NET的ActiveX控件持久性 [英] ActiveX Control Persistence with VB.NET

查看:113
本文介绍了VB.NET的ActiveX控件持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用VB 2012为Word,Excel和Access 2010制作ActiveX控件,所以我从MSDN的示例VB ActiveX控件开始: VB ActiveX控件(VBActiveX)。除了仅在Excel中有效的属性持久性之外,控件按预期工作,对于Word和Access,属性仅在窗体上创建控件时保留。



我想在CCW中有一个IPersistPropertyBag接口的默认实现,因为持久性在Excel中工作,但因为它不能使用Word和Access我试图在我的Control中提供我自己的接口实现,它应该覆盖在CCW。



这是我添加VBActiveX项目的代码:

I want to make an ActiveX Control for Word, Excel and Access 2010 with VB 2012 so I started from the sample VB ActiveX Control from MSDN: VB ActiveX Control (VBActiveX). The control work as expected except for properties persistence which only works in Excel, for Word and Access the properties are persisted only at the creation of the control on the form.

I guess there is a default implementation of the IPersistPropertyBag interface in the CCW since the persistence works in Excel but as it’s not working with Word and Access I am trying to provide my own implementation of the interface in my Control which should override the interface in the CCW.

Here is the code I added the VBActiveX project:

<ComImport, Guid("55272A00-42CB-11CE-8135-00AA004BB851"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IPropertyBag
    Sub Write(<[In]> Name As String, <[In]> ByRef Value As Object)
    Sub Read(<[In]> Name As String, ByRef Value As Object, ErrorLog As Integer)
End Interface

<ComImport, Guid("0000010C-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IPersist
    <PreserveSig>
    Sub GetClassID(<[Out]> ByRef ClassID As Guid)
End Interface

<ComImport, Guid("37D84F60-42CB-11CE-8135-00AA004BB851"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IPersistPropertyBag
    Inherits IPersist

    <PreserveSig>
    Shadows Sub GetClassID(<[Out]> ByRef ClassID As Guid)

    <PreserveSig>
    Sub InitNew()

    <PreserveSig>
    Sub Load(PropertyBag As IPropertyBag, ErrorLog As Integer)

    <PreserveSig>
    Sub Save(PropertyBag As IPropertyBag, <[In]> ClearDirty As Boolean, <[In]> SaveAll As Boolean)
End Interface







Private Sub GetClassID(ByRef ClassID As Guid) Implements IPersist.GetClassID, IPersistPropertyBag.GetClassID
    Debug.Print("IPersistPropertyBag.GetClassID")
    ClassID = Me.GetType().GUID
End Sub

Private Sub IPersistPropertyBag_InitNew() Implements IPersistPropertyBag.InitNew
    Debug.Print("IPersistPropertyBag_InitNew")
End Sub

Private Sub IPersistPropertyBag_Load(PropertyBag As IPropertyBag, ErrorLog As Integer) Implements IPersistPropertyBag.Load
    Debug.Print("IPersistPropertyBag_Load")
End Sub

Private Sub IPersistPropertyBag_Save(PropertyBag As IPropertyBag, ClearDirty As Boolean, SaveAll As Boolean) Implements IPersistPropertyBag.Save
    Debug.Print("IPersistPropertyBag_Save")
End Sub



我只有一些Debug现在在我的实现中打印,看它是否被Word或Excel调用,并且从我在输出窗口中看到的内容中调用了什么。有人知道我错过了什么吗?


I only have some Debug Print in my implementation for now to see if it is called by Word or Excel and from what I can see in the Output Window nothing in the interface is called. Does anybody know what I am missing?

推荐答案

有现成可用的: PropertyBag class [ ^ ]。按照链接创建自定义DLL以将其与MS Office应用程序一起使用。



这是一组关于公开COM接口的文章:

将.NET组件暴露给COM [ ^ ]

暴露.NET类库的COM接口以进行后期绑定 [ ^ ]

< a href =http://msdn.microsoft.com/en-us/library/ms973807.aspx>在.NET中创建组件 [ ^ ]

扩展您的使用C#,VB.NET或C ++ / CLI的VBA代码 [ ^ ]

了解与.NET应用程序的经典COM互操作性 [ ^ ]
There is ready-to-use: PropertyBag class[^]. Follow the link and create custom dll to use it with MS Office applications.

Here is a set of articles about exposing COM interfaces:
Exposing .NET Components to COM[^]
Exposing COM interfaces of a .NET class library for Late Binding[^]
Creating Components in .NET[^]
Extend your VBA code with C#, VB.NET, or C++/CLI[^]
Understanding Classic COM Interoperability With .NET Applications[^]


这篇关于VB.NET的ActiveX控件持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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