2009-11-04 4 views
8

.NET 3.0 프레임 워크를 사용하여 C#에서 DLL을 만들었습니다.Office 2003에서 .NET 3.0 코드 실행

다음은 내가 Office 2003의 VBA 코드에서 실행하려면이 코드를 얻으려고

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; 
     } 
    } 
} 

내 DLL의 코드입니다. DLL은 기본 Visual Studio 2008 서명을 사용하여 서명되었습니다.

나는

regasm /tlb c:\CompanyName.Net.dll 

를 사용하여 .TLB 파일을 만들어 내 어셈블리를 참조하는 관리하지만 한 나는 개체의 인스턴스를 생성 할 때

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 

나는 다음과 같은 오류를 :

ActiveX component can't create Object

내가 뭘 잘못하고 있니?

내가 테스트하고있는 컴퓨터에는 .NET 3.5 프레임 워크까지 .NET이 설치되어 있습니다.

답변

10

좋아요, Thorsten Dittmar의 꽤 좋은 아이디어를 마친 후에는 마침내이 문제가 해결되었습니다. 우리의 토론과 다른 웹 사이트에서 발견 된 것들 :

  1. .NET Framework를 대상 컴퓨터에 설치해야합니다.
  2. .Net 대상 컴퓨터에 프로그래밍 지원 기능을 설치해야합니다. 의 AssemblyInfo.cs에서
  3. 는 토르스텐 당신이 당신의 닷넷 클래스에 매개 변수없는 public 생성자가 필요 지적이

    [assembly: ComVisible(true)]

  4. 을 설정해야합니다.

  5. 프로젝트 등록 정보 페이지의 빌드 탭에서 "COM Interop에 등록"을 선택했는지 확인하십시오.
  6. 프로젝트의 속성 페이지에서 서명 탭을 사용하여 프로젝트에 서명해야합니다.
  7. 이 명령을 실행하여 대상 컴퓨터에 DLL을 등록하십시오. /코드베이스 매개 변수는 저를 위해 속임수를 쓴 것처럼 보였습니다. 형식 라이브러리 (.tlb) 또는 DLL에 대한 경로는 중요하지 않습니다. 당신은 C에서 RegAsm을 찾을 수 있습니다 : \ WINDOWS를 \ Microsoft.Net 프레임 워크 \ v2.050727 \는 RegAsm.exe

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

  8. 참조 도구> 참조를 사용하여 VBA 편집기에서 .TLB 파일 \.

  9. dll을 C : \에서 GAC로 C : \ Windows \ assembly \ 으로 드래그하십시오. (처음에는이 사실을 알지 못했지만 분명히 Office에서 어셈블리를 찾아야합니다.)

그 트릭을해야합니다.

또한 웹 인터페이스를 추가하여 Webrequest 클래스를 업그레이드하여 VB6에서 IntelliSense 지원을 사용하도록 설정했습니다 (이는 VBA에서 슬프게도 작동하지 않습니다).

+0

이것은 이것이 작동하도록 노력한 몇 시간 후에 발견 한이 문제에 대한 가장 간결하고 완전한 가이드입니다. 고맙습니다. –

2

COM 클래스에 명시적인 매개 변수없는 생성자가 필요합니다. 클래스 정의를

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() 
     { 
     } 

     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; 
     } 
    } 
} 

으로 변경하십시오.

+0

방금 ​​시도했지만 여전히 동일한 오류가 발생합니다. 내 프로젝트를 "COM Interop에 등록"이라고 표시했지만 기쁨은 없습니다. 어쩌면 내가 VBA에서 내 기능을 부르는 방식이 잘못 됐을 까? 나는 손실에있다 – Peter

+0

DLL을 다시 등록 했습니까? –

+0

DLL을 다시 등록해야하는 tlb 파일을 다시 만들었습니다. GAC에 다시 추가 할 수 있습니까? – Peter

관련 문제