Skip to main content

VBA versus .NET

Adapter or Wrapper

This is code possible through .NET, in that it bridges across to COM for automation in VBA.  This concept can be extended to work for many other COM-based applications.  This code allows the add-in to expose internal .NET-coded procedures to Excel COM, extending the use of the .NET code.

Salient Characteristic(s)

  • Handle interface between different, incompatible systems
  • The interface is required because COM is interface-based
  • Other code elements are required for this to work properly
    • Registering the DLL
    • Creating the AdapterForVBA class on startup
Code

using System;
using System.Runtime.InteropServices;

namespace DesignPatterns
{
    /// <summary>
    /// Interace to expose VSTO/COM obects for COM and Excel
    /// Used by class below
    /// </summary>
    [ComVisible(true)]
    [Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IAdapterForVba
    {
        //signatures for methods acessible to VBA go here
        //exists here and in class
    }

    /// <summary>
    /// Class to expose items to VBA and other COM application
    /// </summary>
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class AdapterForVba : IAdapterForVba
    {
        public AdapterForVba()
        {
            //methods to expose go here
            //exists here and in interface
        }
    }
}

Comments

Popular posts from this blog

Decorator Pattern

This is an example of the Decorator pattern, in this case a decorator for ObservableCollection.  Working with WPF and ObservableCollection using threads, one will run into the problem whereby the ObservableCollection cannot be updated from outside the owning thread; Delegates and Invoke will not work. A solution is to contain and expand the class, as is done in this example on michIG's Blog . This linked file contains the original code in C#, as well as the same code converted to VB.NET. Salient Characteristic(s) Sets an internal pointer to the decorated object, sending method calls and property actions to the internal object Extends the object by wrapping it and adding some aspect handled by the decorator Code using System; using System.Collections.ObjectModel; using System.Windows.Threading; using System.Collections.Specialized; using System.ComponentModel; namespace DesignPatterns {     /// <summary>       /// This class is an...

James Igoe's Reviews > Thinking Architecturally

Thinking Architecturally by Nathaniel Schutta My rating: 4 of 5 stars An overview of architectural decisions, the politics and persuasion involved, and the needs to balance competing measures and attributes. A fairly easy read, but full of great suggestions, and, for many, reminders of how to handle being a senior developer or architect. View all my reviews

Migrating and Design Planning

I have been toying with the idea of migrating one of my sites to a better host - it was supported by Yahoo and now AAbaco - and implementing some newer technologies. Among products I have used at work or are working with peripherally, I am considering using ASP.NET MVC, Entity Framework, ReSTful API's, NoSQL, and Azure-hosted databases - it is currently a mixture of very low-end PHP, HTML5/CSS3, light Javascript, and MySQL - so I decided to write up an architectural diagram - it looks like any standard architecture, with maybe a few additional elements - to help with the planning: