从Office 2003执行.NET 3.0代码 [英] Execute .NET 3.0 code from Office 2003

查看:194
本文介绍了从Office 2003执行.NET 3.0代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a DLL in C# using the .NET 3.0 framework.

以下是我的DLL的代码

Below is the code of my 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;
        }
    }
}

我试图得到此代码从Office 2003 VBA代码运行。 DLL已经使用默认的Visual Studio 2008签名进行了签名。

I'm trying to get this code to run from Office 2003 VBA code. The DLL was signed using the default Visual Studio 2008 signing.

我已经设法通过使用

regasm /tlb c:\CompanyName.Net.dll 

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

But when I want to create an instance of the object:

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

我收到以下错误: / p>

I get the following error:


ActiveX组件无法创建Object

ActiveX component can't create Object

我做错了什么?

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

The machine I'm testing with has .NET installed up to the .NET 3.5 framework.

推荐答案

可以,经过Thorsten Dittmar的一些不错的想法,我终于得到了这个东西。
在我们讨论过程中发现的一些事情和我在网络上发现的其他事情:

OK, after some pretty good ideas from Thorsten Dittmar I finally got this thing to work. Some things that came up during our discussion and other things I found on the web:


  1. .NET框架需要安装在目标机器上。

  2. .Net可编程性支持需要安装在目标机器上。

  3. 在AssemblyInfo.cs中确保你设置

  1. The .NET framework needs to be installed on the target machine.
  2. .Net Programmability support needs to be installed on the target machine.
  3. In AssemblyInfo.cs make sure you set


[assembly:ComVisible( true )]

正如Thorsten指出,您需要在.Net类中具有无参数的公共构造函数。

  • As Thorsten pointed out you need to have parameterless public constructor in your .Net class.

    通过运行此命令在目标计算机上注册DLL。 / codebase 参数似乎是为我做的伎俩。类型库(.tlb)或DLL的路径并不重要。您可以在C:\Windows\Microsoft.Net\Framework\v2.050727\RegAsm.exe

    Register your DLL on the target machine by running this command. The /codebase parameter seemed to do the trick for me. The path to the type library (.tlb) or DLL doesn't matter. You can find regasm at C:\Windows\Microsoft.Net\Framework\v2.050727\RegAsm.exe


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


  • Reference the .tlb file in your VBA Editor using Tools > References.

    这应该是诀窍。

    我还通过添加一个接口来升级我的Webrequest类,从而启用了 IntelliSense 支持(这在VBA中可以不起作用)。

    I also upgraded my Webrequest class by adding an interface for it thus enabled IntelliSense support in VB6 (which sadly enough does not work in VBA).

    这篇关于从Office 2003执行.NET 3.0代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆