pass VBA class module to c# objects

  • Thread starter Iulian Dan Repolschi
  • Start date
I

Iulian Dan Repolschi

I have an object in c# that oppens msAccess and run a function from a
module. The function has one parameter, a c# object.
In Vba, the c# object is used to create another c# object, and after
that, a function is called on the last created c# object.
The function called has one parameter, a "System.Object".
If the parameter passed is a "String", even a VBA Form, eveything
works just fine, but when trying to pass a Class Module Object, I get
an Automation Error, with no details about it.
Does anybody has any ideea how to solve this problem?

Yours,
Iulian Dan Repolschi
 
M

Mary Chipman

It would probably help if you posted the code that's causing the error
along with the exact error message.

--Mary
 
I

Iulian Dan Repolschi

Here is part of the code that it is involved.
The error appears when a function from an access module is invoked:
(msaccess.GetType().InvokeMember("Run", BindingFlags.InvokeMethod, null, msaccess,parameters );)
Actually it happens in the vba access code (look at the classes and code involved and the portion of vba code where the error actually appears).

C#:

interface I
{
void Test(string s);
}

public class C:I
{
private object msaccess;
private object o;

public C(object ms)
{
msaccess=ms;
}

public void Test(String s)
{
MessageBox.Show(s);
}

public void setObject(object obj)
{
o=obj;
}
}


public class Creator
{
private object msaccess;
public Creator(object ms)
{
msaccess=ms;
}

public I getObject(string objName)
{
switch (objName.ToUpper())
{
default:
return new C(this.msaccess);
}
}
}

public class Runner
{
public Runner()
{
}

public void Function1()
{
object msaccess=Activator.CreateInstance(Type.GetTypeFromProgID("Access.Application.8",true));
object[] parameters;
parameters = new Object[1];
parameters[0]=true;
accesswindowhandler=new System.IntPtr((int)msaccess.GetType().InvokeMember("hWndAccessApp", BindingFlags.InvokeMethod, null, msaccess,null ));

parameters = new Object[1];
parameters[0]="db1.mdb";
msaccess.GetType().InvokeMember("OpenCurrentDatabase",BindingFlags.InvokeMethod, null, msaccess,parameters );

object[] parameters = new Object[2];
parameters[0]="db1VbaFunc1";
parameters[1]=new Creator(msaccess);
msaccess.GetType().InvokeMember("Run", BindingFlags.InvokeMethod, null, msaccess,parameters );
}

}


and here is the Access part:

Vba class module: clsTest
Option Compare Database
Option Explicit

Public Sub Test()
MsgBox "Hallo"
End Sub


Module1:

Public Function db1VbaFunc1(b As Object)
Dim creator As Object
Set creator = b
Dim newObj As Object
Set newObj = creator.getObject("TEST")
Dim objTest As Variant
Set objTest = New clsTest
newObj.setObject objTest
End Function


Everything works ok, but when I try to invoke "setObject" on "newObj" ("newObj.setObject objTest"),
I got an automation error:
"Run-time error: '-1073741819 (c0000005) Automation error"

Can anyone help me?

Yours,
Iulian Dan Repolschi.
 
E

Eric Marvets

You can't do what you are trying to do, but it can be done.

There is no quick answer, but I can put you on the right track. Start by
reading up on registering .NET components to COM here:

http://msdn.microsoft.com/library/d.../cpconexposingnetframeworkcomponentstocom.asp


--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email (e-mail address removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top