如果您有一个单一的无障碍环境,可使用Marshal.GetActiveObject
:>
using Microsoft.Office.Interop.Access;
using System.Runtime.InteropServices
...
try
{
var msAccess = (Application)Marshal.GetActiveObject("Access.Application");
msAccess.DoCmd.RunMacro("macCTI");
}
catch (COMException ex)
{
// handle error
}
或者,如果不止一个正在运行,而且没有两个情况有相同的数据库,那么你可以使用<<>Marshal.Bind Toiker
<<>:<
var msAccess = (Application) Marshal.BindToMoniker(@"x:fooar.accdb");
<>><>>说明: http://support.microsoft.com/kb/316126“rel=“nofollow” 微软知识基础: 必须指出:
Whether a COM server is Single Use (Multiple Instances) or
Multiuse (Single Instance) might affect your decision to use
GetActiveObject to get reference to that server. Because potentially
more than one instance of Word, Excel, or Microsoft Access can be
running, GetActiveObject on a particular server may return an instance
that you did not expect. The instance that is first registered in the
ROT is typically the instance that is returned by GetActiveObject. If
you want to get an Automation Reference to a specific running instance
of Word, Excel, or Microsoft Access, use BindToMoniker with the name
of the file that is opened in that instance. For a Multiuse (Single
Instance) server like PowerPoint, it does not matter, because the
automation reference points to the same running instance.