从Office 2003执行.NET 3.0代码

Pet*_*ter 8 .net c# dll vba access-vba

我使用.NET 3.0框架在C#中创建了一个DLL.

下面是我的DLL的代码

namespace CompanyName.Net
{
    [Guid("F7075E8D-A6BD-4590-A3B5-7728C94E372F")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ProgId("CompanyName.Net.Webrequest")]
    public class WebRequest
    {
        public string Result { get; private set; }
        public string Url { get; set; }
        public string StatusDescription { get; private set; }
        public HttpStatusCode StatusCode { get; private set; }

        public WebRequest()
        {
            //explicit constructor
        }    

        public string GetResponse(string url)
        {
            System.Net.WebRequest webreq = System.Net.WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse) webreq.GetResponse();
            // Store the status.
            StatusDescription = response.StatusDescription;
            StatusCode = response.StatusCode;
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            Result = reader.ReadToEnd();
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
            //return the response
            return Result;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试从Office 2003 VBA代码运行此代码.DLL使用默认的Visual Studio 2008签名进行签名.

我已经设法通过使用创建.TLB文件来引用我的程序集

regasm /tlb c:\CompanyName.Net.dll 
Run Code Online (Sandbox Code Playgroud)

但是当我想创建一个对象的实例时:

Private Sub Command0_Click()
    Dim o As Object
    Set o = CreateObject("CompanyName.Net.WebRequest")
    Dim s As String
    s = o.GetResponse("http://www.google.be")
    MsgBox s
End Sub
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ActiveX组件无法创建Object

我究竟做错了什么?

我正在测试的机器已安装.NET到.NET 3.5框架.

Pet*_*ter 10

好吧,经过Thorsten Dittmar的一些不错的想法,我终于得到了这个功能.我们在讨论期间提出的一些事情以及我在网上发现的其他事情:

  1. .NET框架需要安装在目标计算机上.
  2. .Net可编程性支持需要安装在目标机器上.
  3. 在AssemblyInfo.cs中确保你设置

    [assembly:ComVisible(true)]

  4. 正如Thorsten所指出的,你需要在.Net类中使用无参数的公共构造函数.

  5. 确保在项目的"属性"页面的"构建"选项卡中选中"注册COM Interop".
  6. 确保使用项目"属性"页面上的"签名"选项卡对项目进行签名.
  7. 通过运行此命令在目标计算机上注册DLL.该/基本代码参数似乎做的伎俩我.类型库(.tlb)或DLL的路径无关紧要.您可以在C:\ Windows\Microsoft.Net\Framework\v2.050727\RegAsm.exe中找到regasm

    regasm c:\ CompanyName.Net.dll /tlb:CompanyName.Net.tlb / codebase

  8. 使用工具>引用在VBA编辑器中引用.tlb文件.

  9. 将dll从C:\拖到C:\ Windows\assembly \的GAC (我最初没有意识到这一点,但显然Office必须找到你的程序集.)

这应该够了吧.

我还通过为其添加接口来升级我的Webrequest类,从而在VB6中启用了IntelliSense支持(遗憾的是在VBA中不起作用).