Click here to Skip to main content
15,173,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i m trying to run a macro from ms access 2010 from vb.net 2010 ..



C#
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


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
Posted
Updated 19-Jul-20 3:05am

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

VB.NET
Dim oAccess As Access.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

VB.NET
Dim oAccess As Microsoft.Office.Interop.Access.ApplicationClass
   
Comments
yuganshu soni 12-Apr-16 11:20am
   
thank you friend but its now giving error:
Reference to class 'ApplicationClass' is not allowed when its assembly is linked using No-PIA mode.
F-ES Sitecore 12-Apr-16 12:20pm
   
Google "not allowed when its assembly is linked using No-PIA mode" for suggestions, at least do basic research before asking a question.
yuganshu soni 12-Apr-16 12:22pm
   
i had already done that, Nothing relevant found!
Imports System.ComponentModel
Imports Microsoft.Office.Interop.Access

Public Sub Run_Access_Procedure(ProcName As String)
Dim oAccess As New Microsoft.Office.Interop.Access.Application()
oAccess.Visible = False
oAccess.OpenCurrentDatabase("C:\TEST\Test.accdb")
'Run the procedure.
oAccess.Run(ProcName)
oAccess.DoCmd().Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
oAccess = Nothing
End Sub
   
Comments
CHill60 20-Jul-20 4:14am
   
A code dump does not make a good answer. Explain why yours works when the OPs did not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900