Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
<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


VB
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

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?
Posted
Updated 9-Dec-14 8:27am
v3
Comments
VAlain 6-Dec-14 16:21pm    
This is about VB6 and C++ not about VB.NET witch is managed code like C# so it's very different.
Maciej Los 6-Dec-14 16:27pm    
Very os too strong. It's bit different... Nevertheless, you need to explain what it means: "not work in WOrd and Access".
VAlain 6-Dec-14 16:43pm    
In Word and Access when I first create the control on the form if I change the properties value it will be saved with the form but after that if I change the properties value they will not save with the form they will revert to the first saved values.
Maciej Los 6-Dec-14 16:30pm    
I believe you don't need to implement PropertyBag, because it's ready to use class: PropertyBag Class[^]

1 solution

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[^]
 
Share this answer
 
Comments
Maciej Los 7-Dec-14 4:46am    
I hate down-voters who don't want to explain the reason of down-vote.
VAlain 7-Dec-14 10:50am    
I have already seen those articles, I know how to expose a .NET Component to COM and I already have a working .NET Component exposed to COM like I say in my post but I have an issue with the persistence and that's not covered by those articles.

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