Tôi đã tạo một DLL trong C# bằng cách sử dụng khuôn khổ .NET 3.0.Thực thi mã .NET 3.0 từ Office 2003
Dưới đây là mã của DLL của tôi
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;
}
}
}
Tôi đang cố gắng để có được mã này để chạy từ mã VBA Office 2003. DLL được ký bằng cách sử dụng ký Visual Studio 2008 mặc định.
tôi đã quản lý để tham khảo lắp ráp của tôi bằng cách tạo ra một tập tin .tlb sử dụng
regasm /tlb c:\CompanyName.Net.dll
Nhưng khi tôi muốn tạo một thể hiện của đối tượng:
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
tôi nhận được lỗi sau :
ActiveX component can't create Object
Tôi đang làm gì sai?
Máy tôi đang thử nghiệm đã cài đặt .NET lên khung .NET 3.5.
Đây là hướng dẫn ngắn gọn và đầy đủ nhất về vấn đề này mà tôi tìm thấy sau hàng giờ cố gắng làm cho việc này hoạt động. Cảm ơn bạn. –