tags:

views:

39

answers:

1

Hi

I have a .net object that I want to use in Excel. I have an existing VBA script that i need to alter to call this the object from. I have then converted the object to a TLB. I've not really touched on this area before so any help will be appreciated.

I have created an interface

[Guid("0F700B48-E0CA-446b-B87E-555BCC317D74"),InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IOfficeCOMInterface
{

    [DispId(1)]
    void ResetOrder();
    [DispId(2)]
    void SetDeliveryAddress(string PostalName, string AddressLine1, string AddressLine2, string AddressLine3, string AddressLine4, string PostCode, string CountryCode, string TelephoneNo, string FaxNo, string EmailAddress);
}

I have also created an class that inherits that object.

[ClassInterface(ClassInterfaceType.None), ProgId("NAMESPACE.OfficeCOMInterface"), Guid("9D9723F9-8CF1-4834-BE69-C3FEAAAAB530"), ComVisible(true)]
public class OfficeCOMInterface : IOfficeCOMInterface, IDisposable
{
 public void ResetSOPOrder()
    {

    }
public void SetDeliveryAddress(string PostalName, string AddressLine1, string AddressLine2, string AddressLine3, string AddressLine4, string PostCode, string CountryCode, string TelephoneNo, string FaxNo, string EmailAddress)
    {

        try
        {
            SalesOrder.AmendDeliveryAddress(PostalName, AddressLine1, AddressLine2, AddressLine3, AddressLine4, PostCode);

            MessageBox.Show("Delivery address set");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

I can't access the object methods apart from dispose. Is there anything i need to do?

+1  A: 

The following steps should work:

  1. Compile your DLL.
  2. Use regasm /tlb (or regasm /codebase /tlb, if you don't want to put your C# DLL into the GAC) to create a TLB and register it as a COM object.
  3. Instantiate your object in Excel VBA, just like you would instantiate any other COM object. To do this, you can either:

    • use early binding: add a reference to your tlb in Excel and then use Set myObject = New NAMESPACE.OfficeCOMInterface or
    • use late binding, which does not require a reference: Set myObject = CreateObject("NAMESPACE.OfficeCOMInterface")

    The first option is easier for development, the second option is (often) easier for deployment.

  4. Use your object in VBA: myObject.ResetSOPOrder.

Heinzi
Hi thanks a lot for answering although I only have the dispose method available which must come from IDisposable. There must be something wrong be my interface, Im not sure the reason why. Would there be any reason for it. The instance is public, Im not sure if it has anything to do with the attributes.
Mark O'Grady