从Office 2003中执行.NET 3.0 codeOffice、NET、code

2023-09-03 12:40:04 作者:星河烂漫

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

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

下面是我的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;
        }
    }
}

我试图让这个code从Office 2003 VBA code运行。使用默认的Visual Studio 2008年签署的DLL上签字。

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.

我已经成功通过创建一个.TLB文件,以引用我的程序集

I've managed to reference my assembly by creating a .TLB file using

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

我收到以下错误:

I get the following error:

ActiveX组件不能创建对象

ActiveX component can't create Object

我是什么做错了吗?

本机,我与已安装到.NET 3.5框架。

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

推荐答案

确定后,来自此Thorsten Dittmar一些pretty的好点子,我终于得到这个东西的工作。 有些事情,来到了我们的讨论和其他东西的时候,我在网上找到了:

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:

NET框架需要在目标计算机上安装。 在.NET可编程性支持,需要在目标计算机上安装。

在AssemblyInfo.cs中一定要设置 The .NET framework needs to be installed on the target machine. .Net Programmability support needs to be installed on the target machine. 如何去掉Microsoft Office Word 2003的浏览记录

In AssemblyInfo.cs make sure you set

[总成:标记有ComVisible特性(真正)]

[assembly: ComVisible(true)]

由于托尔斯滕指出你需要有参数的公共构造在你的.NET类。

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

运行以下命令注册您的DLL在目标机器上。在 / codeBase的参数似乎做的伎俩我。的路径类型库(.tlb)或DLL没有关系。您位于C找到regasm:\ WINDOWS \ Microsoft.Net \框架\ 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 C:\ CompanyName.Net.dll /tlb:CompanyName.Net.tlb / codeBase的

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

使用工具>参考参考.tlb文件在VBA编辑器。

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

这是应该做的伎俩。

我还通过添加界面就为其启用在VB6 智能感知支持升级我的WebRequest类(这可悲的是在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).

 
精彩推荐
图片推荐