If you have a single instance of Access running you could use 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.BindToMoniker :Alternatively, if more than one are running, and no two instances have the same database open, you could use Marshal.BindToMoniker:var msAccess = (Application) Marshal.BindToMoniker(@"x:\foo\bar.accdb"); 注意::此 Microsoft知识库文章具有这样说: Note: This Microsoft knowledge base article has this to say: COM服务器是一次性使用(多个实例)还是多次使用(单实例)可能会影响您使用的决定GetActiveObject以获取对该服务器的引用.因为可能可以有多个Word,Excel或Microsoft Access实例运行中,特定服务器上的GetActiveObject可能会返回一个实例你没想到的.首先在ROT通常是GetActiveObject返回的实例.如果您想要获取对特定正在运行的实例的自动化参考Word,Excel或Microsoft Access的名称使用BindToMoniker在该实例中打开的文件.对于多用途(单个实例)服务器(例如PowerPoint),这没关系,因为自动化参考指向相同的运行实例. 这篇关于与Microsoft Access互操作不与用户会话交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 09:03