如何从VB.NET运行ms访问宏 [英] How to run ms access macro from VB.NET
问题描述
i m trying to run a macro from ms access 2010 from vb.net 2010 ..
< br $>
this is giving me following error..
Unable to cast COM object of type 'Microsoft.Office.Interop.Access.ApplicationClass' to class type 'WindowsApplication1.Access.ApplicationClass'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
i don't know what to do plz help... its very urgent
我的尝试:
Dim oAccess As Access.ApplicationClass
'开始访问并打开数据库。
oAccess = CreateObject(Access.Application)
oAccess.Visible = True
oAccess.OpenCurrentDatabase(C:\Users\Yuganshu \Desktop\New folder(4)\ WindowsWindowsApplication1 \ db .mdb,False)
'运行宏。
oAccess.Run(Macro1)
'oAccess.Run(DoKbTestWithParameter,来自VB .NET客户端的Hello)
'清理:退出访问而不保存对数据库的更改。
oAccess.DoCmd()。退出(Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObj等(oAccess)
oAccess = Nothing
What I have tried:
Dim oAccess As Access.ApplicationClass
'Start Access and open the database.
oAccess = CreateObject("Access.Application")
oAccess.Visible = True
oAccess.OpenCurrentDatabase("C:\Users\Yuganshu\Desktop\New folder (4)\WindowsApplication1\db.mdb", False)
'Run the macros.
oAccess.Run("Macro1")
'oAccess.Run("DoKbTestWithParameter", "Hello from VB .NET Client")
'Clean-up: Quit Access without saving changes to the database.
oAccess.DoCmd().Quit(Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess = Nothing
推荐答案
我假设您的解决方案中还有一个名为Access.ApplicationClass的类,可能是因为您创建一个Access文件夹并在其中放入一个ApplicationClass,所以当你定义
I assume you also have a class called Access.ApplicationClass in your solution, probably because you created an Access folder and put an ApplicationClass in it, so when you define
Dim oAccess As Access.ApplicationClass
它是定义错误的类型。由于你的项目中有ApplicationClass而且你引用了一个,你需要明确你的意思
it is defining the wrong type. As you have the ApplicationClass in your project and one referenced you need to be explicit about which one you mean
Dim oAccess As Microsoft.Office.Interop.Access.ApplicationClass
这篇关于如何从VB.NET运行ms访问宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!