Access automation does not work from 64 bit .Net application

 

Summary

A .Net 64 bit application automating Microsoft Access throws an error while opening Access database. Same code works fine when run as 32 bit .Net application. Below is the sample code to reproduce this issue:

Microsoft.Office.Interop.Access.Application accessApplication;

accessApplication = new Microsoft.Office.Interop.Access.Application();

accessApplication.OpenCurrentDatabaseOld("<Path to .accdb file>", true);

Below is the exception message:

"Unable to cast COM object of type 'system._comObject' to interface type 'Microsoft.Office.Interop.Access.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{<missing text>}'failed due to the following error: Error loading type library/DLL. (Exception from HRESULT:0x80029C4A(TYPE_E_CANTLOADLIBRARY)).".

 

 

Symptoms

Error message : “Error loading type library/DLL. (Exception from HRESULT:0x80029C4A(TYPE_E_CANTLOADLIBRARY)) “.

 

 

Cause

64 bit .Net application is not able to find DAO360.dll which is looked-up during Access automation and is located at "C:\Program Files (x86)\Common Files\Microsoft shared\DAO". This Path is not listed in the probing directories of .Net application.

Also DAO360.dll is registered only under Wow6432Node and hence 64 bit .Net application is not able to find it via registry lookup.

 

 

Resolution

Adding DAO360.dll’s path to ‘PATH’ environment variable resolve this issue. Note that directories listed under PATH environment variable are included in the proving directories of .Net application.

Below is the code to modify ‘PATH’ environment variable in-process. In-process means that changes to PATH environment variable will not be visible outside the process or after application quits. This code should be pasted before instantiating Access.

string PathValue = "";

string sAdd = "";

string strCommonFiles =

System.Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");

sAdd = ";" + strCommonFiles + "\\microsoft shared\\DAO";

PathValue = System.Environment.GetEnvironmentVariable("Path");

PathValue += sAdd;

bool result = SetEnvironmentVariable("path", PathValue);