Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VBA运行时错误:';459';使用自定义ActiveX控件运行userform时出现外部异常_C#_.net_Vba_Excel - Fatal编程技术网

C# VBA运行时错误:';459';使用自定义ActiveX控件运行userform时出现外部异常

C# VBA运行时错误:';459';使用自定义ActiveX控件运行userform时出现外部异常,c#,.net,vba,excel,C#,.net,Vba,Excel,我通过创建一个简单的COM可见类,在C#中创建了一个自定义ActiveX控件。我能够构建我的C#解决方案,我可以在Excel2010(32位)中看到该控件,我可以将自定义控件添加到VBA用户表单中并正确查看它。我正在运行Windows 7 64位企业版。但是,当我尝试运行UserForm时,VBA抛出运行时错误“459”:外部异常 从下图可以看出,我已成功地将自定义控件添加到UserForm(只需一个简单的按钮): 实际上,我可以单击ActiveX控件中的按钮,它将触发一个OnButtonCl

我通过创建一个简单的COM可见类,在C#中创建了一个自定义ActiveX控件。我能够构建我的C#解决方案,我可以在Excel2010(32位)中看到该控件,我可以将自定义控件添加到VBA用户表单中并正确查看它。我正在运行Windows 7 64位企业版。但是,当我尝试运行UserForm时,VBA抛出运行时错误“459”:外部异常

从下图可以看出,我已成功地将自定义控件添加到UserForm(只需一个简单的按钮):

实际上,我可以单击ActiveX控件中的按钮,它将触发一个OnButtonClick事件,显示一个messagebox:

该控件也可以在其他控件下使用,这使我可以在VBA工具箱中查看它:

我已经尝试了很多解决方案,但到目前为止没有任何效果,我只是无法在VBA用户窗体中使用自定义控件。以下是我迄今为止所尝试的:

  • 对64位和32位使用RegAsm.exe“控制DLL路径”/codebase
  • 尝试了不带/codebase的RegAsm.exe
  • 先将RegAsm.exe用于32位,然后使用64位,仅将其用于32位,并且仅用于64位
  • 在VBA编辑器中的“工具/引用”下添加了对自定义用户控件类的引用,这现在允许我查看带有方法等的公开COM接口,这现在抛出
    '459'对象或类不支持事件集
  • 我的项目在properties/build和assembly information/make assembly COM Visible下勾选了COM Visible
我还尝试在64位的Windows1064位机器上使用我的代码,并使用Excel2016 64位。我可以生成解决方案,但在VBA中的其他控件下,该控件不可见。我已经执行了注册DLL文件的所有RegAsm命令,但它不会显示在那里。这是非常奇怪的行为。我正在尝试创建一个ActiveX控件,该控件将在32位和64位Excel上工作,因为64位版本的Office不支持32位现有VBA控件(我们需要ListView、DateTimePicker)。我们有一个庞大的代码库,无法从VBA切换到其他平台,我们正在从32位系统迁移到64位系统。我找不到比我现在的解决方案更优雅的解决方案,这将是理想的,但它的行为如此怪异,拒绝工作

下面是我的C#代码(这是垃圾代码,只是做了一个快速测试解决方案),下面是我找到的一个示例,这是唯一一个允许我将控件添加到用户表单的解决方案:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ActiveXTest2
{
    [ProgId("ActiveXTest2.CustomUserControl")]
    [ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(IUserControlEvents))]
    public partial class CustomUserControl : UserControl
    {
        private ListView listView1;

        public CustomUserControl()
        {
            InitializeComponent();
        }

        // register COM ActiveX object
        [ComRegisterFunction]
        public static void RegisterClass(string key)
        {
            StringBuilder skey = new StringBuilder(key);
            skey.Replace(@"HKEY_CLASSES_ROOT\", "");

            Type myType1 = Type.GetTypeFromProgID("ActiveXTest2.CustomUserControl");
            Console.WriteLine("ProgID=ActiveXTest2.CustomUserControl GUID={0}.", myType1.GUID);

            TextWriter tw = File.CreateText("guid.txt");
            tw.WriteLine(skey.ToString());
            tw.WriteLine(myType1.GUID.ToString());
            tw.Close();

            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
            RegistryKey ctrl = regKey.CreateSubKey("Control");
            ctrl.Close();
            RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();
            regKey.Close();
        }

        // Unregister COM ActiveX object
        [ComUnregisterFunction]
        public static void UnregisterClass(string key)
        {
            StringBuilder skey = new StringBuilder(key);
            skey.Replace(@"HKEY_CLASSES_ROOT\", "");
            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
            regKey.DeleteSubKey("Control", false);
            RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
            regKey.DeleteSubKey("CodeBase", false);
            regKey.Close();
        }

        public delegate void ControlEventHandler();

        [Guid("0A415E38-372F-45fb-813B-D9558C787EB0")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IUserControlEvents
        {
            [DispId(0x60020001)]
            void OnButtonClick();
        }

        public interface ICOMCallable
        {
            int TestValue();
        }

        public int TestValue()
        {
            return 0;
        }

        public event ControlEventHandler OnButtonClick;

        protected virtual void OnOnButtonClick()
        {
            MessageBox.Show("TEST", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Location = new System.Drawing.Point(4, 4);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(228, 210);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            // 
            // CustomUserControl
            // 
            this.Controls.Add(this.listView1);
            this.Name = "CustomUserControl";
            this.Size = new System.Drawing.Size(235, 217);
            this.ResumeLayout(false);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OnOnButtonClick();
        }
    }


}

我正在看这个例子

他错了。我已经使用了下面的代码来注册COM,它成功了

        [ComRegisterFunction()]
    public static void RegisterFunction(Type _type)
    {
        if (_type != null)
        {
            string sCLSID = "CLSID\\" + _type.GUID.ToString("B");
            try
            {
                RegistryKey _key = Registry.ClassesRoot.OpenSubKey(sCLSID, true);
                try
                {
                    Guid _libID = Marshal.GetTypeLibGuidForAssembly(_type.Assembly);
                    int _major, _minor;
                    Marshal.GetTypeLibVersionForAssembly(_type.Assembly, out _major, out _minor);
                    using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
                    using (RegistryKey _sub = _key.CreateSubKey("MiscStatus")) { _sub.SetValue("", "0", RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("TypeLib")) { _sub.SetValue("", _libID.ToString("B"), RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("Version")) { _sub.SetValue("", String.Format("{0}.{1}", _major, _minor), RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
                    using (RegistryKey _sub = _key.CreateSubKey("InprocServer32")) { _sub.SetValue("", Environment.SystemDirectory + "\\" + _sub.GetValue("", "mscoree.dll"), RegistryValueKind.String); }
                }
                finally
                {
                }
            }
            catch
            {
            }
        }
    }

    [ComUnregisterFunction()]
    public static void UnregisterClass(Type t) //(string key)
    {
        string keyName = @"CLSID\" + t.GUID.ToString("B");
        Registry.ClassesRoot.DeleteSubKeyTree(keyName);
        //StringBuilder skey = new StringBuilder(key);
        //skey.Replace(@"HKEY_CLASSES_ROOT\", "");
        //RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
        //regKey.DeleteSubKey("Control", false);
        //RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
        //regKey.DeleteSubKey("CodeBase", false);
        //regKey.Close();
    }

我正在看这个例子

他错了。我已经使用了下面的代码来注册COM,它成功了

        [ComRegisterFunction()]
    public static void RegisterFunction(Type _type)
    {
        if (_type != null)
        {
            string sCLSID = "CLSID\\" + _type.GUID.ToString("B");
            try
            {
                RegistryKey _key = Registry.ClassesRoot.OpenSubKey(sCLSID, true);
                try
                {
                    Guid _libID = Marshal.GetTypeLibGuidForAssembly(_type.Assembly);
                    int _major, _minor;
                    Marshal.GetTypeLibVersionForAssembly(_type.Assembly, out _major, out _minor);
                    using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
                    using (RegistryKey _sub = _key.CreateSubKey("MiscStatus")) { _sub.SetValue("", "0", RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("TypeLib")) { _sub.SetValue("", _libID.ToString("B"), RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("Version")) { _sub.SetValue("", String.Format("{0}.{1}", _major, _minor), RegistryValueKind.String); }
                    using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
                    using (RegistryKey _sub = _key.CreateSubKey("InprocServer32")) { _sub.SetValue("", Environment.SystemDirectory + "\\" + _sub.GetValue("", "mscoree.dll"), RegistryValueKind.String); }
                }
                finally
                {
                }
            }
            catch
            {
            }
        }
    }

    [ComUnregisterFunction()]
    public static void UnregisterClass(Type t) //(string key)
    {
        string keyName = @"CLSID\" + t.GUID.ToString("B");
        Registry.ClassesRoot.DeleteSubKeyTree(keyName);
        //StringBuilder skey = new StringBuilder(key);
        //skey.Replace(@"HKEY_CLASSES_ROOT\", "");
        //RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
        //regKey.DeleteSubKey("Control", false);
        //RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
        //regKey.DeleteSubKey("CodeBase", false);
        //regKey.Close();
    }

基本上,这个控件可以在x32操作系统上运行,但在x64上失败了?你没有阅读全部内容。它显示在x32的VBA工具箱中的附加控件下,但它仍然不起作用,我只能在UserForm上拉它,但在运行时,它不会运行,因为它会引发异常。在x64上,它甚至没有在其他控件下显示。您使用的是哪一版本的Visual Studio?我使用的是VS 2013 Professional。因此,基本上该控件在x32操作系统上工作,但在x64上失败?您没有阅读全部内容。它显示在x32的VBA工具箱中的附加控件下,但它仍然不起作用,我只能在UserForm上拉它,但在运行时,它不会运行,因为它会引发异常。在x64上,它甚至不会在其他控件下显示。您使用的是哪个版本的Visual Studio?我使用的是VS 2013 Professional。