我正在尝试创建一个可在我正在构建的VBA项目中访问的.NET类库。我希望能够将dao.recorset对象作为参数传递给.net类库中的公共函数。
我的代码类似于
Public Sub PassDaoItem(item as DAO.recordSet)
TODO: [Code Here]
End Sub
当我使用regasm注册对象时,我收到以下错误:
Error
: Type library exporter cannot load type 'DAO.Recordset' (error: System.IO.FileN
otFoundException: Could not load file or assembly 'Interop.DAO, Version=5.0.0.0,
Culture=neutral, PublicKeyToken=5db0597a00edd538' or one of its dependencies. T
he system cannot find the file specified.).
关于如何做到这一点有什么建议吗?
答案 0 :(得分:1)
应该可以在.net代码中使用dao recorset添加对dao的引用: 在VS中打开“解决方案资源管理器”> “参考”> “添加参考”> “。Net”,你应该看到组件“dao”。
E.g。 C:\ Program Files(x86)\ Microsoft Visual Studio 9.0 \ Visual Studio Tools for Office \ PIA \ Office12 \ dao.dll。
访问VBA代码:
Option Compare Database
Option Explicit
Public Sub Test()
Dim db As DAO.Database
Dim rec As DAO.Recordset
Set db = Application.CurrentDb
Set rec = db.OpenRecordset("Table1")
Dim dotNetObject As New COMAccessibleTest.ReadData
dotNetObject.GetData rec
rec.Clone
db.Close
End Sub
C#代码:
using System.Runtime.InteropServices;
using System.Text;
namespace ReadDataTest
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("772de12c-3ade-4f43-9981-fd3c39691520")]
public interface IReadData
{
void GetData(ref dao.Recordset data);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("2acf2296-63b0-47b2-ab77-51c4e35e7365")]
[ProgId("ReadDataTest")]
public class ReadData : IReadData
{
public void GetData(ref dao.Recordset data)
{
var lines = new StringBuilder();
data.MoveFirst();
while (!data.EOF)
{
lines.AppendLine(data.Fields[0].Value + "," + data.Fields[1].Value + "," + data.Fields[2].Value + "," + data.Fields[3].Value);
data.MoveNext();
}
var file = new System.IO.StreamWriter("c:\\Temp\\test.txt");
file.WriteLine(lines);
file.Close();
}
}
}