none
Accessing C# Class Library via Outlook VBA RRS feed

  • Question

  • Trying to access a .Net Class Library from MS Outlook 2007.

    I built a class library that looks like this. Build using x86

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace PeruToJira
    {
        /// <summary>
        /// Represents a PeruToJira class
        /// </summary>
        public class SubmitReport
        {
            /// <summary>
            /// Submit report data to be submitted tp JIRA Database
            /// </summary>
            public static int Submit(string strXMLFilePath, string strTitle, string strDescription)
            {
                return 1234;
            }
        }
    } 
    In the class
     project I checked the "Register for COM interop" and "Make assembly COM-Visible".

    I built a dummy C# console app to access the class, it looks like this and executes fine. Built using x86

        static void Main(string[] args)
            {
            int retvalue = PeruToJira.SubmitReport.Submit("", "", "");
            }


    In the Outlook VBA I added the reference via the PeruToJira.tlb file

                Dim objp2Jira As PeruToJira.SubmitReport
               
                Set objp2Jira = New PeruToJira.SubmitReport
               
                lngTrackerNumber = objp2Jira.Submit(strXMLPathFile, strTitle, strDescription)


    When I execute the "objp2Jira.Submit(...) line I get an exeception that states "Object doesn't support this property or method"


    Any help? Thanks

    Monday, February 6, 2012 6:35 PM

Answers

  • i guess, without trying, that your method is static and this is a problem, change it to instance methid and try again.
    • Marked as answer by GSP_T Tuesday, February 7, 2012 1:05 PM
    Tuesday, February 7, 2012 9:22 AM

All replies

  • Unfortunately your question isn't about Outlook development.  You have created your own assembly that you are referencing, and the problem appears to be directly related to the implementation or utilization of your custom assembly.  If you have specific questions about the Outlook Object Model, we'd be more than happy to help!
    Eric Legault
    MVP (Outlook)
    About me...
    Monday, February 6, 2012 6:58 PM
    Moderator
  • I'll try the .Net forums
    Monday, February 6, 2012 7:02 PM
  • i guess, without trying, that your method is static and this is a problem, change it to instance methid and try again.
    • Marked as answer by GSP_T Tuesday, February 7, 2012 1:05 PM
    Tuesday, February 7, 2012 9:22 AM
  • Hi GSP_T,

    Thanks for posting in the MSDN Forum.

    You need regist your class type in HKEY_CLASS_ROOT if you want to use VBA access your class library. I will show you a sample of it.

    First, let's create a ClassLibrary project. In this sample is MyUDF.

    Second, create a Class. In my sample is UDF1.

    UDF1.cs:

    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    
    namespace MyUDF
    {
        //The GUID generated via Tools->Create GUID
        [Guid("A2DDB8AC-32B5-4356-B465-D2CF05CCAF8A")]
        [ClassInterface(ClassInterfaceType.AutoDispatch)]
        [ComVisible(true)]
        public class UDF1
        {
            //This is the Function which we will access in VBA Project.
            public string Hello()
            {
                return "Hello World!";
            }
    
            //We need This to Regist This Class.
            [ComRegisterFunctionAttribute]
            public static void RegisterFunction(Type type)
            {
    
                Registry.ClassesRoot.CreateSubKey(
                  GetSubKeyName(type, "Programmable"));
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(
                  GetSubKeyName(type, "InprocServer32"), true);
    
                key.SetValue("",
                  System.Environment.SystemDirectory + @"\mscoree.dll",
                  RegistryValueKind.String);
            }
    
            //This method used to unregeister Class.
            [ComUnregisterFunctionAttribute]
            public static void UnregisterFunction(Type type)
            {
                Registry.ClassesRoot.DeleteSubKey(
                  GetSubKeyName(type, "Programmable"), false);
            }
    
            private static string GetSubKeyName(Type type,
              string subKeyName)
            {
    
                System.Text.StringBuilder s =
                  new System.Text.StringBuilder();
    
                s.Append(@"CLSID\{");
                s.Append(type.GUID.ToString().ToUpper());
                s.Append(@"}\");
                s.Append(subKeyName);
                return s.ToString();
            } 
        }
    }

    Third, right click mouse button on solution. select Properties -> select Build tab -> select Regist for COM Interop. Build under administrator right.

    Forth, insert a module in VBA project. Select Menu Tools -> References... -> select MyUDF

    At last, we use follow snippet in the VBA to access the function:

    Sub test()
        Dim obj As New MyUDF.UDF1
        
        MsgBox (obj.Hello())
    End Sub

    I hope what I said can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, February 7, 2012 10:17 AM
    Moderator