|
Hi!
You'll have to create a COM object for this, VBA cannot access a function in a .NET assembly.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
What Mav said is true. But as an addition, you can write your code as C# and make your C# dll, then register it as COM dll then use it from your VBA macro.
I think this post[^] would be helpful.
Regards
|
|
|
|
|
Really?
Is it complicate to register a dll as com?
Ariadne
|
|
|
|
|
Well, not really. You can follow the link to the post I gave you in the previous reply.
Here is the link again[^].
Regards
|
|
|
|
|
you should check : ServicedComponent
|
|
|
|
|
Thanks.
If I use tlbexp I get only my class exposed, but not my methods. Any ideas? (I suppose, the methods have to have a certain format.)
Nevertheless I do not belive, that a COM-object is the only way to get a connection to VBA. With a COM-object you do not need a Declare Function statement. But with a Declare Function one can access all routines eg. in the kernel.dll. (And that are not COM's in kernel.dll).
Ariadne
|
|
|
|
|
If you got your class exposed, how come not the methods in it?? Aren't they public?
If there is some sort of a VBA .Net language then this would be a solution to your problem, bur otherwise there is no other direct conection between umanaged-to-managed code that I know of.
Regards
|
|
|
|
|
public class PW
{
public static string Convert5Bit(byte[] abytID)
{
return sConv;
}
}
This is the declaration. But only PW as class is exposed.
-- modified at 5:19 Thursday 26th October, 2006
Ariadne
|
|
|
|
|
First:
Post your question to my answers, not to your question. I only saw your question by chance.
Second:
Try to reference to your new COM object in a C++ or C# project and see what is/isn't exposed
Third:
If none other works -I'm not that expert in VBA so if applicable- Make your method non-static, and call it form an instance of the class rather than the calss itself.
Ps.
BTW, what way did you know by it's not exposed?
Regards
|
|
|
|
|
thanks for 1st.
2nd I look into the code with notepad and try in VBA the Object-Browser. both show only PW and not Convert5Bit()
3rd it tried that, but no success.
thanks for your ansers!
Ariadne
|
|
|
|
|
Ariadne wrote: Object-Browser. both show only PW and not Convert5Bit()
Well, in that object browser the method should be under the class name, shouldn't it? I mean you access the method by the class name? Also what file did you look into using the notepad?
Ariadne wrote: 3rd it tried that, but no success.
Define "no success"? You can't instantiate the object from the exposed class? Or the instance can't access the member function?
Regards
|
|
|
|
|
Nader Elshehabi wrote: Also what file did you look into using the notepad?
I opened the tlb-File with notepad: There was no Convert5Bit.
Nader Elshehabi wrote: object browser the method should be under the class name
Under the class name is no method.
If I instantiate the class I get an runtime error: 429 ActiveX component can't create object.
But VBA knows the class PW.
Ariadne
|
|
|
|
|
Well, I haven't tested the issue thoroughly. I did some googling and found this post on MSDN forum
Post link[^]
and here are two more articles in MSDN about the issue:
Article 1[^]
Article 2[^]
Also I did what I told you. I made a tlb from the .Net dll, and it actually references to the class and the methods inside the class!! About the instantiation errer; Did you make your constructor public?
I hope this directs you somewhere. Good luck! And I'm waiting for your feedback.
Regards
|
|
|
|
|
Thanks for your articles.
I read a lot of stuff too.
But it seams there is something basic wrong.
here the story, what I try:
1. I build a classlibrary-project in VS8 C#.
2. I insert one TestDLL class:
using System;
using System.Collections.Generic;
using System.Text;
public class TestDLL
{
public string StringChange(string str)
{
return str +" from C#";
}
}
3. I build the project with a TestDLL.dll as result
4. I run tlbexp TestDLL.dll /win 32 /out:TestDLL.tlb
5. An inspection of TestDLL.tlb with notepad.exe shows no StringChange string.
6. I run regasm TestDLL.dll
It returns a waring: RA00000 : there are no types registered (retranslation from german)
7. I tested the same on a virgin VS8 Enviroment on my home notebook with the same result.
8. I have no idea....
Ariadne
|
|
|
|
|
That's it: in assemblyinfo.cs there is a switch:
[assembly: ComVisible(true)]
but it was false
Now all works fine...
Thank you for your support Nader
Ariadne
|
|
|
|
|
VBA does not support static functions.
Also, you are confusing Declare with COM.
To get at an exposed COM object in VBA, ad a reference to it, then instansiate the class.
Declare Function has nothing to do with COM - it uses access points, which cannot be created from C#.
To create an access point, use an MFC dll.
Schabse S. Laks
|
|
|
|
|
i have 3 projects in my .sln file.
1 - webproject (vb.net), 2 vb.net class library(Schemas) 3. c# class library (Managers).
i am passing Schemas object from code behind of web project to a method of Managers class.
it is giving an error as
"Reference required to assembly 'Schemas' containing the type 'Schemas.mySchemas'. Add one to your project."
All the references correct.
current references :
webproject having ref. to Schemas and Managers prjs
Managers having ref. to Schemas prj.
Please suggest me how to go about this issue.
Thank you inadvance.
-- modified at 1:25 Thursday 26th October, 2006
PradeepG
|
|
|
|
|
Hi,
if your passing an object of one of the Schema classes to a Managers class than Managers also needs a reference to Schemas . The fact that webproject passed it over doesn't matter in this context.
Robert
|
|
|
|
|
Hi Robert,
Managers class also having a reference to 'Schemas'
This problem is giving when i am passing a vb.net class object to a c# class method.
It was working for vb.net class object to a vb.net class method.
Problem is peculiar
PradeepG
|
|
|
|
|
pdeep wrote: Managers class also having a reference to 'Schemas'
So it won't allow you to create a reference because then you'd end up with a circular reference.
This generally indicates that there is something wrong with the design. You may like to refactor your classes to remove the circular reference between assemblies. Either by putting all the classes in one assembly, or defining more clearly the boundaries between assemblies.
[ADDITIONAL]
The problem has nothing to do with whether the code was written in C# or VB.NER - it is all to do with which assembly the code is in.
[/ADDITIONAL]
|
|
|
|
|
Colin Angus Mackay wrote: The problem has nothing to do with whether the code was written in C# or VB.NER - it is all to do with which assembly the code is in.
Nice typo! I first read: VB.NERD
Robert
|
|
|
|
|
I have a dataset that loads an xml file. What i'm wanting to do is load each row into an array. each row has 9 elements. When i try to loop through the item array for that row i get a null reference. I'm not sure of an easier way of doing it. Could someone help me out please?
DataSet ds = new DataSet();
ds.ReadXml(filePath);
foreach (DataRow r in ds.Tables[0].Rows)
{
for (int d = 0; d <= 8; d++)
{
xmldata[d] = r.ItemArray[d].ToString();
}
}
When i ran through the debugger i did verifiy that r.ItemArray[d] did have a string in there.
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
Hi,
hard to say. Generally there shouldn't be null values in the rows (only DBNulls).
Are you sure you have you have initialized xmldata correctly?
string[] xmldata = new string[9];
Robert
|
|
|
|
|
Are you defining the array xmldata? Is it large enough to hold 8 items? You can try adding items to an arraylist and then convert that to an array.
I know this is example code, but as read, xmldata will hold the items for the last row in the DB when it exits the loop.
|
|
|
|
|
yeah i am defining a string array without a limit to hold items:
string[] xmldata;
What i'm wanting to do is load an xml into an array then edit the array then once satisfied with the array i am going to write the array into an xml document. When i directly load the listview it loads properly with the code below
private void Load_Xml()
{
DataSet ds = new DataSet();
ds.ReadXml(filePath);
foreach (DataRow r in ds.Tables[0].Rows)
{
ListViewItem lvindex = new ListViewItem(r.ItemArray[0].ToString());
ListViewItem.ListViewSubItem sublvindex = new ListViewItem.ListViewSubItem(lvindex, r.ItemArray[1].ToString());
lvindex.SubItems.Add(sublvindex);
sublvindex = new ListViewItem.ListViewSubItem(lvindex, r.ItemArray[2].ToString());
lvindex.SubItems.Add(sublvindex);
lv_xmlList.Items.Add(lvindex);
}
}
Don't be overcome by evil, but overcome evil with good
|
|
|
|