0

我正在尝试创建一个可在我正在构建的 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.).

对我如何做到这一点有什么建议吗?

4

1 回答 1

1

应该可以在 .net 代码中使用 dao recorset 只需添加对 dao 的引用:在 VS 中打开“解决方案资源管理器”>“引用”>“添加引用”>“.Net”,您应该会看到组件“dao”。

例如 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();
        }
    }
}
于 2012-10-25T07:46:32.100 回答