VBA integrated program can't catch ProjectItem.Instances lost

Latest post 03-25-2008 2:22 PM by jimg. 4 replies.
  • 03-18-2008 9:22 AM

    • ilia2
    • Top 50 Contributor
    • Joined on 12-12-2006
    • Posts 5

    VBA integrated program can't catch ProjectItem.Instances lost

    Hi,
    I represent the company that develops the application that integrates VBA. We're creating VBAProjects with host classes:
    TheProject.ProjectItems.AddHostClass(HostClass, HostClassName)
    Where "HostClass" is an instance of the HostClassObject.

    And then later we're adding instances like this:
    TheProjectItem.Instances.Add(DefaultInstance), HostClassUniqueID

    This works fine, but under some unknown circumstances these instances are lost, that is VBA can't catch Events fired by our HostClass. In this moment "TheProjectItem_ReleaseInstances" event is fired, but unfortunately this event not always fires.

    My question is:
    Is there any trick to find out, are instances lost or not? (maybe some VBA property or something else)
    Any help would be appreciated.

    Thanks in advance,
    Ilia
  • 03-18-2008 11:43 AM In reply to

    Re: VBA integrated program can't catch ProjectItem.Instances lost

    Ilia:

     Please contact me directly at dschneid@summsoft.com.

    We need to know what company you represent and learn more about your application.

    Best,

    David Schneid

     

     

  • 03-19-2008 11:06 AM In reply to

    • jimg
    • Top 10 Contributor
    • Joined on 04-05-2006
    • Posts 34

    Re: VBA integrated program can't catch ProjectItem.Instances lost

    Hello Ilia,

    I have placed our HostClass sample on our ftp site

    ftp://ftp.summsoft.com/vba/hostclassexample.zip

    This example is intended to demonstrate the use of VBA host classes.

    Full implementation of VBA host classes is quite involved, requiring that
    you respond to a number of events. Without this example, getting all the
    pieces to cooperate requires trial and error.

    The example is written in VB. The application itself is minimal, thereby
    exposing the particulars of host classes in its most raw and simple form.

    1. A template host class is created by calling IApcProjectItems::AddHostClass.

    2. Two arrays (or VB collections) are created to hold the aggregated instances (called TheOuterInstances in the VB example) and the raw instances (called TheRawInstances).

    3. When you click on the "New Doc Class" menu item, IApcInstances::Add is called to create a new instance of our given host class type. Before Add returns, an event is generated -- IApcProjectItemEvents::CreateInstance. This event handler creates a new raw instance and returns it, saving it in the TheRawInstances collection. When IApcInstances::Add finally
    returns to the caller, it returns the aggregated instance which we save into the TheOuterInstances collection.

    4. To raise an event, we simply iterate through the raw instances and raise the event on each instance.

    5. If you hit the Reset button in VBA, you will be asked to destroy all your instances -- the IApcProjectItemEvents::ReleaseInstances event is fired. We simply destroy our two collections to release our hold on everything.

    Hope this helps.

    Jim G

     

  • 03-21-2008 2:08 AM In reply to

    • ilia2
    • Top 50 Contributor
    • Joined on 12-12-2006
    • Posts 5

    Re: VBA integrated program can't catch ProjectItem.Instances lost

    Hi Jim, Thanks for your answer. In the example you sent me, you describe how to create HostClass in VBAProject, but I already know this technique very well, Actually I already had such an example given from some summit staff. I'm doing the very same things to create and destroy HostClass instances as it is described in the example, but in some seldom cases VBA stops to receive events raised by the instance of my HostClass (I can't catch ReleaseInstances event). I'm trying hard to detect such a situation, but unfortunately it's hard and probably depends on several things. So I decided to find out somehow, is VBA receiving events of my HostClass or not (in other words, I'd like to ask VBA was ReleaseInstances event fired or not). That's what I'm asking you. As I noticed (observing VBA for a long time) VBA destroys instances of HostClass in the following cases: 1. When UserForm is attempted to remove 2. When some controls are about to add on the UserForm 3. When you're going to code some event procedure (of HostClass) in the VBA IDE Can you add something to this list to have a full picture? And now for the most important: Most of all, I'm concerning about the situation when the above-described interruption occurs with my end-user when he/she works with my application. They can't tell the exact situation, but it is probably related to the raising VBA error and showing error message with "Debug" button (BTW this is normal for me). But stopping VBA to receive events of my HostClass could harm data integrity in my app. So can you help me with this? Thanks in advance, Ilia P.S. I still keep trying to detect the case when my problem occurs and will tell you immediately as it happens.
  • 03-25-2008 2:22 PM In reply to

    • jimg
    • Top 10 Contributor
    • Joined on 04-05-2006
    • Posts 34

    Re: VBA integrated program can't catch ProjectItem.Instances lost

    Resets Happen


    Category:  Known Issues 

    Document Number: KB62RESET


    When you design your object model, remember that resets happen. A reset can be triggered by many different actions including the user clicking the reset button, adding a new control, or entering design mode. When a reset occurs, the project returns to its initial state; all local and global variables are cleared. You might create global variables that handle event processing using Dim WithEvents. You might create global variables that reference an instantiation of a VB class that implements some interfaces. A reset clears these global variables.

    When you define your object model, you should remember that global variables can be cleared at any point in time. You may choose to control how new elements are added to collections or how properties are set or what properties are exposed so that your object model cannot reference a global variable. Alternatively, you may choose to support your collections and properties in ways that gracefully handle a situation where these objects could be cleared.  Or you may simply choose to remind your users about the reset.

    The event IApcProjectEvents::Reset is triggered after a reset occurs. While it doesn't provide a complete solution, you can leverage this event to allow your application an opportunity to respond to the reset.

    Jim G

Page 1 of 1 (5 items) | RSS
Copyright Summit Software Company, 2008. All rights reserved.