views:

199

answers:

4

I have cross posted on access-programmers.co.uk

Please post responses on that forum is possible..

I have compiled a dll in c# 3.5.. where I set the solution properties to register the build for COM interop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SBG_TestWithVBA
{
  [ClassInterface(ClassInterfaceType.AutoDual)]

  public class TestHello
  {
    [ComVisible(true)]

    public string SayHello(string name)
    {
      string message = "Hello " + name;
      return message;
    }
    public string HelloWorld()
    {
      string message = "Hello World!";
      return message;
    }
  }
}

I have then registered the assembly and tlb file using

"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe" "\sbgfs01\users\Vincent Pirolli\My Docs\Visual Studio 2008\Projects\TestWithVBA\TestWithVBA\bin\Release\SBG_TestWithVBA.dll" /tlb: "\sbgfs01\users\Vincent Pirolli\My Docs\Visual Studio 2008\Projects\TestWithVBA\TestWithVBA\bin\Release\SBG_TestWithVBA.tlb"

I then add the tlb file within vba from the tools>references menu..

and my VBA code is as follows:

Sub test()
    Dim f As SBG_TestWithVBA
    Set f = New SBG_TestWithVBA
    f.HelloWorld
End Sub

I get the following error even though Intellisense works within the VBA editor: "Automation error The system cannot find the file specified."

Anyone got any ideas? I have the articles in the previous posts but I cannot find the solution.

+1  A: 

It looks like the assembly you're registering is on a network share. .net assemblies have a different security model applied to them when that's the case.

Have you tried having the assembly and TLB in a local directory and then adding the TLB in the Tools > References menu?

Also - and this may well be off the mark utterly - but is there any need for the DLL to be registered with RegSvr32 so it's addressable?

Rob
thanks for the response.. i have tried installing and registering local (i unregistered the old location first).. but this did not solve the problem..when i use regsvr i get the following err:"...dll was loaded, but the DLLRegisterServer entry point was not found. This file cannot be registered".
winshent
One other thing worth a try - are you compiling them to x32? Office is 32bit, so even if your machine is 64bit, you'll need to compile your DLLs down to 32bit.Other than that, I'm stumped =(
Rob
yeah.. am compiling to 32bit.. i'm building my .net projects on a shared drive and then, copying to c drive and registering the assembly there.. should i move where i build my projects onto the c drive ?
winshent
the vba code should have read:Sub test() Dim f As SBG_TestWithVBA.TestHello Set f = New SBG_TestWithVBA.TestHello f.HelloWorldEnd Subthe error occurs on this line: "Set f = New SBG_TestWithVBA.TestHello"
winshent
A: 

ok, this is really annoying me now..

i've managed to get this to work.. but i dont understand why..

In solution properties within visual studio.. on the Build tab i set 'Register for COM interop' to true, and within 'Application>Assembly Information', i set 'Make assembly COM-Visible' = TRUE..

I have the VS project files in a shared drive. Now if i set the build location to the bin folder on the share drive, under my project files, when i call from VBA it doesnt work.. However, if i set the build folder to a folder on my local C Drive, then it works fine when calling from VBA..

Any ideas?

winshent
Does anything in http://support.microsoft.com/kb/817248 point towards the answer?
Rob
A: 

The fact that you can get it to work locally, yet it fails on a network share, points to a .NET security problem. .NET treats local resources differently from network resources and different from trusted Internet and untrusted Internet as well. I suspect that your .NET components are configured to trust local assemblies, but not to trust LocalIntranet assemblies.

See here for the configuration requirements to get Local Intranet and such to work:

http://support.microsoft.com/kb/892465

See this answer for a way to determine what zone an assembly is attempting to run in:

http://stackoverflow.com/questions/239463?sort=newest

As an aside I will say that I have found that configuration an environment to target a network share is fairly complex, as many of the assumptions that are made when working with code on the local machine are invalid. For this reason I prefer to install to the local machine where possible and only fight the security model when there is a strong reason to not install locally.

Godeke
A: 

If you want to learn more about the .net security, google for CASPOL.EXE. CASPOL.EXE is a security tool that ships with all .net frameworks. CASPOL stands for Code Acces Security POLicy. With this tool you can configure policies based on zones.