小弟新手,希望能够得到各位大大的指点,最近做一个能添加到Word中的ActiveX控件(C#开发的),搜索了很多帖子,目前根据各个论坛share的方法,做出了一个小的Demo,在Word的Active控件中能够找到了,但是当我想添加那个控件到Word中的时候,出现了一个错误提示对话框:

 

下面的是那个控件的注册表信息:

 

以下是源代码:

 1 namespace MyActiveX
 2 {
 3     [ComVisible(true)]
 4     [Guid("D02E9DEA-A861-4cfa-B89D-817282E6CB23")]
 5     public interface IMyActiveXControl
 6     {
 7         string WhatIsMyName();
 8         string WahtAmIDoing();
 9     }
10 
11     [ComVisible(true)]
12     [Guid("d5177203-e179-4918-b3d5-2065f894ba55")]
13     [ProgId("MyActiveX.MyActiveXControl")]
14     public partial class MyActiveXControl : UserControl, IMyActiveXControl
15     {
16         public MyActiveXControl()
17         {
18             InitializeComponent();
19         }
20 
21 
22         [EditorBrowsable(EditorBrowsableState.Never)]
23         [ComRegisterFunction()]
24         public static void Register(Type type)
25         {
26             try
27             {
28                 // Open the CLSID key of the control
29                 using (RegistryKey keyCLSID = Registry.ClassesRoot.OpenSubKey(                    @"CLSID\" + type.GUID.ToString("B"), true))
30                 {
31                     RegistryKey subkey = null;
32                     subkey = keyCLSID.OpenSubKey("InprocServer32", true);
33 
34                     if (subkey != null)
35                     {
36                         subkey.SetValue(null, Environment.SystemDirectory + @"\mscoree.dll");
37                     }
38                     using (subkey = keyCLSID.CreateSubKey("Control"))
39                     {
40                     }
41                     using (subkey = keyCLSID.CreateSubKey("TypeLib"))
42                     {
43                         Guid libId = Marshal.GetTypeLibGuidForAssembly(type.Assembly);
44                         subkey.SetValue("", libId.ToString("B"), RegistryValueKind.String);
45                     }
46                     using (subkey = keyCLSID.CreateSubKey("MiscStatus"))
47                     {
48                         subkey.SetValue("", "131201", RegistryValueKind.String);
49                     }
50                 }
51             }
52             catch (Exception ex)
53             {
54                 Console.WriteLine(ex.Message); // Log the error
55                 throw;  // Re-throw the exception
56             }
57         }
58 
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         [ComUnregisterFunction()]
61         public static void Unregister(Type type)
62         {
63             try
64             {
65                 Registry.ClassesRoot.DeleteSubKeyTree(@"CLSID\" + type.GUID.ToString("B"));
66             }
67             catch (Exception ex)
68             {
69                 Console.WriteLine(ex.Message); // Log the error
70                 throw; // Re-throw the exception
71             }
72         }
73 
74         private void BtnTestClick(object sender, EventArgs e)
75         {
76             MessageBox.Show("You are succeed to create an ActiveX control since you can see me now.");
77         }
78 
79         #region IMyActiveXControl 成员
80         public string WhatIsMyName()
81         {
82             return "My name is ActiveX Control Demo.";
83         }
84         public string WahtAmIDoing()
85         {
86             return "I'm trying to create a useful ActiveX Control through C#.";
87         }
88 
89         #endregion
90     }
91 }
View Code

我的问题在第一段中已经描述了,我这里再重复一下:这个控件在Word中现在是可以找到的,就是无法添加到Word的Document里面去。

知道的好心人帮帮吗,再次先谢谢了~~~